33namespace Overtrue \LaravelWechat ;
44
55use Illuminate \Support \ServiceProvider as LaravelServiceProvider ;
6+ use Overtrue \Wechat \Server as WechatServer ;
67use Overtrue \Wechat \Alias ;
78
89class ServiceProvider extends LaravelServiceProvider
910{
11+ /**
12+ * 延迟加载
13+ *
14+ * @var boolean
15+ */
1016 protected $ defer = true ;
1117
1218 /**
@@ -30,6 +36,7 @@ class ServiceProvider extends LaravelServiceProvider
3036 'wechat.image ' => 'Overtrue \\Wechat \\Image ' ,
3137 ];
3238
39+
3340 /**
3441 * Boot the provider.
3542 *
@@ -53,26 +60,28 @@ public function register()
5360 Alias::register ();
5461 }
5562
56- $ this ->app ->singleton ('wechat.server ' , function ($ app ){
57- return new WechatServer (config ('wechat.appId ' ), config ('wechat.token ' ), config ('wechat.encodingAESKey ' ));
63+ if (config ('wechat ' ) == include __DIR__ . '/config.php ' ) {
64+ throw new \Exception ("请先在config/wechat.php完成微信相关配置 " );
65+ }
66+
67+ $ this ->app ->singleton (['Overtrue \\Wechat \\Server ' => 'wechat.server ' ], function ($ app ){
68+ return new WechatServer (config ('wechat.app_id ' ), config ('wechat.token ' ), config ('wechat.encoding_key ' ));
5869 });
5970
6071 foreach ($ this ->services as $ alias => $ service ) {
61- $ this ->app ->singleton ($ service , function ($ app ) use ($ service ){
62- return new $ service (config ('wechat.appId ' ), config ('wechat.secret ' ));
72+ $ this ->app ->singleton ([ $ service => $ alias ] , function ($ app ) use ($ service ){
73+ return new $ service (config ('wechat.app_id ' ), config ('wechat.secret ' ));
6374 });
64-
65- $ this ->app ->alias ($ service , $ alias );
6675 }
6776 }
6877
6978 /**
70- * 提供的服务名称列表
79+ * 提供的服务
7180 *
7281 * @return array
7382 */
7483 public function provides ()
7584 {
76- return array_keys ( $ this ->services ) + array_values ( $ this -> services ) ;
85+ return $ this ->services ;
7786 }
7887}
0 commit comments