3
3
namespace Overtrue \LaravelWechat ;
4
4
5
5
use Illuminate \Support \ServiceProvider as LaravelServiceProvider ;
6
+ use Overtrue \Wechat \Server as WechatServer ;
6
7
use Overtrue \Wechat \Alias ;
7
8
8
9
class ServiceProvider extends LaravelServiceProvider
9
10
{
11
+ /**
12
+ * 延迟加载
13
+ *
14
+ * @var boolean
15
+ */
10
16
protected $ defer = true ;
11
17
12
18
/**
@@ -30,6 +36,7 @@ class ServiceProvider extends LaravelServiceProvider
30
36
'wechat.image ' => 'Overtrue \\Wechat \\Image ' ,
31
37
];
32
38
39
+
33
40
/**
34
41
* Boot the provider.
35
42
*
@@ -53,26 +60,28 @@ public function register()
53
60
Alias::register ();
54
61
}
55
62
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 ' ));
58
69
});
59
70
60
71
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 ' ));
63
74
});
64
-
65
- $ this ->app ->alias ($ service , $ alias );
66
75
}
67
76
}
68
77
69
78
/**
70
- * 提供的服务名称列表
79
+ * 提供的服务
71
80
*
72
81
* @return array
73
82
*/
74
83
public function provides ()
75
84
{
76
- return array_keys ( $ this ->services ) + array_values ( $ this -> services ) ;
85
+ return $ this ->services ;
77
86
}
78
87
}
0 commit comments