Skip to content

Commit

Permalink
Fixed minor issues in the router class
Browse files Browse the repository at this point in the history
- Added missing UrlGeneratorInterface to the router class.
- Updated the getCollection method to no longer throw an exception
- Improved performance of the addRoute method in the router's class
  • Loading branch information
divineniiquaye committed May 11, 2022
1 parent 7c5b4e1 commit 95d8177
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
14 changes: 5 additions & 9 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
* @author Divine Niiquaye Ibok <[email protected]>
*/
class Router implements RouteMatcherInterface, RequestMethodInterface, MiddlewareInterface
class Router implements RouteMatcherInterface, RequestMethodInterface, MiddlewareInterface, UrlGeneratorInterface
{
/**
* Standard HTTP methods for browser requests.
Expand Down Expand Up @@ -90,9 +90,7 @@ public static function withCollection(RouteCollection $collection = null, RouteC
*/
public function addRoute(Route ...$routes): void
{
if ($this->collection instanceof RouteCollection) {
$this->collection->routes($routes);
}
$this->getCollection()->routes($routes, false);
}

/**
Expand All @@ -118,11 +116,9 @@ public function generateUri(string $routeName, array $parameters = [], int $refe
{
$matcher = $this->getMatcher();

//@codeCoverageIgnoreStart
if (!$matcher instanceof UrlGeneratorInterface) {
throw new UrlGenerationException(\sprintf('The route matcher does not support using the %s implementation', UrlGeneratorInterface::class));
}
//@codeCoverageIgnoreEnd

return $matcher->generateUri($routeName, $parameters, $referenceType);
}
Expand Down Expand Up @@ -166,11 +162,11 @@ public function getCollection(): RouteCollection
{
if (\is_callable($collection = $this->collection)) {
$collection($collection = new RouteCollection());
} elseif (null === $collection) {
throw new \RuntimeException(\sprintf('Did you forget to set add the route collection with the "%s".', __CLASS__ . '::setCollection'));
} elseif (null !== $collection) {
return $this->collection;
}

return $this->collection = $collection;
return $this->collection = $collection ?? new RouteCollection();
}

/**
Expand Down
12 changes: 7 additions & 5 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
use Flight\Routing\Generator\GeneratedUri;
use Flight\Routing\Handlers\ResourceHandler;
use Flight\Routing\Handlers\RouteHandler;
use Flight\Routing\Interfaces\RouteMatcherInterface;
use Flight\Routing\Interfaces\UrlGeneratorInterface;
use Flight\Routing\Route;
use Flight\Routing\RouteCollection;
use Flight\Routing\RouteMatcher;
use Flight\Routing\Router;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
Expand All @@ -49,11 +52,10 @@ public function testConstructor(): void
{
$router = new Router();
$this->assertInstanceOf(MiddlewareInterface::class, $router);
$this->assertInstanceOf(UrlGeneratorInterface::class, $router);
$this->assertInstanceOf(RouteMatcherInterface::class, $router);

$this->expectExceptionMessage('Did you forget to set add the route collection with the "Flight\Routing\Router::setCollection".');
$this->expectException(\RuntimeException::class);

$router->getMatcher();
$this->assertInstanceOf(RouteMatcher::class, $router->getMatcher());
}

public function testAddRoute(): void
Expand Down Expand Up @@ -419,7 +421,7 @@ public function testHandleRouteHandlerAsResponse(): void
$route = new Route('/foo');
$route->run(new Response(200, ['Response' => 'Controller']));

$router = Router::withCollection();
$router = new Router();
$router->addRoute($route);
$response = $router->process(new ServerRequest($route->getMethods()[0], $route->getPath()), new RouteHandler(new Psr17Factory()));

Expand Down

0 comments on commit 95d8177

Please sign in to comment.