Skip to content

Commit b07292a

Browse files
committed
使用控制反转来处理
1 parent 70ae196 commit b07292a

File tree

1 file changed

+67
-36
lines changed

1 file changed

+67
-36
lines changed

src/Wechat.php

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
namespace xutl\wechat;
99

1010
use Yii;
11-
use yii\base\Component;
12-
use yii\base\InvalidConfigException;
13-
use yii\base\InvalidParamException;
14-
use yii\caching\Cache;
1511
use yii\di\Instance;
12+
use yii\caching\Cache;
13+
use yii\di\ServiceLocator;
14+
use yii\base\InvalidConfigException;
1615
use xutl\wechat\js\Js;
1716
use xutl\wechat\url\Url;
1817
use xutl\wechat\menu\Menu;
@@ -22,6 +21,7 @@
2221
use xutl\wechat\material\Material;
2322
use xutl\wechat\material\Temporary;
2423

24+
2525
/**
2626
* Class Wechat
2727
*
@@ -37,7 +37,7 @@
3737
* @property Cache $cache
3838
* @package xutl\wechat
3939
*/
40-
class Wechat extends Component
40+
class Wechat extends ServiceLocator
4141
{
4242
/**
4343
* @var string
@@ -54,6 +54,32 @@ class Wechat extends Component
5454
*/
5555
public $cache = 'cache';
5656

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+
5783
/**
5884
* Initializes the object.
5985
* This method is invoked at the end of the constructor after the object is initialized with the
@@ -80,9 +106,7 @@ public function init()
80106
*/
81107
public function getAccessToken()
82108
{
83-
return Yii::createObject([
84-
'class' => 'xutl\wechat\AccessToken',
85-
]);
109+
return $this->get('accessToken');
86110
}
87111

88112
/**
@@ -91,9 +115,7 @@ public function getAccessToken()
91115
*/
92116
public function getOauth()
93117
{
94-
return Yii::createObject([
95-
'class' => 'xutl\wechat\oauth\OAuth',
96-
]);
118+
return $this->get('oauth');
97119
}
98120

99121
/**
@@ -102,9 +124,7 @@ public function getOauth()
102124
*/
103125
public function getJs()
104126
{
105-
return Yii::createObject([
106-
'class' => 'xutl\wechat\js\Js',
107-
]);
127+
return $this->get('js');
108128
}
109129

110130
/**
@@ -113,9 +133,7 @@ public function getJs()
113133
*/
114134
public function getNotice()
115135
{
116-
return Yii::createObject([
117-
'class' => 'xutl\wechat\notice\Notice',
118-
]);
136+
return $this->get('notice');
119137
}
120138

121139
/**
@@ -124,48 +142,61 @@ public function getNotice()
124142
*/
125143
public function getUrl()
126144
{
127-
return Yii::createObject([
128-
'class' => 'xutl\wechat\url\Url',
129-
]);
145+
return $this->get('url');
130146
}
131147

132148
/**
133149
* @return object|Menu
134150
* @throws InvalidConfigException
135151
*/
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');
140155
}
141156

142157
/**
143158
* @return object|QRCode
144159
* @throws InvalidConfigException
145160
*/
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');
150164
}
151165

152166
/**
153167
* @return object|Material
154168
* @throws InvalidConfigException
155169
*/
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');
160173
}
161174

162175
/**
163176
* @return object|Temporary
164177
* @throws InvalidConfigException
165178
*/
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+
];
170201
}
171202
}

0 commit comments

Comments
 (0)