From cb87660548803ff03eb72612cb5c56a1c60dfed7 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Mon, 6 Apr 2015 20:18:21 -0400 Subject: [PATCH] Add RouteFactoryInterface --- src/Routing/Route/RouteFactory.php | 37 +++++++------------ src/Routing/Route/RouteFactoryInterface.php | 31 ++++++++++++++++ src/Routing/Router.php | 15 ++++++-- .../unit/Routing/Route/RouteFactoryTest.php | 16 ++++---- 4 files changed, 64 insertions(+), 35 deletions(-) create mode 100644 src/Routing/Route/RouteFactoryInterface.php diff --git a/src/Routing/Route/RouteFactory.php b/src/Routing/Route/RouteFactory.php index 1995fd3..f77d519 100644 --- a/src/Routing/Route/RouteFactory.php +++ b/src/Routing/Route/RouteFactory.php @@ -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); } } diff --git a/src/Routing/Route/RouteFactoryInterface.php b/src/Routing/Route/RouteFactoryInterface.php new file mode 100644 index 0000000..b7ee4f8 --- /dev/null +++ b/src/Routing/Route/RouteFactoryInterface.php @@ -0,0 +1,31 @@ +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(); + } } diff --git a/test/tests/unit/Routing/Route/RouteFactoryTest.php b/test/tests/unit/Routing/Route/RouteFactoryTest.php index fb7e893..40abe1c 100644 --- a/test/tests/unit/Routing/Route/RouteFactoryTest.php +++ b/test/tests/unit/Routing/Route/RouteFactoryTest.php @@ -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(); }