Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.x - Split RouterInterface into RouteCollectorInterface and RouteResolverInterface #2622

Merged
merged 21 commits into from
Apr 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
ddd7fcc
add RouteCollectorInterface and RouteResolverInterface
l0gicgate Apr 2, 2019
45559e6
move all routing components to new Slim\Routing aggregate folder
l0gicgate Apr 2, 2019
ea0351a
add implementations of new RouteCollectorInterface and RouteResolver
l0gicgate Apr 2, 2019
45a87c9
refactor RoutingMiddleware
l0gicgate Apr 2, 2019
34436e8
remove RouterInterface
l0gicgate Apr 2, 2019
fb928c7
reorder methods in RouteCollectorInterface and RouteCollector
l0gicgate Apr 3, 2019
f3b1410
fix comment for RouteCollectorInterface::setCacheFile method
l0gicgate Apr 8, 2019
f44c140
add class comment for RouteCollector and RouteResolver
l0gicgate Apr 8, 2019
086ddd2
add getIdentifier() method to RouteInterface
l0gicgate Apr 9, 2019
b810b1e
make parameter required for RouteCollectorInterface::setCacheFile()
l0gicgate Apr 10, 2019
38b99f5
add new line at end of RouteResolverInterface file
l0gicgate Apr 10, 2019
37fdae7
fix constructor variable declaration logic
l0gicgate Apr 10, 2019
25c64b1
move is_readable and is_writable php function overrides into proper n…
l0gicgate Apr 10, 2019
a623ec6
refactor all tests for proposed changes
l0gicgate Apr 10, 2019
a8306a3
fix phpstan errors
l0gicgate Apr 10, 2019
978a90e
add missing methods to RouteInterface and re-order methods in Route i…
l0gicgate Apr 10, 2019
f317236
fix RouteCollectorInterface::getBasePath() method and add test covera…
l0gicgate Apr 10, 2019
c379b6e
clean up RouteCollector::relativePathFor()
l0gicgate Apr 10, 2019
26b1a83
fix declaration statement order in App constructor
l0gicgate Apr 17, 2019
10c7351
remove double asterisks from comment blocks in RouteCollector
l0gicgate Apr 17, 2019
fd90436
add comment for getCacheFile() on RouteCollectorInterface
l0gicgate Apr 17, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 44 additions & 19 deletions Slim/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Slim;

use Closure;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -17,9 +18,13 @@
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Interfaces\CallableResolverInterface;
use Slim\Interfaces\RouteCollectorInterface;
use Slim\Interfaces\RouteGroupInterface;
use Slim\Interfaces\RouteInterface;
use Slim\Interfaces\RouterInterface;
use Slim\Interfaces\RouteResolverInterface;
use Slim\Routing\RouteCollector;
use Slim\Routing\RouteResolver;
use Slim\Routing\RouteRunner;

/**
* App
Expand Down Expand Up @@ -55,9 +60,14 @@ class App implements RequestHandlerInterface
protected $middlewareDispatcher;

/**
* @var RouterInterface
* @var RouteCollectorInterface
*/
protected $router;
protected $routeCollector;

/**
* @var RouteResolverInterface
*/
protected $routeResolver;

/**
* @var ResponseFactoryInterface
Expand All @@ -74,19 +84,28 @@ class App implements RequestHandlerInterface
* @param ResponseFactoryInterface $responseFactory
* @param ContainerInterface|null $container
* @param CallableResolverInterface $callableResolver
* @param RouterInterface $router
* @param RouteCollectorInterface $routeCollector
* @param RouteResolverInterface $routeResolver
*/
public function __construct(
ResponseFactoryInterface $responseFactory,
ContainerInterface $container = null,
CallableResolverInterface $callableResolver = null,
RouterInterface $router = null
RouteCollectorInterface $routeCollector = null,
RouteResolverInterface $routeResolver = null
) {
$this->responseFactory = $responseFactory;
$this->container = $container;
$this->callableResolver = $callableResolver ?? new CallableResolver($container);
$this->router = $router ?? new Router($responseFactory, $this->callableResolver, $this->container);
$this->middlewareDispatcher = new MiddlewareDispatcher(new RouteDispatcher($this->router), $container);
$this->routeCollector = $routeCollector ?? new RouteCollector(
$responseFactory,
$this->callableResolver,
$container
);
$this->routeResolver = $routeResolver ?? new RouteResolver($this->routeCollector);

$routeRunner = new RouteRunner($this->routeResolver);
$this->middlewareDispatcher = new MiddlewareDispatcher($routeRunner, $container);
}

/**
Expand Down Expand Up @@ -134,13 +153,23 @@ public function getCallableResolver(): CallableResolverInterface
}

/**
* Get router
* Get route collector
*
* @return RouteCollectorInterface
*/
public function getRouteCollector(): RouteCollectorInterface
{
return $this->routeCollector;
}

/**
* Get route resolver
*
* @return RouterInterface
* @return RouteResolverInterface
*/
public function getRouter(): RouterInterface
public function getRouteResolver(): RouteResolverInterface
{
return $this->router;
return $this->routeResolver;
}

/********************************************************************************
Expand Down Expand Up @@ -250,15 +279,11 @@ public function any(string $pattern, $callable): RouteInterface
public function map(array $methods, string $pattern, $callable): RouteInterface
{
// Bind route callable to container, if present
if ($this->container instanceof ContainerInterface && $callable instanceof \Closure) {
if ($this->container instanceof ContainerInterface && $callable instanceof Closure) {
$callable = $callable->bindTo($this->container);
}

/** @var Router $router */
$router = $this->getRouter();
$route = $router->map($methods, $pattern, $callable);

