From e12511f51e1c8e5815b0a36f95e07c2d4bd87853 Mon Sep 17 00:00:00 2001 From: Eric Zhu Date: Tue, 29 Dec 2020 05:26:55 +0800 Subject: [PATCH] added some methods --- README.md | 10 ++-------- src/DispatcherFactory.php | 11 +++++++++- src/Route.php | 42 ++++++++++++++++++++++++++++++++++++++- src/RouteCollector.php | 22 ++++++++++++++++++++ 4 files changed, 75 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ce34626..07f47bc 100644 --- a/README.md +++ b/README.md @@ -38,16 +38,10 @@ $route = Router::getRoute('users.comments.index'); #### 通过当前请求获取当前路由 ```php -use Hyperf\HttpServer\Contract\RequestInterface; -use Hyperf\HttpServer\Router\Dispatched; -use Hyperf\Utils\ApplicationContext; +use Hyperf\HttpServer\Router\Router; /** @var \HyperfExt\HttpServer\Router\Route $route */ -$route = ApplicationContext::getContainer() - ->get(RequestInterface::class) - ->getAttribute(Dispatched::class) - ->handler - ->routeInstance; // 为避免过多修改原始组件,该组件将路由实例放到了 Handler 中 +$route = Router::getCurrentRoute(); ``` ### 生成指定路由的 URI diff --git a/src/DispatcherFactory.php b/src/DispatcherFactory.php index 5ebf72d..216cbae 100644 --- a/src/DispatcherFactory.php +++ b/src/DispatcherFactory.php @@ -13,9 +13,18 @@ use FastRoute\DataGenerator\GroupCountBased as DataGenerator; use FastRoute\RouteParser\Std; use Hyperf\HttpServer\Router\DispatcherFactory as HyperfDispatcherFactory; +use Psr\Container\ContainerInterface; class DispatcherFactory extends HyperfDispatcherFactory { + protected ContainerInterface $container; + + public function __construct(ContainerInterface $container) + { + parent::__construct(); + $this->container = $container; + } + public function getRouter(string $serverName): RouteCollector { if (isset($this->routers[$serverName])) { @@ -24,6 +33,6 @@ public function getRouter(string $serverName): RouteCollector $parser = new Std(); $generator = new DataGenerator(); - return $this->routers[$serverName] = new RouteCollector($parser, $generator, $serverName); + return $this->routers[$serverName] = new RouteCollector($this->container, $parser, $generator, $serverName); } } diff --git a/src/Route.php b/src/Route.php index e1fa6fa..af2e588 100644 --- a/src/Route.php +++ b/src/Route.php @@ -25,11 +25,51 @@ class Route public ?string $name; - public function __construct(string $rule, array $data, ?string $name = null) + private array $defaults; + + public function __construct(string $rule, array $data, ?string $name = null, array $defaults = []) { $this->rule = $rule; $this->data = $data; $this->name = empty($name) ? null : $name; + $this->defaults = $defaults; + } + + public function getRule(): string + { + return $this->rule; + } + + public function getData(): array + { + return $this->data; + } + + public function getName(): ?string + { + return $this->name; + } + + public function setDefault(string $key, $value): self + { + $this->defaults[$key] = $value; + return $this; + } + + public function getDefault(string $key, $default = null) + { + return Arr::get($this->defaults, $key, $default); + } + + public function setDefaults(array $defaults): self + { + $this->defaults = $defaults; + return $this; + } + + public function getDefaults(): array + { + return $this->defaults; } public function createUri(array $parameters = []): Uri diff --git a/src/RouteCollector.php b/src/RouteCollector.php index c71d970..95cb691 100644 --- a/src/RouteCollector.php +++ b/src/RouteCollector.php @@ -10,11 +10,18 @@ */ namespace HyperfExt\HttpServer\Router; +use FastRoute\DataGenerator; +use FastRoute\RouteParser; use Hyperf\HttpServer\MiddlewareManager; +use Hyperf\HttpServer\Router\Dispatched; use Hyperf\HttpServer\Router\RouteCollector as BaseRouteCollector; +use Psr\Container\ContainerInterface; +use Psr\Http\Message\ServerRequestInterface; class RouteCollector extends BaseRouteCollector { + protected ContainerInterface $container; + protected string $currentGroupName = ''; /** @@ -22,6 +29,12 @@ class RouteCollector extends BaseRouteCollector */ protected array $namedRoutes = []; + public function __construct(ContainerInterface $container, RouteParser $routeParser, DataGenerator $dataGenerator, string $server = 'http') + { + parent::__construct($routeParser, $dataGenerator, $server); + $this->container = $container; + } + public function addRoute($httpMethod, string $route, $handler, array $options = []): Route { if (isset($options['name'])) { @@ -116,4 +129,13 @@ public function getRoute(string $name): Route throw new RouteNotFoundException("Route [{$name}] not defined."); } + + /** + * Get a route instance by current request. + */ + public function getCurrentRoute(): ?Route + { + $dispatched = $this->container->get(ServerRequestInterface::class)->getAttribute(Dispatched::class); + return $dispatched ? $dispatched->handler->routeInstance : null; + } }