diff --git a/src/pjdietz/WellRESTed/Interfaces/DispatcherInterface.php b/src/pjdietz/WellRESTed/Interfaces/DispatcherInterface.php new file mode 100644 index 0000000..659d617 --- /dev/null +++ b/src/pjdietz/WellRESTed/Interfaces/DispatcherInterface.php @@ -0,0 +1,17 @@ + - * @copyright Copyright 2013 by PJ Dietz - * @license MIT - */ - -namespace pjdietz\WellRESTed\Interfaces; - -/** - * Interface for a route to relate a pattern for matching a URI to a handler class. - * @package pjdietz\WellRESTed - */ -interface RouteInterface -{ - /** @return string Regex pattern used to match the URI */ - public function getPattern(); - - /** @para string $pattern Regex pattern used to match the URI */ - public function setPattern($pattern); - - /** @return string Fully qualified name of the class the route will dispatch. */ - public function getTarget(); - - /** @param string $className Fully qualified name of the class the route will dispatch. */ - public function setTarget($className); -} diff --git a/src/pjdietz/WellRESTed/Routes/BaseRoute.php b/src/pjdietz/WellRESTed/Routes/BaseRoute.php new file mode 100644 index 0000000..8769ca1 --- /dev/null +++ b/src/pjdietz/WellRESTed/Routes/BaseRoute.php @@ -0,0 +1,40 @@ +targetClassName = $targetClassName; + } + + protected function getTarget(RoutableInterface $routable) + { + if (is_subclass_of($this->targetClassName, self::ROUTE_TARGET_INTERFACE)) { + /** @var RouteTargetInterface $target */ + $target = new $this->targetClassName(); + $target->setRequest($routable); + return $target; + } + return null; + } + +} diff --git a/src/pjdietz/WellRESTed/Routes/RegexRoute.php b/src/pjdietz/WellRESTed/Routes/RegexRoute.php new file mode 100644 index 0000000..2fcaa61 --- /dev/null +++ b/src/pjdietz/WellRESTed/Routes/RegexRoute.php @@ -0,0 +1,43 @@ +pattern = $pattern; + } + + // ------------------------------------------------------------------------ + /* DispatcherInterface */ + + public function getResponse(RoutableInterface $request) + { + if (preg_match($this->getPattern(), $request->getPath(), $matches)) { + $target = $this->getTarget($request); + if ($target) { + $target->setArguments($matches); + return $target->getResponse($request); + } + } + return null; + } + + // ------------------------------------------------------------------------ + + protected function getPattern() + { + return $this->pattern; + } + +} diff --git a/src/pjdietz/WellRESTed/Routes/StaticRoute.php b/src/pjdietz/WellRESTed/Routes/StaticRoute.php new file mode 100644 index 0000000..a35ac2f --- /dev/null +++ b/src/pjdietz/WellRESTed/Routes/StaticRoute.php @@ -0,0 +1,50 @@ +paths = array($paths); + } elseif (is_array($paths)) { + $this->paths = $paths; + } else { + throw new InvalidArgumentException("$paths must be a string or array of string"); + } + } + + // ------------------------------------------------------------------------ + /* DispatcherInterface */ + + public function getResponse(RoutableInterface $request) + { + $requestPath = $request->getPath(); + foreach ($this->paths as $path) { + if ($path === $requestPath) { + $target = $this->getTarget($request); + if ($target) { + return $target->getResponse($request); + } + } + } + return null; + } + +} diff --git a/src/pjdietz/WellRESTed/Routes/TemplateRoute.php b/src/pjdietz/WellRESTed/Routes/TemplateRoute.php new file mode 100644 index 0000000..ad7ff21 --- /dev/null +++ b/src/pjdietz/WellRESTed/Routes/TemplateRoute.php @@ -0,0 +1,100 @@ +buildPattern($template, $variables); + parent::__construct($pattern, $targetClassName); + } + + private function buildPattern($template, $variables) + { + if (is_null($variables)) { + $variables = array(); + } + + $pattern = ''; + + // Explode the template into an array of path segments. + if ($template[0] === '/') { + $parts = explode('/', substr($template, 1)); + } else { + $parts = explode('/', $template); + } + + foreach ($parts as $part) { + + $pattern .= '\/'; + + // Is this part an expression or a literal? + if (preg_match(self::URI_TEMPLATE_EXPRESSION_RE, $part, $matches)) { + + // This part of the path is an expresion. + + if (count($matches) === 2) { + + // Locate the name for the variable from the template. + $variableName = $matches[1]; + + // If the caller passed an array with this variable name + // as a key, use its value for the pattern here. + // Otherwise, use the class's current default. + if (isset($variables[$variableName])) { + $variablePattern = $variables[$variableName]; + } else { + $variablePattern = self::$defaultVariablePattern; + } + + $pattern .= sprintf( + '(?<%s>%s)', + $variableName, + $variablePattern + ); + + } else { + throw new InvalidArgumentException('Invalid URI Template.'); + } + + } else { + // This part is a literal. + $pattern .= $part; + } + + } + + $pattern = '/^' . $pattern . '$/'; + return $pattern; + } + +}