Skip to content

Commit

Permalink
增加启动时模块替换
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyan74 committed Jul 31, 2019
1 parent bc3f690 commit 2fbdfa8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ composer require jianyan74/yii2-easy-wechat
'userOptions' => [], // 用户身份类参数
'sessionParam' => 'wechatUser', // 微信用户信息将存储在会话在这个密钥
'returnUrlParam' => '_wechatReturnUrl', // returnUrl 存储在会话中
'rebinds' => [ // 自定义服务模块
// 'cache' => 'common\components\Cache',
]
],
// ...
]
Expand Down
29 changes: 24 additions & 5 deletions src/Wechat.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class Wechat extends Component
*/
public $returnUrlParam = '_wechatReturnUrl';

/**
* @var array
*/
public $rebinds = [];

/**
* 微信SDK
*
Expand Down Expand Up @@ -157,6 +162,7 @@ public function getApp()
{
if (!self::$_app instanceof \EasyWeChat\OfficialAccount\Application) {
self::$_app = Factory::officialAccount(Yii::$app->params['wechatConfig']);
!empty($this->rebinds) && self::$_app = $this->rebind(self::$_app);
}

return self::$_app;
Expand All @@ -171,6 +177,7 @@ public function getPayment()
{
if (!self::$_payment instanceof \EasyWeChat\Payment\Application) {
self::$_payment = Factory::payment(Yii::$app->params['wechatPaymentConfig']);
!empty($this->rebinds) && self::$_payment = $this->rebind(self::$_payment);
}

return self::$_payment;
Expand All @@ -185,6 +192,7 @@ public function getMiniProgram()
{
if (!self::$_miniProgram instanceof \EasyWeChat\MiniProgram\Application) {
self::$_miniProgram = Factory::miniProgram(Yii::$app->params['wechatMiniProgramConfig']);
!empty($this->rebinds) && self::$_miniProgram = $this->rebind(self::$_miniProgram);
}

return self::$_miniProgram;
Expand All @@ -199,6 +207,7 @@ public function getOpenPlatform()
{
if (!self::$_openPlatform instanceof \EasyWeChat\OpenPlatform\Application) {
self::$_openPlatform = Factory::openPlatform(Yii::$app->params['wechatOpenPlatformConfig']);
!empty($this->rebinds) && self::$_openPlatform = $this->rebind(self::$_openPlatform);
}

return self::$_openPlatform;
Expand All @@ -213,6 +222,7 @@ public function getWork()
{
if (!self::$_work instanceof \EasyWeChat\Work\Application) {
self::$_work = Factory::work(Yii::$app->params['wechatWorkConfig']);
!empty($this->rebinds) && self::$_work = $this->rebind(self::$_work);
}

return self::$_work;
Expand All @@ -227,11 +237,24 @@ public function getOpenWork()
{
if (!self::$_openWork instanceof \EasyWeChat\OpenWork\Application) {
self::$_openWork = Factory::openWork(Yii::$app->params['wechatOpenWorkConfig']);
!empty($this->rebinds) && self::$_openWork = $this->rebind(self::$_openWork);
}

return self::$_openWork;
}

/**
* @param $app
*/
public function rebind($app)
{
foreach ($this->rebinds as $key => $class) {
$app->rebind($key, new $class());
}

return $app;
}

/**
* 获取微信身份信息
*
Expand Down Expand Up @@ -262,11 +285,7 @@ public function __get($name)
try {
return parent::__get($name);
} catch (\Exception $e) {
if ($this->getApp()->$name) {
return $this->app->$name;
} else {
throw $e->getPrevious();
}
throw $e->getPrevious();
}
}

Expand Down

0 comments on commit 2fbdfa8

Please sign in to comment.