Added RouteCollector into container

This commit is contained in:
李铭昕 2018-12-29 09:54:54 +08:00
parent 6b8a44387f
commit 766433b658
10 changed files with 92 additions and 19 deletions

View File

@ -13,6 +13,8 @@ namespace Hyperf\GrpcServer;
use Hyperf\GrpcServer\Router\Dispatcher;
use Hyperf\GrpcServer\Router\DispatcherFactory;
use Hyperf\GrpcServer\Router\RouteCollector;
use Hyperf\HttpServer\Router\RouteCollectorFactory;
class ConfigProvider
{
@ -22,6 +24,7 @@ class ConfigProvider
'dependencies' => [
Server::class => ServerFactory::class,
Dispatcher::class => DispatcherFactory::class,
RouteCollector::class => RouteCollectorFactory::class,
],
'scan' => [
'paths' => [],

View File

@ -16,4 +16,6 @@ use Hyperf\HttpServer\Router\DispatcherFactory as HttpDispatcherFactory;
class DispatcherFactory extends HttpDispatcherFactory
{
protected $routes = [BASE_PATH . '/config/grpc_routes.php'];
protected $routeCollector = RouteCollector::class;
}

View File

@ -0,0 +1,16 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\GrpcServer\Router;
interface RouteCollector
{
}

View File

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\GrpcServer\Router;
use Hyperf\HttpServer\Router\Router as HttpServerRouter;
class Router extends HttpServerRouter
{
protected static $defautCollector = RouteCollector::class;
}

View File

@ -1,4 +1,13 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\HttpServer\Annotation;
@ -13,7 +22,6 @@ use ReflectionMethod;
*/
class AutoController extends AbstractAnnotation
{
/**
* @var string|null
*/
@ -36,5 +44,4 @@ class AutoController extends AbstractAnnotation
{
return $this->prefix;
}
}

View File

@ -12,9 +12,11 @@ declare(strict_types=1);
namespace Hyperf\HttpServer;
use FastRoute\Dispatcher;
use FastRoute\RouteCollector;
use Hyperf\HttpServer\Command\StartServer;
use Hyperf\HttpServer\Command\StartServerFactory;
use Hyperf\HttpServer\Router\DispatcherFactory;
use Hyperf\HttpServer\Router\RouteCollectorFactory;
class ConfigProvider
{
@ -25,6 +27,7 @@ class ConfigProvider
Server::class => ServerFactory::class,
StartServer::class => StartServerFactory::class,
Dispatcher::class => DispatcherFactory::class,
RouteCollector::class => RouteCollectorFactory::class,
],
'commands' => [
StartServer::class,

View File

@ -11,24 +11,20 @@ declare(strict_types=1);
namespace Hyperf\HttpServer\Router;
use FastRoute\DataGenerator\GroupCountBased as DataGenerator;
use FastRoute\Dispatcher;
use FastRoute\Dispatcher\GroupCountBased;
use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std;
use Psr\Container\ContainerInterface;
class DispatcherFactory
{
protected $routes = [BASE_PATH . '/config/routes.php'];
protected $routeCollector = RouteCollector::class;
public function __invoke(ContainerInterface $container): Dispatcher
{
$parser = new Std();
$generator = new DataGenerator();
/** @var RouteCollector $routeCollector */
$router = new RouteCollector($parser, $generator);
Router::init($router);
$router = $container->get($this->routeCollector);
foreach ($this->routes as $route) {
require_once $route;

View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://hyperf.org
* @document https://wiki.hyperf.org
* @contact group@hyperf.org
* @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
*/
namespace Hyperf\HttpServer\Router;
use FastRoute\DataGenerator\GroupCountBased as DataGenerator;
use FastRoute\RouteCollector;
use FastRoute\RouteParser\Std;
class RouteCollectorFactory
{
public function __invoke()
{
$parser = new Std();
$generator = new DataGenerator();
/** @var RouteCollector $routeCollector */
return new RouteCollector($parser, $generator);
}
}

View File

@ -12,6 +12,8 @@ declare(strict_types=1);
namespace Hyperf\HttpServer\Router;
use FastRoute\RouteCollector;
use Hyperf\Framework\ApplicationContext;
use Psr\Container\ContainerInterface;
/**
* Class Router
@ -27,15 +29,13 @@ use FastRoute\RouteCollector;
*/
class Router
{
protected static $router;
protected static $defautCollector = RouteCollector::class;
public static function __callStatic($name, $arguments)
{
return static::$router->$name(...$arguments);
}
public static function init(RouteCollector $routeCollector)
{
static::$router = $routeCollector;
/** @var ContainerInterface $container */
$container = ApplicationContext::getContainer();
$router = $container->get(static::$defautCollector);
return $router->$name(...$arguments);
}
}

View File

@ -19,10 +19,10 @@ use Hyperf\Utils\Context;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Swoft\Http\Message\Server\Request as Psr7Request;
use Swoft\Http\Message\Server\Response as Psr7Response;
use Swoole\Http\Request as SwooleRequest;
use Swoole\Http\Response as SwooleResponse;
use Throwable;
class Server