Add RouteFactoryInterface
This commit is contained in:
parent
6d9adfc7ee
commit
cb87660548
|
|
@ -2,39 +2,28 @@
|
|||
|
||||
namespace WellRESTed\Routing\Route;
|
||||
|
||||
use ReflectionClass;
|
||||
use WellRESTed\Routing\RouteTableInterface;
|
||||
|
||||
/**
|
||||
* Class for creating routes
|
||||
*/
|
||||
class RouteFactory
|
||||
class RouteFactory implements RouteFactoryInterface
|
||||
{
|
||||
/** @var RouteTableInterface */
|
||||
private $table;
|
||||
|
||||
public function __construct(RouteTableInterface $table)
|
||||
{
|
||||
$this->table = $table;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create and return a route given a string path, a handler, and optional
|
||||
* extra arguments.
|
||||
*
|
||||
* The method will determine the most appropriate route subclass to use
|
||||
* and will forward the arguments on to the subclass's constructor.
|
||||
*
|
||||
* - Paths with no special characters will generate StaticRoutes
|
||||
* - Paths ending with * will generate PrefixRoutes
|
||||
* - Paths containing URI variables (e.g., {id}) will generate TemplateRoutes
|
||||
* - Regular exressions will generate RegexRoutes
|
||||
* - Paths with no special characters will register StaticRoutes
|
||||
* - Paths ending with * will register PrefixRoutes
|
||||
* - Paths containing URI variables (e.g., {id}) will register TemplateRoutes
|
||||
* - Regular exressions will register RegexRoutes
|
||||
*
|
||||
* @param RouteTableInterface $routeTable Table to add the route to
|
||||
* @param string $target Path, prefix, or pattern to match
|
||||
* @param mixed $middleware Middleware to dispatch
|
||||
* @param string|array $variablePattern @see TemplateRoute
|
||||
* @param mixed $extra Additional options to pass to a route constructor
|
||||
*/
|
||||
public function registerRoute($target, $middleware, $variablePattern = null)
|
||||
public function registerRoute(RouteTableInterface $routeTable, $target, $middleware, $extra = null)
|
||||
{
|
||||
if ($target[0] === "/") {
|
||||
|
||||
|
|
@ -45,22 +34,22 @@ class RouteFactory
|
|||
// Remove the trailing *, since the PrefixRoute constructor doesn't expect it.
|
||||
$target = substr($target, 0, -1);
|
||||
$route = new PrefixRoute($target, $middleware);
|
||||
$this->table->addPrefixRoute($route);
|
||||
$routeTable->addPrefixRoute($route);
|
||||
}
|
||||
|
||||
// TempalateRoutes contain {variable}
|
||||
if (preg_match(TemplateRoute::URI_TEMPLATE_EXPRESSION_RE, $target)) {
|
||||
$route = new TemplateRoute($target, $middleware, $variablePattern);
|
||||
$this->table->addRoute($route);
|
||||
$route = new TemplateRoute($target, $middleware, $extra);
|
||||
$routeTable->addRoute($route);
|
||||
}
|
||||
|
||||
// StaticRoute
|
||||
$route = new StaticRoute($target, $middleware);
|
||||
$this->table->addStaticRoute($route);
|
||||
$routeTable->addStaticRoute($route);
|
||||
}
|
||||
|
||||
// Regex
|
||||
$route = new RegexRoute($target, $middleware);
|
||||
$this->table->addRoute($route);
|
||||
$routeTable->addRoute($route);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace WellRESTed\Routing\Route;
|
||||
|
||||
use WellRESTed\Routing\RouteTableInterface;
|
||||
|
||||
interface RouteFactoryInterface
|
||||
{
|
||||
/**
|
||||
* Adds a new route to a route table.
|
||||
*
|
||||
* This method SHOULD parse $target to determine to the type of route to
|
||||
* use and MUST create the route with the provided $middleware.
|
||||
*
|
||||
* Once the implementation has created the route the route, it MUST
|
||||
* the route with $routeTable by calling an appropriate RouteTable::add-
|
||||
* method.
|
||||
*
|
||||
* $extra MAY be passed to route constructors that use an extra option,
|
||||
* such as TemplateRoute.
|
||||
*
|
||||
* This method MAY register any instance implementing
|
||||
* WellRESTed\Routing\Route\RouteInterface.
|
||||
*
|
||||
* @param RouteTableInterface $routeTable Table to add the route to
|
||||
* @param string $target Path, prefix, or pattern to match
|
||||
* @param mixed $middleware Middleware to dispatch
|
||||
* @param mixed $extra Additional options to pass to a route constructor
|
||||
*/
|
||||
public function registerRoute(RouteTableInterface $routeTable, $target, $middleware, $extra = null);
|
||||
}
|
||||
|
|
@ -6,6 +6,7 @@ use Psr\Http\Message\ResponseInterface;
|
|||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use WellRESTed\HttpExceptions\HttpException;
|
||||
use WellRESTed\Routing\Route\RouteFactory;
|
||||
use WellRESTed\Routing\Route\RouteFactoryInterface;
|
||||
use WellRESTed\Stream\StringStream;
|
||||
|
||||
class Router implements MiddlewareInterface
|
||||
|
|
@ -14,13 +15,13 @@ class Router implements MiddlewareInterface
|
|||
private $statusHandlers;
|
||||
/** @var RouteTable Collection of routes */
|
||||
private $routeTable;
|
||||
/** @var RouteFactory */
|
||||
/** @var RouteFactoryInterface */
|
||||
private $routeFactory;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->routeFactory = $this->getRouteFactory();
|
||||
$this->routeTable = new RouteTable();
|
||||
$this->routeFactory = new RouteFactory($this->routeTable);
|
||||
$this->statusHandlers = [];
|
||||
}
|
||||
|
||||
|
|
@ -45,7 +46,7 @@ class Router implements MiddlewareInterface
|
|||
if (is_array($middleware)) {
|
||||
$middleware = $this->getMethodMap($middleware);
|
||||
}
|
||||
$this->routeFactory->registerRoute($target, $middleware, $extra);
|
||||
$this->routeFactory->registerRoute($this->routeTable, $target, $middleware, $extra);
|
||||
}
|
||||
|
||||
public function setStatusHandler($statusCode, $middleware)
|
||||
|
|
@ -73,4 +74,12 @@ class Router implements MiddlewareInterface
|
|||
{
|
||||
return new MethodMap($map);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return RouteFactoryInterface
|
||||
*/
|
||||
protected function getRouteFactory()
|
||||
{
|
||||
return new RouteFactory();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,29 +28,29 @@ class RouteFactoryTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
public function testRegistersStaticRoute()
|
||||
{
|
||||
$factory = new RouteFactory($this->routeTable->reveal());
|
||||
$factory->registerRoute("/cats/", null);
|
||||
$factory = new RouteFactory();
|
||||
$factory->registerRoute($this->routeTable->reveal(), "/cats/", null);
|
||||
$this->routeTable->addStaticRoute(Argument::any())->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testRegistersPrefixRoute()
|
||||
{
|
||||
$factory = new RouteFactory($this->routeTable->reveal());
|
||||
$factory->registerRoute("/cats/*", null);
|
||||
$factory = new RouteFactory();
|
||||
$factory->registerRoute($this->routeTable->reveal(), "/cats/*", null);
|
||||
$this->routeTable->addPrefixRoute(Argument::any())->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testRegistersTemplateRoute()
|
||||
{
|
||||
$factory = new RouteFactory($this->routeTable->reveal());
|
||||
$factory->registerRoute("/cats/{catId}", null);
|
||||
$factory = new RouteFactory();
|
||||
$factory->registerRoute($this->routeTable->reveal(), "/cats/{catId}", null);
|
||||
$this->routeTable->addRoute(Argument::type("\\WellRESTed\\Routing\\Route\\TemplateRoute"))->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
public function testRegistersRegexRoute()
|
||||
{
|
||||
$factory = new RouteFactory($this->routeTable->reveal());
|
||||
$factory->registerRoute("~/cat/[0-9]+~", null);
|
||||
$factory = new RouteFactory();
|
||||
$factory->registerRoute($this->routeTable->reveal(), "~/cat/[0-9]+~", null);
|
||||
$this->routeTable->addRoute(Argument::type("\\WellRESTed\\Routing\\Route\\RegexRoute"))->shouldHaveBeenCalled();
|
||||
$this->routeTable->addRoute(Argument::type("\\WellRESTed\\Routing\\Route\\TemplateRoute"))->shouldNotHaveBeenCalled();
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue