diff --git a/CHANGELOG.md b/CHANGELOG.md index ff59fc3b..6ecb7c61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,7 +27,7 @@ All notable changes to `Slytherin` will be documented in this file. - Backward compatibility for `LeagueContainer::set` (as of `~3.0`) - Backward compatibility for `TwigRenderer::render` (as of `~3.0`) - Backward compatibility for `StratigilityDispatcher::process` (until `~3.0`) -- Resolving type hinted routes for third-party routers +- Resolving type hinted routes for third-party routing dispatchers ### Removed - `Integration\ConfigurationInterface` as its not being used diff --git a/src/Middleware/StratigilityDispatcher.php b/src/Middleware/StratigilityDispatcher.php index 910e0e3b..69bd730d 100644 --- a/src/Middleware/StratigilityDispatcher.php +++ b/src/Middleware/StratigilityDispatcher.php @@ -77,10 +77,12 @@ public function process(ServerRequestInterface $request, HandlerInterface $handl { $item = $this->setMiddleware($item); + // @codeCoverageIgnoreStart if (! $this->hasFactory() && $this->hasPsr()) { $item = $this->setPsrMiddleware($item); } + // @codeCoverageIgnoreEnd $this->zend->pipe($item); } @@ -93,11 +95,13 @@ public function process(ServerRequestInterface $request, HandlerInterface $handl $zend = $this->zend; + // @codeCoverageIgnoreStart if ($this->hasPsr()) { /** @phpstan-ignore-next-line */ return $zend->process($request, $next); } + // @codeCoverageIgnoreEnd /** @phpstan-ignore-next-line */ return $zend($request, $response, $next); @@ -135,6 +139,7 @@ protected function setFactory(ResponseInterface $response) */ protected function setMiddleware(MiddlewareInterface $item) { + // @codeCoverageIgnoreStart if ($this->hasPsr()) { return function ($request, $handler) use ($item) @@ -142,6 +147,7 @@ protected function setMiddleware(MiddlewareInterface $item) return $item->process($request, new Interop($handler)); }; } + // @codeCoverageIgnoreEnd return function ($request, $response, $next) use ($item) { diff --git a/src/Routing/FastRouteDispatcher.php b/src/Routing/FastRouteDispatcher.php index d65d590d..401069c9 100644 --- a/src/Routing/FastRouteDispatcher.php +++ b/src/Routing/FastRouteDispatcher.php @@ -47,7 +47,7 @@ public function dispatch($method, $uri) /** @var \Rougin\Slytherin\Routing\RouteInterface */ $route = $result[1]; - /** @var string[] */ + /** @var array */ $params = $result[2]; return $route->setParams($params); diff --git a/src/Routing/FastRouteRouter.php b/src/Routing/FastRouteRouter.php index 1f2496a5..76e51791 100644 --- a/src/Routing/FastRouteRouter.php +++ b/src/Routing/FastRouteRouter.php @@ -57,7 +57,21 @@ public function parsed(array $routes = array()) { foreach ($routes as $route) { - $collector->addRoute($route->getMethod(), $route->getUri(), $route); + // Convert the ":name" pattern into "{name}" pattern ------------------ + $uri = $route->getUri(); + + $matched = preg_match_all('/\:([a-zA-Z0-9\_\-]+)/i', $uri, $matches); + + if ($matched) + { + foreach ($matches[0] as $key => $item) + { + $uri = str_replace($item, '{' . $matches[1][$key] . '}', $uri); + } + } + // -------------------------------------------------------------------- + + $collector->addRoute($route->getMethod(), $uri, $route); } }; diff --git a/src/Routing/PhrouteDispatcher.php b/src/Routing/PhrouteDispatcher.php index ce9d8923..9b395c1e 100644 --- a/src/Routing/PhrouteDispatcher.php +++ b/src/Routing/PhrouteDispatcher.php @@ -73,7 +73,27 @@ public function dispatch($method, $uri) try { /** @var \Rougin\Slytherin\Routing\RouteInterface */ - return $phroute->dispatch($method, $uri); + $route = $phroute->dispatch($method, $uri); + + // Combine values from resolver and the current parameters ------------- + $regex = '/\{([a-zA-Z0-9\_\-]+)\}/i'; + + $matched = preg_match_all($regex, $route->getUri(), $matches); + + // If "{name}" pattern is not found, try the ":name" pattern instead --- + if (! $matched) + { + $regex = '/\:([a-zA-Z0-9\_\-]+)/i'; + + $matched = preg_match_all($regex, $route->getUri(), $matches); + } + // --------------------------------------------------------------------- + + /** @var array */ + $params = array_combine($matches[1], $route->getParams()); + + return $route->setParams($params); + // --------------------------------------------------------------------- } catch (HttpRouteNotFoundException $e) { diff --git a/src/Routing/PhrouteRouter.php b/src/Routing/PhrouteRouter.php index b39ef9de..5ef442be 100644 --- a/src/Routing/PhrouteRouter.php +++ b/src/Routing/PhrouteRouter.php @@ -63,7 +63,21 @@ public function asParsed(array $routes) { foreach ($routes as $route) { - $this->collector->addRoute($route->getMethod(), $route->getUri(), $route); + // Convert the ":name" pattern into "{name}" pattern ------------------ + $uri = $route->getUri(); + + $matched = preg_match_all('/\:([a-zA-Z0-9\_\-]+)/i', $uri, $matches); + + if ($matched) + { + foreach ($matches[0] as $key => $item) + { + $uri = str_replace($item, '{' . $matches[1][$key] . '}', $uri); + } + } + // -------------------------------------------------------------------- + + $this->collector->addRoute($route->getMethod(), $uri, $route); } return $this->collector->getData(); diff --git a/src/Routing/Route.php b/src/Routing/Route.php index 59dc31bb..95d74dc7 100644 --- a/src/Routing/Route.php +++ b/src/Routing/Route.php @@ -28,7 +28,7 @@ class Route implements RouteInterface protected $middlewares; /** - * @var string[] + * @var array */ protected $params = array(); @@ -98,7 +98,7 @@ public function getMiddlewares() /** * Returns the defined parameters. * - * @return string[] + * @return array */ public function getParams() { @@ -143,7 +143,9 @@ public function getUri() } /** - * @param string[] $params + * Sets the parameters to the route. + * + * @param array $params * @return self */ public function setParams($params) diff --git a/src/System.php b/src/System.php index 28f197ba..b0f01c07 100644 --- a/src/System.php +++ b/src/System.php @@ -72,20 +72,6 @@ public function __construct($container = null, Configuration $config = null) } } - /** - * Finds an entry of the container by its identifier and returns it. - * - * @param string $id - * @return mixed - * - * @throws \Psr\Container\NotFoundExceptionInterface - * @throws \Psr\Container\ContainerExceptionInterface - */ - public function get($id) - { - return $this->container->get($id); - } - /** * Handles the ServerRequestInterface to convert it to a ResponseInterface. * diff --git a/tests/Application/ApplicationTestCases.php b/tests/Application/ApplicationTestCases.php index 4c35f651..c0fad39e 100644 --- a/tests/Application/ApplicationTestCases.php +++ b/tests/Application/ApplicationTestCases.php @@ -200,17 +200,6 @@ public function testHandleMethodWithServerRequest() */ public function testHandleMethodWithTypehintedParameter() { - $interface = 'Rougin\Slytherin\Routing\DispatcherInterface'; - - $dispatcher = $this->application->get($interface); - - // TODO: Implement resolving of type hinted parameters from container to PhrouteResolver ---------- - if (is_a($dispatcher, 'Rougin\Slytherin\Routing\PhrouteDispatcher')) - { - $this->markTestSkipped('Resolving type hinted parameters are not yet implemented in Phroute.'); - } - // ------------------------------------------------------------------------------------------------ - $request = $this->request('GET', '/typehint/202'); $expected = (integer) 202; diff --git a/tests/Routing/FastRouteDispatcherTest.php b/tests/Routing/FastRouteDispatcherTest.php index 81362d9f..fd197b8d 100644 --- a/tests/Routing/FastRouteDispatcherTest.php +++ b/tests/Routing/FastRouteDispatcherTest.php @@ -69,13 +69,13 @@ public function testDispatchMethodWithClassAndPhrouteRouter() $router->prefix('', 'Rougin\Slytherin\Fixture\Classes'); - $router->get('/', 'NewClass@index'); + $router->get('/hello/:name', 'NewClass@index'); $dispatcher = new FastRouteDispatcher($router); $controller = new NewClass; - $route = $dispatcher->dispatch('GET', '/'); + $route = $dispatcher->dispatch('GET', '/hello/Slytherin'); $expected = (string) $controller->index(); diff --git a/tests/Sample/Router.php b/tests/Sample/Router.php index 2c7cb123..6a9c6273 100644 --- a/tests/Sample/Router.php +++ b/tests/Sample/Router.php @@ -22,7 +22,7 @@ public function routes($parsed = false) $this->get('/response', 'Hello@response'); - $this->get('/hi/{name}', 'Hello@name'); + $this->get('/hi/:name', 'Hello@name'); $this->get('without-slash', 'Hello@string'); @@ -49,7 +49,7 @@ public function routes($parsed = false) return 'Welcome ' . $name . ', ' . $age . '!'; }); - $this->get('/call/{name}', function ($name) + $this->get('/call/:name', function ($name) { return 'Welcome ' . $name . '!'; });