diff --git a/src/Controllers/OpenPlatformController.php b/src/Controllers/OpenPlatformController.php index c5553ef..d43498e 100644 --- a/src/Controllers/OpenPlatformController.php +++ b/src/Controllers/OpenPlatformController.php @@ -4,10 +4,9 @@ use Event; use EasyWeChat\Foundation\Application; -use Illuminate\Routing\Controller; use Overtrue\LaravelWechat\Events\OpenPlatform as Events; -class OpenPlatformController extends Controller +class OpenPlatformController { /** * Events. @@ -29,11 +28,7 @@ class OpenPlatformController extends Controller */ public function index(Application $application) { - $server = $application->open_platform->server; - - $server->setMessageHandler([$this, 'handle']); - - return $server->serve(); + return $application->open_platform->server->setMessageHandler([$this, 'handle'])->serve(); } /** diff --git a/src/Providers/RouteServiceProvider.php b/src/Providers/RouteServiceProvider.php deleted file mode 100644 index 5cbbade..0000000 --- a/src/Providers/RouteServiceProvider.php +++ /dev/null @@ -1,82 +0,0 @@ -config('attributes', []), [ - 'namespace' => 'Overtrue\\LaravelWechat\\Controllers', - ]); - } - - /** - * Check if routes is enabled. - * - * @return bool - */ - public function isEnabled() - { - return $this->config('enabled', false); - } - - /** - * Define the routes. - */ - public function map() - { - if ($this->isEnabled()) { - $this->group($this->routeAttributes(), function () { - $this->mapOpenPlatformRoutes(); - }); - } - } - - /** - * Map open platform routes. - */ - public function mapOpenPlatformRoutes() - { - $this->match(['GET','POST'], $this->config('open_platform_serve_url'), 'OpenPlatformController@index')->name('open-platform'); - } - - /** - * Get config value by key. - * - * @param string $key - * @param mixed|null $default - * - * @return mixed - */ - private function config($key, $default = null) - { - /** @var \Illuminate\Config\Repository $config */ - $config = $this->app->make('config'); - - return $config->get("wechat.route.$key", $default); - } - - /** - * Call the router method. - * - * @param string $name - * @param array $args - * - * @return mixed - */ - public function __call($name, $args) - { - return $this->app->make('router')->$name(...$args); - } -} diff --git a/src/Routing/Adapters/Adapter.php b/src/Routing/Adapters/Adapter.php new file mode 100644 index 0000000..acd0528 --- /dev/null +++ b/src/Routing/Adapters/Adapter.php @@ -0,0 +1,27 @@ +app = $app; + } + + abstract public function group(array $attributes, $callback); + + abstract public function any($uri, $action); +} diff --git a/src/Routing/Adapters/Laravel.php b/src/Routing/Adapters/Laravel.php new file mode 100644 index 0000000..a74d276 --- /dev/null +++ b/src/Routing/Adapters/Laravel.php @@ -0,0 +1,16 @@ +app->router->group($attributes, $callback); + } + + public function any($uri, $action) + { + $this->app->router->any($uri, $action); + } +} diff --git a/src/Routing/Adapters/Lumen.php b/src/Routing/Adapters/Lumen.php new file mode 100644 index 0000000..68c2feb --- /dev/null +++ b/src/Routing/Adapters/Lumen.php @@ -0,0 +1,18 @@ +app->group($attributes, $callback); + } + + public function any($uri, $action) + { + $verbs = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE']; + + $this->app->addRoute($verbs, $uri, $action); + } +} diff --git a/src/Routing/Router.php b/src/Routing/Router.php new file mode 100644 index 0000000..2c24178 --- /dev/null +++ b/src/Routing/Router.php @@ -0,0 +1,40 @@ +adapter = new Adapters\Laravel($app); + } elseif ($app instanceof LumenApplication) { + $this->adapter = new Adapters\Lumen($app); + } + } + + /** + * @param string $method + * @param array $arguments + */ + public function __call($method, $arguments) + { + call_user_func_array([$this->adapter, $method], $arguments); + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 49e7bcf..3299bb1 100755 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -2,11 +2,11 @@ namespace Overtrue\LaravelWechat; -use EasyWeChat\Foundation\Application as EasyWeChatApplication; +use EasyWeChat\Foundation\Application as EasyWeChat; use Illuminate\Foundation\Application as LaravelApplication; use Illuminate\Support\ServiceProvider as LaravelServiceProvider; use Laravel\Lumen\Application as LumenApplication; -use Overtrue\LaravelWechat\Providers\RouteServiceProvider; +use Overtrue\LaravelWechat\Routing\Router; use Overtrue\Socialite\User as SocialiteUser; class ServiceProvider extends LaravelServiceProvider @@ -20,7 +20,9 @@ public function boot() { $this->setupConfig(); - $this->app->register(RouteServiceProvider::class); + if ($this->config('route.enabled')) { + $this->registerRoutes(); + } } /** @@ -55,18 +57,18 @@ protected function setupConfig() */ public function register() { - $this->app->singleton(EasyWeChatApplication::class, function ($laravelApp) { - $app = new EasyWeChatApplication(config('wechat')); + $this->app->singleton(EasyWeChat::class, function ($app) { + $easywechat = new EasyWeChat(config('wechat')); if (config('wechat.use_laravel_cache')) { - $app->cache = new CacheBridge(); + $easywechat->cache = new CacheBridge(); } - $app->server->setRequest($laravelApp['request']); + $easywechat->server->setRequest($app['request']); - return $app; + return $easywechat; }); - $this->app->alias(EasyWeChatApplication::class, 'wechat'); - $this->app->alias(EasyWeChatApplication::class, 'easywechat'); + $this->app->alias(EasyWeChat::class, 'wechat'); + $this->app->alias(EasyWeChat::class, 'easywechat'); } /** @@ -89,4 +91,42 @@ protected function setUpMockAuthUser() session(['wechat.oauth_user' => $user]); } } + + /** + * Register routes. + */ + protected function registerRoutes() + { + $router = new Router($this->app); + + $router->group($this->routeAttributes(), function () use ($router) { + $router->any($this->config('route.open_platform_serve_url'), 'OpenPlatformController@index'); + }); + } + + + /** + * Get Route attributes. + * + * @return array + */ + public function routeAttributes() + { + return array_merge($this->config('route.attributes', []), [ + 'namespace' => '\\Overtrue\\LaravelWechat\\Controllers', + ]); + } + + /** + * Get config value by key. + * + * @param string $key + * @param mixed|null $default + * + * @return mixed + */ + private function config($key, $default = null) + { + return $this->app->make('config')->get("wechat.{$key}", $default); + } }