Add RouteFactory

This commit is contained in:
PJ Dietz 2015-04-02 22:15:07 -04:00
parent f788d9a2f3
commit 57271fa19f
2 changed files with 124 additions and 0 deletions

View File

@ -0,0 +1,67 @@
<?php
namespace WellRESTed\Routing\Route;
use ReflectionClass;
use WellRESTed\Routing\RouteTableInterface;
/**
* Class for creating routes
*/
class RouteFactory
{
/** @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
*
* @param string $target Path, prefix, or pattern to match
* @param mixed $middleware Middleware to dispatch
* @param $defaultPattern @see TemplateRoute
* @param $variablePatterns @see TemplateRoute
*/
public function registerRoute($target, $middleware, $defaultPattern = null, $variablePatterns = null)
{
if ($target[0] === "/") {
// Possible static, prefix, or template
// PrefixRoutes end with *
if (substr($target, -1) === "*") {
// 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);
}
// TempalateRoutes contain {variable}
if (preg_match(TemplateRoute::URI_TEMPLATE_EXPRESSION_RE, $target)) {
$route = new TemplateRoute($target, $middleware, $defaultPattern, $variablePatterns);
$this->table->addRoute($route);
}
// StaticRoute
$route = new StaticRoute($target, $middleware);
$this->table->addStaticRoute($route);
}
// Regex
$route = new RegexRoute($target, $middleware);
$this->table->addRoute($route);
}
}

View File

@ -0,0 +1,57 @@
<?php
namespace WellRESTed\Test\Unit\Routing\Route;
use Prophecy\Argument;
use WellRESTed\Routing\Route\RouteFactory;
/**
* @covers WellRESTed\Routing\Route\RouteFactory
* @uses WellRESTed\Routing\Route\TemplateRoute
* @uses WellRESTed\Routing\Route\RegexRoute
* @uses WellRESTed\Routing\Route\PrefixRoute
* @uses WellRESTed\Routing\Route\StaticRoute
* @uses WellRESTed\Routing\Route\Route
*/
class RouteFactoryTest extends \PHPUnit_Framework_TestCase
{
private $routeTable;
public function setUp()
{
parent::setUp();
$this->routeTable = $this->prophesize("\\WellRESTed\\Routing\\RouteTableInterface");
$this->routeTable->addStaticRoute(Argument::cetera())->willReturn();
$this->routeTable->addPrefixRoute(Argument::cetera())->willReturn();
$this->routeTable->addRoute(Argument::cetera())->willReturn();
}
public function testRegistersStaticRoute()
{
$factory = new RouteFactory($this->routeTable->reveal());
$factory->registerRoute("/cats/", null);
$this->routeTable->addStaticRoute(Argument::any())->shouldHaveBeenCalled();
}
public function testRegistersPrefixRoute()
{
$factory = new RouteFactory($this->routeTable->reveal());
$factory->registerRoute("/cats/*", null);
$this->routeTable->addPrefixRoute(Argument::any())->shouldHaveBeenCalled();
}
public function testRegistersTemplateRoute()
{
$factory = new RouteFactory($this->routeTable->reveal());
$factory->registerRoute("/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);
$this->routeTable->addRoute(Argument::type("\\WellRESTed\\Routing\\Route\\RegexRoute"))->shouldHaveBeenCalled();
$this->routeTable->addRoute(Argument::type("\\WellRESTed\\Routing\\Route\\TemplateRoute"))->shouldNotHaveBeenCalled();
}
}