8
8
namespace xutl \wechat ;
9
9
10
10
use Yii ;
11
- use yii \base \Component ;
12
- use yii \base \InvalidConfigException ;
13
- use yii \base \InvalidParamException ;
14
- use yii \caching \Cache ;
15
11
use yii \di \Instance ;
12
+ use yii \caching \Cache ;
13
+ use yii \di \ServiceLocator ;
14
+ use yii \base \InvalidConfigException ;
16
15
use xutl \wechat \js \Js ;
17
16
use xutl \wechat \url \Url ;
18
17
use xutl \wechat \menu \Menu ;
22
21
use xutl \wechat \material \Material ;
23
22
use xutl \wechat \material \Temporary ;
24
23
24
+
25
25
/**
26
26
* Class Wechat
27
27
*
37
37
* @property Cache $cache
38
38
* @package xutl\wechat
39
39
*/
40
- class Wechat extends Component
40
+ class Wechat extends ServiceLocator
41
41
{
42
42
/**
43
43
* @var string
@@ -54,6 +54,32 @@ class Wechat extends Component
54
54
*/
55
55
public $ cache = 'cache ' ;
56
56
57
+ /**
58
+ * Payment constructor.
59
+ * @param array $config
60
+ */
61
+ public function __construct ($ config = [])
62
+ {
63
+ $ this ->preInit ($ config );
64
+ parent ::__construct ($ config );
65
+ }
66
+
67
+ /**
68
+ * 预处理组件
69
+ * @param array $config
70
+ */
71
+ public function preInit (&$ config )
72
+ {
73
+ // merge core components with custom components
74
+ foreach ($ this ->coreComponents () as $ id => $ component ) {
75
+ if (!isset ($ config ['components ' ][$ id ])) {
76
+ $ config ['components ' ][$ id ] = $ component ;
77
+ } elseif (is_array ($ config ['components ' ][$ id ]) && !isset ($ config ['components ' ][$ id ]['class ' ])) {
78
+ $ config ['components ' ][$ id ]['class ' ] = $ component ['class ' ];
79
+ }
80
+ }
81
+ }
82
+
57
83
/**
58
84
* Initializes the object.
59
85
* This method is invoked at the end of the constructor after the object is initialized with the
@@ -80,9 +106,7 @@ public function init()
80
106
*/
81
107
public function getAccessToken ()
82
108
{
83
- return Yii::createObject ([
84
- 'class ' => 'xutl\wechat\AccessToken ' ,
85
- ]);
109
+ return $ this ->get ('accessToken ' );
86
110
}
87
111
88
112
/**
@@ -91,9 +115,7 @@ public function getAccessToken()
91
115
*/
92
116
public function getOauth ()
93
117
{
94
- return Yii::createObject ([
95
- 'class ' => 'xutl\wechat\oauth\OAuth ' ,
96
- ]);
118
+ return $ this ->get ('oauth ' );
97
119
}
98
120
99
121
/**
@@ -102,9 +124,7 @@ public function getOauth()
102
124
*/
103
125
public function getJs ()
104
126
{
105
- return Yii::createObject ([
106
- 'class ' => 'xutl\wechat\js\Js ' ,
107
- ]);
127
+ return $ this ->get ('js ' );
108
128
}
109
129
110
130
/**
@@ -113,9 +133,7 @@ public function getJs()
113
133
*/
114
134
public function getNotice ()
115
135
{
116
- return Yii::createObject ([
117
- 'class ' => 'xutl\wechat\notice\Notice ' ,
118
- ]);
136
+ return $ this ->get ('notice ' );
119
137
}
120
138
121
139
/**
@@ -124,48 +142,61 @@ public function getNotice()
124
142
*/
125
143
public function getUrl ()
126
144
{
127
- return Yii::createObject ([
128
- 'class ' => 'xutl\wechat\url\Url ' ,
129
- ]);
145
+ return $ this ->get ('url ' );
130
146
}
131
147
132
148
/**
133
149
* @return object|Menu
134
150
* @throws InvalidConfigException
135
151
*/
136
- public function getMenu (){
137
- return Yii::createObject ([
138
- 'class ' => 'xutl\wechat\menu\Menu ' ,
139
- ]);
152
+ public function getMenu ()
153
+ {
154
+ return $ this ->get ('menu ' );
140
155
}
141
156
142
157
/**
143
158
* @return object|QRCode
144
159
* @throws InvalidConfigException
145
160
*/
146
- public function getQrcode (){
147
- return Yii::createObject ([
148
- 'class ' => 'xutl\wechat\qrcode\QRCode ' ,
149
- ]);
161
+ public function getQrcode ()
162
+ {
163
+ return $ this ->get ('qrcode ' );
150
164
}
151
165
152
166
/**
153
167
* @return object|Material
154
168
* @throws InvalidConfigException
155
169
*/
156
- public function getMaterial (){
157
- return Yii::createObject ([
158
- 'class ' => 'xutl\wechat\material\Material ' ,
159
- ]);
170
+ public function getMaterial ()
171
+ {
172
+ return $ this ->get ('material ' );
160
173
}
161
174
162
175
/**
163
176
* @return object|Temporary
164
177
* @throws InvalidConfigException
165
178
*/
166
- public function getMaterialTemporary (){
167
- return Yii::createObject ([
168
- 'class ' => 'xutl\wechat\material\Temporary ' ,
169
- ]);
179
+ public function getMaterialTemporary ()
180
+ {
181
+ return $ this ->get ('materialTemporary ' );
182
+ }
183
+
184
+ /**
185
+ * Returns the configuration of wechat components.
186
+ * @see set()
187
+ */
188
+ public function coreComponents ()
189
+ {
190
+ return [
191
+ 'accessToken ' => ['class ' => 'xutl\wechat\AccessToken ' ],
192
+ 'oauth ' => ['class ' => 'xutl\wechat\oauth\OAuth ' ],
193
+ 'js ' => ['class ' => 'xutl\wechat\js\Js ' ],
194
+ 'notice ' => ['class ' => 'xutl\wechat\notice\Notice ' ],
195
+ 'url ' => ['class ' => 'xutl\wechat\url\Url ' ],
196
+ 'menu ' => ['class ' => 'xutl\wechat\menu\Menu ' ],
197
+ 'qrcode ' => ['class ' => 'xutl\wechat\qrcode\QRCode ' ],
198
+ 'material ' => ['class ' => 'xutl\wechat\material\Material ' ],
199
+ 'materialTemporary ' => ['class ' => 'xutl\wechat\material\Temporary ' ],
200
+ ];
170
201
}
171
202
}
0 commit comments