return $route;
return $this->routeCollector->map($methods, $pattern, $callable);
}

/**
Expand Down Expand Up @@ -294,9 +319,9 @@ public function redirect(string $from, $to, int $status = 302): RouteInterface
*/
public function group(string $pattern, $callable): RouteGroupInterface
{
$group = $this->router->pushGroup($pattern, $callable);
$group = $this->routeCollector->pushGroup($pattern, $callable);
$group($this);
$this->router->popGroup();
$this->routeCollector->popGroup();
return $group;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,56 +10,64 @@
namespace Slim\Interfaces;

use InvalidArgumentException;
use Psr\Http\Message\ServerRequestInterface;
use RuntimeException;
use Slim\RoutingResults;

/**
* Router Interface
*
* @package Slim
* @since 3.0.0
*/
interface RouterInterface
interface RouteCollectorInterface
{
/**
* Add route
* Get default route invocation strategy
*
* @param string[] $methods Array of HTTP methods
* @param string $pattern The route pattern
* @param callable $handler The route callable
* @return InvocationStrategyInterface
*/
public function getDefaultInvocationStrategy(): InvocationStrategyInterface;

/**
* Set default route invocation strategy
*
* @return RouteInterface
* @param InvocationStrategyInterface $strategy
* @return RouteCollectorInterface
*/
public function map(array $methods, string $pattern, $handler): RouteInterface;
public function setDefaultInvocationStrategy(InvocationStrategyInterface $strategy): RouteCollectorInterface;

/**
l0gicgate marked this conversation as resolved.
Show resolved Hide resolved
* Dispatch router for HTTP request
* Get path to FastRoute cache file
*
* @param ServerRequestInterface $request The current HTTP request object
* @return null|string
*/
public function getCacheFile(): ?string;

/**
* Set path to FastRoute cache file
*
* @return RoutingResults
* @param string $cacheFile
* @return RouteCollectorInterface
*
* @link https://github.com/nikic/FastRoute/blob/master/src/Dispatcher.php
* @throws InvalidArgumentException
* @throws RuntimeException
*/
public function dispatch(ServerRequestInterface $request): RoutingResults;
public function setCacheFile(string $cacheFile): RouteCollectorInterface;

/**
* Add a route group to the array
* Get the base path used in pathFor()
*
* @param string $pattern The group pattern
* @param callable $callable A group callable
* @return string
*/
public function getBasePath(): string;

/**
* Set the base path used in pathFor()
*
* @return RouteGroupInterface
* @param string $basePath
* @return RouteCollectorInterface
*/
public function pushGroup(string $pattern, $callable): RouteGroupInterface;
public function setBasePath(string $basePath): RouteCollectorInterface;

/**
* Removes the last route group from the array
* Get route objects
*
* @return RouteGroupInterface|null
* @return RouteInterface[]
*/
public function popGroup();
public function getRoutes(): array;

/**
* Get named route object
Expand All @@ -72,13 +80,53 @@ public function popGroup();
*/
public function getNamedRoute(string $name): RouteInterface;

/**
* Remove named route
*
* @param string $name Route name
* @return RouteCollectorInterface
*
* @throws RuntimeException If named route does not exist
*/
public function removeNamedRoute(string $name): RouteCollectorInterface;

/**
* @param string $identifier
*
* @return RouteInterface
*
* @throws RuntimeException
*/
public function lookupRoute(string $identifier): RouteInterface;
l0gicgate marked this conversation as resolved.
Show resolved Hide resolved

/**
* Add a route group to the array
*
* @param string $pattern The group pattern
* @param callable $callable A group callable
*
* @return RouteGroupInterface
*/
public function pushGroup(string $pattern, $callable): RouteGroupInterface;
l0gicgate marked this conversation as resolved.
Show resolved Hide resolved

/**
* Removes the last route group from the array
*
* @return RouteGroupInterface|null
*/
public function popGroup();

/**
* Add route
*
* @param string[] $methods Array of HTTP methods
* @param string $pattern The route pattern
* @param callable|string $handler The route callable
*
* @return RouteInterface
*/
public function map(array $methods, string $pattern, $handler): RouteInterface;

/**
* Build the path for a named route excluding the base path
*
Expand Down Expand Up @@ -108,16 +156,18 @@ public function relativePathFor(string $name, array $data = [], array $queryPara
public function pathFor(string $name, array $data = [], array $queryParams = []): string;

/**
* @param string|null $cacheFile
* @return RouterInterface
*/
public function setCacheFile(?string $cacheFile): RouterInterface;

/**
* Set default route invocation strategy
* Build the path for a named route.
*
* @param InvocationStrategyInterface $strategy
* @return RouterInterface
* This method is deprecated. Use pathFor() from now on.
*
* @param string $name Route name
* @param array $data Named argument replacement data
* @param array $queryParams Optional query string parameters
*
* @return string
*
* @throws RuntimeException If named route does not exist
* @throws InvalidArgumentException If required data not provided
*/
public function setDefaultInvocationStrategy(InvocationStrategyInterface $strategy): RouterInterface;
public function urlFor(string $name, array $data = [], array $queryParams = []): string;
}
Loading