diff --git a/src/Routing/Route/RouteFactory.php b/src/Routing/Route/RouteFactory.php new file mode 100644 index 0000000..83e0bd1 --- /dev/null +++ b/src/Routing/Route/RouteFactory.php @@ -0,0 +1,67 @@ +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); + } +} diff --git a/test/tests/unit/Routing/Route/RouteFactoryTest.php b/test/tests/unit/Routing/Route/RouteFactoryTest.php new file mode 100644 index 0000000..fb7e893 --- /dev/null +++ b/test/tests/unit/Routing/Route/RouteFactoryTest.php @@ -0,0 +1,57 @@ +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(); + } +}