Skip to content

Commit

Permalink
Merge pull request #114 from mingyoung/routes-for-lumen
Browse files Browse the repository at this point in the history
Support Lumen routes.
  • Loading branch information
overtrue authored Jul 3, 2017
2 parents 52131d1 + 37103c2 commit 70b6557
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 99 deletions.
9 changes: 2 additions & 7 deletions src/Controllers/OpenPlatformController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
}

/**
Expand Down
82 changes: 0 additions & 82 deletions src/Providers/RouteServiceProvider.php

This file was deleted.

27 changes: 27 additions & 0 deletions src/Routing/Adapters/Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Overtrue\LaravelWechat\Routing\Adapters;

use Illuminate\Container\Container;

abstract class Adapter
{
/**
* @var \Illuminate\Container\Container
*/
protected $app;

/**
* Adapter constructor.
*
* @param \Illuminate\Container\Container $app
*/
public function __construct(Container $app)
{
$this->app = $app;
}

abstract public function group(array $attributes, $callback);

abstract public function any($uri, $action);
}
16 changes: 16 additions & 0 deletions src/Routing/Adapters/Laravel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Overtrue\LaravelWechat\Routing\Adapters;

class Laravel extends Adapter
{
public function group(array $attributes, $callback)
{
$this->app->router->group($attributes, $callback);
}

public function any($uri, $action)
{
$this->app->router->any($uri, $action);
}
}
18 changes: 18 additions & 0 deletions src/Routing/Adapters/Lumen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Overtrue\LaravelWechat\Routing\Adapters;

class Lumen extends Adapter
{
public function group(array $attributes, $callback)
{
$this->app->group($attributes, $callback);
}

public function any($uri, $action)
{
$verbs = ['GET', 'HEAD', 'POST', 'PUT', 'PATCH', 'DELETE'];

$this->app->addRoute($verbs, $uri, $action);
}
}
40 changes: 40 additions & 0 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Overtrue\LaravelWechat\Routing;

use Illuminate\Container\Container;
use Illuminate\Foundation\Application as LaravelApplication;
use Laravel\Lumen\Application as LumenApplication;

class Router
{
/**
* Routing adapter instance.
*
* @var \Overtrue\LaravelWechat\Routing\Adapters\Adapter
*/
protected $adapter;

/**
* Create a new route registrar instance.
*
* @param \Illuminate\Container\Container $app
*/
public function __construct(Container $app)
{
if ($app instanceof LaravelApplication) {
$this->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);
}
}
60 changes: 50 additions & 10 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +20,9 @@ public function boot()
{
$this->setupConfig();

$this->app->register(RouteServiceProvider::class);
if ($this->config('route.enabled')) {
$this->registerRoutes();
}
}

/**
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -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);
}
}

0 comments on commit 70b6557

Please sign in to comment.