Skip to content

Commit 70b6557

Browse files
authored
Merge pull request #114 from mingyoung/routes-for-lumen
Support Lumen routes.
2 parents 52131d1 + 37103c2 commit 70b6557

File tree

7 files changed

+153
-99
lines changed

7 files changed

+153
-99
lines changed

src/Controllers/OpenPlatformController.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
use Event;
66
use EasyWeChat\Foundation\Application;
7-
use Illuminate\Routing\Controller;
87
use Overtrue\LaravelWechat\Events\OpenPlatform as Events;
98

10-
class OpenPlatformController extends Controller
9+
class OpenPlatformController
1110
{
1211
/**
1312
* Events.
@@ -29,11 +28,7 @@ class OpenPlatformController extends Controller
2928
*/
3029
public function index(Application $application)
3130
{
32-
$server = $application->open_platform->server;
33-
34-
$server->setMessageHandler([$this, 'handle']);
35-
36-
return $server->serve();
31+
return $application->open_platform->server->setMessageHandler([$this, 'handle'])->serve();
3732
}
3833

3934
/**

src/Providers/RouteServiceProvider.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

src/Routing/Adapters/Adapter.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Overtrue\LaravelWechat\Routing\Adapters;
4+
5+
use Illuminate\Container\Container;
6+
7+
abstract class Adapter
8+
{
9+
/**
10+
* @var \Illuminate\Container\Container
11+
*/
12+
protected $app;
13+
14+
/**
15+
* Adapter constructor.
16+
*
17+
* @param \Illuminate\Container\Container $app
18+
*/
19+
public function __construct(Container $app)
20+
{
21+
$this->app = $app;
22+
}
23+
24+
abstract public function group(array $attributes, $callback);
25+
26+
abstract public function any($uri, $action);
27+
}

src/Routing/Adapters/Laravel.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Overtrue\LaravelWechat\Routing\Adapters;
4+
5+
class Laravel extends Adapter
6+
{
7+
public function group(array $attributes, $callback)
8+
{
9+
$this->app->router->group($attributes, $callback);
10+
}
11+
12+
public function any($uri, $action)
13+
{
14+
$this->app->router->any($uri, $action);
15+
}
16+
}

src/Routing/Adapters/Lumen.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Overtrue\LaravelWechat\Routing\Adapters;
4+
5+
class Lumen extends Adapter
6+
{
7+
public function group(array $attributes, $callback)
8+
{
9+
$this->app->group($attributes, $callback);
10+
}
11+
12+
public function any($uri, $action)
13+
{
14+
$verbs = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE'];
15+
16+
$this->app->addRoute($verbs, $uri, $action);
17+
}
18+
}

src/Routing/Router.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Overtrue\LaravelWechat\Routing;
4+
5+
use Illuminate\Container\Container;
6+
use Illuminate\Foundation\Application as LaravelApplication;
7+
use Laravel\Lumen\Application as LumenApplication;
8+
9+
class Router
10+
{
11+
/**
12+
* Routing adapter instance.
13+
*
14+
* @var \Overtrue\LaravelWechat\Routing\Adapters\Adapter
15+
*/
16+
protected $adapter;
17+
18+
/**
19+
* Create a new route registrar instance.
20+
*
21+
* @param \Illuminate\Container\Container $app
22+
*/
23+
public function __construct(Container $app)
24+
{
25+
if ($app instanceof LaravelApplication) {
26+
$this->adapter = new Adapters\Laravel($app);
27+
} elseif ($app instanceof LumenApplication) {
28+
$this->adapter = new Adapters\Lumen($app);
29+
}
30+
}
31+
32+
/**
33+
* @param string $method
34+
* @param array $arguments
35+
*/
36+
public function __call($method, $arguments)
37+
{
38+
call_user_func_array([$this->adapter, $method], $arguments);
39+
}
40+
}

src/ServiceProvider.php

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
namespace Overtrue\LaravelWechat;
44

5-
use EasyWeChat\Foundation\Application as EasyWeChatApplication;
5+
use EasyWeChat\Foundation\Application as EasyWeChat;
66
use Illuminate\Foundation\Application as LaravelApplication;
77
use Illuminate\Support\ServiceProvider as LaravelServiceProvider;
88
use Laravel\Lumen\Application as LumenApplication;
9-
use Overtrue\LaravelWechat\Providers\RouteServiceProvider;
9+
use Overtrue\LaravelWechat\Routing\Router;
1010
use Overtrue\Socialite\User as SocialiteUser;
1111

1212
class 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

Comments
 (0)