22
33namespace Overtrue \LaravelWechat ;
44
5- use EasyWeChat \Foundation \Application as EasyWeChatApplication ;
5+ use EasyWeChat \Foundation \Application as EasyWeChat ;
66use Illuminate \Foundation \Application as LaravelApplication ;
77use Illuminate \Support \ServiceProvider as LaravelServiceProvider ;
88use Laravel \Lumen \Application as LumenApplication ;
9- use Overtrue \LaravelWechat \Providers \ RouteServiceProvider ;
9+ use Overtrue \LaravelWechat \Routing \ Router ;
1010use Overtrue \Socialite \User as SocialiteUser ;
1111
1212class ServiceProvider extends LaravelServiceProvider
@@ -20,7 +20,9 @@ public function boot()
2020 {
2121 $ this ->setupConfig ();
2222
23- $ this ->app ->register (RouteServiceProvider::class);
23+ if ($ this ->config ('route.enabled ' )) {
24+ $ this ->registerRoutes ();
25+ }
2426 }
2527
2628 /**
@@ -55,18 +57,18 @@ protected function setupConfig()
5557 */
5658 public function register ()
5759 {
58- $ this ->app ->singleton (EasyWeChatApplication ::class, function ($ laravelApp ) {
59- $ app = new EasyWeChatApplication (config ('wechat ' ));
60+ $ this ->app ->singleton (EasyWeChat ::class, function ($ app ) {
61+ $ easywechat = new EasyWeChat (config ('wechat ' ));
6062 if (config ('wechat.use_laravel_cache ' )) {
61- $ app ->cache = new CacheBridge ();
63+ $ easywechat ->cache = new CacheBridge ();
6264 }
63- $ app ->server ->setRequest ($ laravelApp ['request ' ]);
65+ $ easywechat ->server ->setRequest ($ app ['request ' ]);
6466
65- return $ app ;
67+ return $ easywechat ;
6668 });
6769
68- $ this ->app ->alias (EasyWeChatApplication ::class, 'wechat ' );
69- $ this ->app ->alias (EasyWeChatApplication ::class, 'easywechat ' );
70+ $ this ->app ->alias (EasyWeChat ::class, 'wechat ' );
71+ $ this ->app ->alias (EasyWeChat ::class, 'easywechat ' );
7072 }
7173
7274 /**
@@ -89,4 +91,42 @@ protected function setUpMockAuthUser()
8991 session (['wechat.oauth_user ' => $ user ]);
9092 }
9193 }
94+
95+ /**
96+ * Register routes.
97+ */
98+ protected function registerRoutes ()
99+ {
100+ $ router = new Router ($ this ->app );
101+
102+ $ router ->group ($ this ->routeAttributes (), function () use ($ router ) {
103+ $ router ->any ($ this ->config ('route.open_platform_serve_url ' ), 'OpenPlatformController@index ' );
104+ });
105+ }
106+
107+
108+ /**
109+ * Get Route attributes.
110+ *
111+ * @return array
112+ */
113+ public function routeAttributes ()
114+ {
115+ return array_merge ($ this ->config ('route.attributes ' , []), [
116+ 'namespace ' => '\\Overtrue \\LaravelWechat \\Controllers ' ,
117+ ]);
118+ }
119+
120+ /**
121+ * Get config value by key.
122+ *
123+ * @param string $key
124+ * @param mixed|null $default
125+ *
126+ * @return mixed
127+ */
128+ private function config ($ key , $ default = null )
129+ {
130+ return $ this ->app ->make ('config ' )->get ("wechat. {$ key }" , $ default );
131+ }
92132}
0 commit comments