@@ -24,28 +24,28 @@ |
||
| 24 | 24 | class CallableRequestHandler implements RequestHandlerInterface |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * The request handler callback |
|
| 29 | - * |
|
| 30 | - * @var callable |
|
| 31 | - */ |
|
| 32 | - private $callback; |
|
| 27 | + /** |
|
| 28 | + * The request handler callback |
|
| 29 | + * |
|
| 30 | + * @var callable |
|
| 31 | + */ |
|
| 32 | + private $callback; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Constructor of the class |
|
| 36 | - * |
|
| 37 | - * @param callable $callback |
|
| 38 | - */ |
|
| 39 | - public function __construct(callable $callback) |
|
| 40 | - { |
|
| 41 | - $this->callback = $callback; |
|
| 42 | - } |
|
| 34 | + /** |
|
| 35 | + * Constructor of the class |
|
| 36 | + * |
|
| 37 | + * @param callable $callback |
|
| 38 | + */ |
|
| 39 | + public function __construct(callable $callback) |
|
| 40 | + { |
|
| 41 | + $this->callback = $callback; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * {@inheritDoc} |
|
| 46 | - */ |
|
| 47 | - public function handle(ServerRequestInterface $request) : ResponseInterface |
|
| 48 | - { |
|
| 49 | - return ($this->callback)($request); |
|
| 50 | - } |
|
| 44 | + /** |
|
| 45 | + * {@inheritDoc} |
|
| 46 | + */ |
|
| 47 | + public function handle(ServerRequestInterface $request) : ResponseInterface |
|
| 48 | + { |
|
| 49 | + return ($this->callback)($request); |
|
| 50 | + } |
|
| 51 | 51 | } |
@@ -28,215 +28,215 @@ |
||
| 28 | 28 | class Route implements RouteInterface |
| 29 | 29 | { |
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * The route name |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 36 | - private $name; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The route path |
|
| 40 | - * |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - private $path; |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The route methods |
|
| 47 | - * |
|
| 48 | - * @var string[] |
|
| 49 | - */ |
|
| 50 | - private $methods; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * The route request handler |
|
| 54 | - * |
|
| 55 | - * @var RequestHandlerInterface |
|
| 56 | - */ |
|
| 57 | - private $requestHandler; |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The route middlewares |
|
| 61 | - * |
|
| 62 | - * @var MiddlewareInterface[] |
|
| 63 | - */ |
|
| 64 | - private $middlewares = []; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The route attributes |
|
| 68 | - * |
|
| 69 | - * @var array |
|
| 70 | - */ |
|
| 71 | - private $attributes = []; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * Constructor of the class |
|
| 75 | - * |
|
| 76 | - * @param string $name |
|
| 77 | - * @param string $path |
|
| 78 | - * @param string[] $methods |
|
| 79 | - * @param RequestHandlerInterface $requestHandler |
|
| 80 | - * @param MiddlewareInterface[] $middlewares |
|
| 81 | - * @param array $attributes |
|
| 82 | - */ |
|
| 83 | - public function __construct( |
|
| 84 | - string $name, |
|
| 85 | - string $path, |
|
| 86 | - array $methods, |
|
| 87 | - RequestHandlerInterface $requestHandler, |
|
| 88 | - array $middlewares = [], |
|
| 89 | - array $attributes = [] |
|
| 90 | - ) { |
|
| 91 | - $this->setName($name); |
|
| 92 | - $this->setPath($path); |
|
| 93 | - $this->setMethods(...$methods); |
|
| 94 | - $this->setRequestHandler($requestHandler); |
|
| 95 | - $this->setMiddlewares(...$middlewares); |
|
| 96 | - $this->setAttributes($attributes); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * {@inheritDoc} |
|
| 101 | - */ |
|
| 102 | - public function getName() : string |
|
| 103 | - { |
|
| 104 | - return $this->name; |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * {@inheritDoc} |
|
| 109 | - */ |
|
| 110 | - public function getPath() : string |
|
| 111 | - { |
|
| 112 | - return $this->path; |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * {@inheritDoc} |
|
| 117 | - */ |
|
| 118 | - public function getMethods() : array |
|
| 119 | - { |
|
| 120 | - return $this->methods; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * {@inheritDoc} |
|
| 125 | - */ |
|
| 126 | - public function getRequestHandler() : RequestHandlerInterface |
|
| 127 | - { |
|
| 128 | - return $this->requestHandler; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * {@inheritDoc} |
|
| 133 | - */ |
|
| 134 | - public function getMiddlewares() : array |
|
| 135 | - { |
|
| 136 | - return $this->middlewares; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * {@inheritDoc} |
|
| 141 | - */ |
|
| 142 | - public function getAttributes() : array |
|
| 143 | - { |
|
| 144 | - return $this->attributes; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * {@inheritDoc} |
|
| 149 | - */ |
|
| 150 | - public function setName(string $name) : RouteInterface |
|
| 151 | - { |
|
| 152 | - $this->name = $name; |
|
| 153 | - |
|
| 154 | - return $this; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * {@inheritDoc} |
|
| 159 | - */ |
|
| 160 | - public function setPath(string $path) : RouteInterface |
|
| 161 | - { |
|
| 162 | - $this->path = $path; |
|
| 163 | - |
|
| 164 | - return $this; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * {@inheritDoc} |
|
| 169 | - */ |
|
| 170 | - public function setMethods(string ...$methods) : RouteInterface |
|
| 171 | - { |
|
| 172 | - foreach ($methods as &$method) { |
|
| 173 | - $method = strtoupper($method); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - $this->methods = $methods; |
|
| 177 | - |
|
| 178 | - return $this; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * {@inheritDoc} |
|
| 183 | - */ |
|
| 184 | - public function setRequestHandler(RequestHandlerInterface $requestHandler) : RouteInterface |
|
| 185 | - { |
|
| 186 | - $this->requestHandler = $requestHandler; |
|
| 187 | - |
|
| 188 | - return $this; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * {@inheritDoc} |
|
| 193 | - */ |
|
| 194 | - public function setMiddlewares(MiddlewareInterface ...$middlewares) : RouteInterface |
|
| 195 | - { |
|
| 196 | - $this->middlewares = $middlewares; |
|
| 197 | - |
|
| 198 | - return $this; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * {@inheritDoc} |
|
| 203 | - */ |
|
| 204 | - public function setAttributes(array $attributes) : RouteInterface |
|
| 205 | - { |
|
| 206 | - $this->attributes = $attributes; |
|
| 207 | - |
|
| 208 | - return $this; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * {@inheritDoc} |
|
| 213 | - */ |
|
| 214 | - public function withAddedAttributes(array $attributes) : RouteInterface |
|
| 215 | - { |
|
| 216 | - $clone = clone $this; |
|
| 217 | - |
|
| 218 | - foreach ($attributes as $key => $value) { |
|
| 219 | - $clone->attributes[$key] = $value; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - return $clone; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * {@inheritDoc} |
|
| 227 | - */ |
|
| 228 | - public function buildPath(array $attributes = [], bool $strict = false) : string |
|
| 229 | - { |
|
| 230 | - $attributes += $this->attributes; |
|
| 231 | - |
|
| 232 | - return path_build($this->path, $attributes, $strict); |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - /** |
|
| 236 | - * {@inheritDoc} |
|
| 237 | - */ |
|
| 238 | - public function buildRegex() : string |
|
| 239 | - { |
|
| 240 | - return path_regex($this->path); |
|
| 241 | - } |
|
| 31 | + /** |
|
| 32 | + * The route name |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | + private $name; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The route path |
|
| 40 | + * |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + private $path; |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The route methods |
|
| 47 | + * |
|
| 48 | + * @var string[] |
|
| 49 | + */ |
|
| 50 | + private $methods; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * The route request handler |
|
| 54 | + * |
|
| 55 | + * @var RequestHandlerInterface |
|
| 56 | + */ |
|
| 57 | + private $requestHandler; |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The route middlewares |
|
| 61 | + * |
|
| 62 | + * @var MiddlewareInterface[] |
|
| 63 | + */ |
|
| 64 | + private $middlewares = []; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The route attributes |
|
| 68 | + * |
|
| 69 | + * @var array |
|
| 70 | + */ |
|
| 71 | + private $attributes = []; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * Constructor of the class |
|
| 75 | + * |
|
| 76 | + * @param string $name |
|
| 77 | + * @param string $path |
|
| 78 | + * @param string[] $methods |
|
| 79 | + * @param RequestHandlerInterface $requestHandler |
|
| 80 | + * @param MiddlewareInterface[] $middlewares |
|
| 81 | + * @param array $attributes |
|
| 82 | + */ |
|
| 83 | + public function __construct( |
|
| 84 | + string $name, |
|
| 85 | + string $path, |
|
| 86 | + array $methods, |
|
| 87 | + RequestHandlerInterface $requestHandler, |
|
| 88 | + array $middlewares = [], |
|
| 89 | + array $attributes = [] |
|
| 90 | + ) { |
|
| 91 | + $this->setName($name); |
|
| 92 | + $this->setPath($path); |
|
| 93 | + $this->setMethods(...$methods); |
|
| 94 | + $this->setRequestHandler($requestHandler); |
|
| 95 | + $this->setMiddlewares(...$middlewares); |
|
| 96 | + $this->setAttributes($attributes); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * {@inheritDoc} |
|
| 101 | + */ |
|
| 102 | + public function getName() : string |
|
| 103 | + { |
|
| 104 | + return $this->name; |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * {@inheritDoc} |
|
| 109 | + */ |
|
| 110 | + public function getPath() : string |
|
| 111 | + { |
|
| 112 | + return $this->path; |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * {@inheritDoc} |
|
| 117 | + */ |
|
| 118 | + public function getMethods() : array |
|
| 119 | + { |
|
| 120 | + return $this->methods; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * {@inheritDoc} |
|
| 125 | + */ |
|
| 126 | + public function getRequestHandler() : RequestHandlerInterface |
|
| 127 | + { |
|
| 128 | + return $this->requestHandler; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * {@inheritDoc} |
|
| 133 | + */ |
|
| 134 | + public function getMiddlewares() : array |
|
| 135 | + { |
|
| 136 | + return $this->middlewares; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * {@inheritDoc} |
|
| 141 | + */ |
|
| 142 | + public function getAttributes() : array |
|
| 143 | + { |
|
| 144 | + return $this->attributes; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * {@inheritDoc} |
|
| 149 | + */ |
|
| 150 | + public function setName(string $name) : RouteInterface |
|
| 151 | + { |
|
| 152 | + $this->name = $name; |
|
| 153 | + |
|
| 154 | + return $this; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * {@inheritDoc} |
|
| 159 | + */ |
|
| 160 | + public function setPath(string $path) : RouteInterface |
|
| 161 | + { |
|
| 162 | + $this->path = $path; |
|
| 163 | + |
|
| 164 | + return $this; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * {@inheritDoc} |
|
| 169 | + */ |
|
| 170 | + public function setMethods(string ...$methods) : RouteInterface |
|
| 171 | + { |
|
| 172 | + foreach ($methods as &$method) { |
|
| 173 | + $method = strtoupper($method); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + $this->methods = $methods; |
|
| 177 | + |
|
| 178 | + return $this; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * {@inheritDoc} |
|
| 183 | + */ |
|
| 184 | + public function setRequestHandler(RequestHandlerInterface $requestHandler) : RouteInterface |
|
| 185 | + { |
|
| 186 | + $this->requestHandler = $requestHandler; |
|
| 187 | + |
|
| 188 | + return $this; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * {@inheritDoc} |
|
| 193 | + */ |
|
| 194 | + public function setMiddlewares(MiddlewareInterface ...$middlewares) : RouteInterface |
|
| 195 | + { |
|
| 196 | + $this->middlewares = $middlewares; |
|
| 197 | + |
|
| 198 | + return $this; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * {@inheritDoc} |
|
| 203 | + */ |
|
| 204 | + public function setAttributes(array $attributes) : RouteInterface |
|
| 205 | + { |
|
| 206 | + $this->attributes = $attributes; |
|
| 207 | + |
|
| 208 | + return $this; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * {@inheritDoc} |
|
| 213 | + */ |
|
| 214 | + public function withAddedAttributes(array $attributes) : RouteInterface |
|
| 215 | + { |
|
| 216 | + $clone = clone $this; |
|
| 217 | + |
|
| 218 | + foreach ($attributes as $key => $value) { |
|
| 219 | + $clone->attributes[$key] = $value; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + return $clone; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * {@inheritDoc} |
|
| 227 | + */ |
|
| 228 | + public function buildPath(array $attributes = [], bool $strict = false) : string |
|
| 229 | + { |
|
| 230 | + $attributes += $this->attributes; |
|
| 231 | + |
|
| 232 | + return path_build($this->path, $attributes, $strict); |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + /** |
|
| 236 | + * {@inheritDoc} |
|
| 237 | + */ |
|
| 238 | + public function buildRegex() : string |
|
| 239 | + { |
|
| 240 | + return path_regex($this->path); |
|
| 241 | + } |
|
| 242 | 242 | } |
@@ -34,94 +34,94 @@ |
||
| 34 | 34 | class Router extends RouteCollection implements MiddlewareInterface, RequestHandlerInterface |
| 35 | 35 | { |
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Server Request attribute name for routing error instance |
|
| 39 | - * |
|
| 40 | - * @var string |
|
| 41 | - */ |
|
| 42 | - public const ATTR_NAME_FOR_ROUTING_ERROR = '@routing-error'; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Gets a route for the given name |
|
| 46 | - * |
|
| 47 | - * @param string $name |
|
| 48 | - * |
|
| 49 | - * @return RouteInterface |
|
| 50 | - * |
|
| 51 | - * @throws RouteNotFoundException |
|
| 52 | - */ |
|
| 53 | - public function getRoute(string $name) : RouteInterface |
|
| 54 | - { |
|
| 55 | - foreach ($this->getRoutes() as $route) { |
|
| 56 | - if ($name === $route->getName()) { |
|
| 57 | - return $route; |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - throw new RouteNotFoundException(); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Looks for a route that matches the given request |
|
| 66 | - * |
|
| 67 | - * @param ServerRequestInterface $request |
|
| 68 | - * |
|
| 69 | - * @return RouteInterface |
|
| 70 | - * |
|
| 71 | - * @throws MethodNotAllowedException |
|
| 72 | - * @throws RouteNotFoundException |
|
| 73 | - * |
|
| 74 | - * @todo Matching by strategy for detecting verbs... |
|
| 75 | - */ |
|
| 76 | - public function match(ServerRequestInterface $request) : RouteInterface |
|
| 77 | - { |
|
| 78 | - $routes = []; |
|
| 79 | - foreach ($this->getRoutes() as $route) { |
|
| 80 | - foreach ($route->getMethods() as $method) { |
|
| 81 | - $routes[$method][] = $route; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $method = $request->getMethod(); |
|
| 86 | - if (! isset($routes[$method])) { |
|
| 87 | - throw new MethodNotAllowedException( |
|
| 88 | - array_keys($routes) |
|
| 89 | - ); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - $target = $request->getUri()->getPath(); |
|
| 93 | - foreach ($routes[$method] as $route) { |
|
| 94 | - if (path_match($route->getPath(), $target, $attributes)) { |
|
| 95 | - return $route->withAddedAttributes($attributes); |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - throw new RouteNotFoundException(); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * {@inheritDoc} |
|
| 104 | - */ |
|
| 105 | - public function handle(ServerRequestInterface $request) : ResponseInterface |
|
| 106 | - { |
|
| 107 | - $route = $this->match($request); |
|
| 108 | - |
|
| 109 | - $requestHandler = new RoutableRequestHandler($route); |
|
| 110 | - |
|
| 111 | - return $requestHandler->handle($request); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * {@inheritDoc} |
|
| 116 | - */ |
|
| 117 | - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
|
| 118 | - { |
|
| 119 | - try { |
|
| 120 | - return $this->handle($request); |
|
| 121 | - } catch (ExceptionInterface $e) { |
|
| 122 | - return $handler->handle( |
|
| 123 | - $request->withAttribute(self::ATTR_NAME_FOR_ROUTING_ERROR, $e) |
|
| 124 | - ); |
|
| 125 | - } |
|
| 126 | - } |
|
| 37 | + /** |
|
| 38 | + * Server Request attribute name for routing error instance |
|
| 39 | + * |
|
| 40 | + * @var string |
|
| 41 | + */ |
|
| 42 | + public const ATTR_NAME_FOR_ROUTING_ERROR = '@routing-error'; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Gets a route for the given name |
|
| 46 | + * |
|
| 47 | + * @param string $name |
|
| 48 | + * |
|
| 49 | + * @return RouteInterface |
|
| 50 | + * |
|
| 51 | + * @throws RouteNotFoundException |
|
| 52 | + */ |
|
| 53 | + public function getRoute(string $name) : RouteInterface |
|
| 54 | + { |
|
| 55 | + foreach ($this->getRoutes() as $route) { |
|
| 56 | + if ($name === $route->getName()) { |
|
| 57 | + return $route; |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + throw new RouteNotFoundException(); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Looks for a route that matches the given request |
|
| 66 | + * |
|
| 67 | + * @param ServerRequestInterface $request |
|
| 68 | + * |
|
| 69 | + * @return RouteInterface |
|
| 70 | + * |
|
| 71 | + * @throws MethodNotAllowedException |
|
| 72 | + * @throws RouteNotFoundException |
|
| 73 | + * |
|
| 74 | + * @todo Matching by strategy for detecting verbs... |
|
| 75 | + */ |
|
| 76 | + public function match(ServerRequestInterface $request) : RouteInterface |
|
| 77 | + { |
|
| 78 | + $routes = []; |
|
| 79 | + foreach ($this->getRoutes() as $route) { |
|
| 80 | + foreach ($route->getMethods() as $method) { |
|
| 81 | + $routes[$method][] = $route; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $method = $request->getMethod(); |
|
| 86 | + if (! isset($routes[$method])) { |
|
| 87 | + throw new MethodNotAllowedException( |
|
| 88 | + array_keys($routes) |
|
| 89 | + ); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + $target = $request->getUri()->getPath(); |
|
| 93 | + foreach ($routes[$method] as $route) { |
|
| 94 | + if (path_match($route->getPath(), $target, $attributes)) { |
|
| 95 | + return $route->withAddedAttributes($attributes); |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + throw new RouteNotFoundException(); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * {@inheritDoc} |
|
| 104 | + */ |
|
| 105 | + public function handle(ServerRequestInterface $request) : ResponseInterface |
|
| 106 | + { |
|
| 107 | + $route = $this->match($request); |
|
| 108 | + |
|
| 109 | + $requestHandler = new RoutableRequestHandler($route); |
|
| 110 | + |
|
| 111 | + return $requestHandler->handle($request); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * {@inheritDoc} |
|
| 116 | + */ |
|
| 117 | + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
|
| 118 | + { |
|
| 119 | + try { |
|
| 120 | + return $this->handle($request); |
|
| 121 | + } catch (ExceptionInterface $e) { |
|
| 122 | + return $handler->handle( |
|
| 123 | + $request->withAttribute(self::ATTR_NAME_FOR_ROUTING_ERROR, $e) |
|
| 124 | + ); |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | 127 | } |
@@ -83,7 +83,7 @@ |
||
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | $method = $request->getMethod(); |
| 86 | - if (! isset($routes[$method])) { |
|
| 86 | + if (!isset($routes[$method])) { |
|
| 87 | 87 | throw new MethodNotAllowedException( |
| 88 | 88 | array_keys($routes) |
| 89 | 89 | ); |
@@ -23,35 +23,35 @@ |
||
| 23 | 23 | class MethodNotAllowedException extends RuntimeException implements ExceptionInterface |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Allowed HTTP methods |
|
| 28 | - * |
|
| 29 | - * @var string[] |
|
| 30 | - */ |
|
| 31 | - private $allowedMethods; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Constructor of the class |
|
| 35 | - * |
|
| 36 | - * @param string[] $allowedMethods |
|
| 37 | - * @param string $message |
|
| 38 | - * @param int $code |
|
| 39 | - * @param Throwable $previous |
|
| 40 | - */ |
|
| 41 | - public function __construct(array $allowedMethods, string $message = '', int $code = 0, Throwable $previous = null) |
|
| 42 | - { |
|
| 43 | - $this->allowedMethods = $allowedMethods; |
|
| 44 | - |
|
| 45 | - parent::__construct($message, $code, $previous); |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Gets allowed HTTP methods |
|
| 50 | - * |
|
| 51 | - * @return string[] |
|
| 52 | - */ |
|
| 53 | - public function getAllowedMethods() : array |
|
| 54 | - { |
|
| 55 | - return $this->allowedMethods; |
|
| 56 | - } |
|
| 26 | + /** |
|
| 27 | + * Allowed HTTP methods |
|
| 28 | + * |
|
| 29 | + * @var string[] |
|
| 30 | + */ |
|
| 31 | + private $allowedMethods; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Constructor of the class |
|
| 35 | + * |
|
| 36 | + * @param string[] $allowedMethods |
|
| 37 | + * @param string $message |
|
| 38 | + * @param int $code |
|
| 39 | + * @param Throwable $previous |
|
| 40 | + */ |
|
| 41 | + public function __construct(array $allowedMethods, string $message = '', int $code = 0, Throwable $previous = null) |
|
| 42 | + { |
|
| 43 | + $this->allowedMethods = $allowedMethods; |
|
| 44 | + |
|
| 45 | + parent::__construct($message, $code, $previous); |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Gets allowed HTTP methods |
|
| 50 | + * |
|
| 51 | + * @return string[] |
|
| 52 | + */ |
|
| 53 | + public function getAllowedMethods() : array |
|
| 54 | + { |
|
| 55 | + return $this->allowedMethods; |
|
| 56 | + } |
|
| 57 | 57 | } |
@@ -23,205 +23,205 @@ |
||
| 23 | 23 | interface RouteCollectionInterface |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Gets the collection prefix |
|
| 28 | - * |
|
| 29 | - * @return null|string |
|
| 30 | - */ |
|
| 31 | - public function getPrefix() : ?string; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Gets the collection middlewares |
|
| 35 | - * |
|
| 36 | - * @return MiddlewareInterface[] |
|
| 37 | - */ |
|
| 38 | - public function getMiddlewares() : array; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Gets the collection routes |
|
| 42 | - * |
|
| 43 | - * @return RouteInterface[] |
|
| 44 | - */ |
|
| 45 | - public function getRoutes() : array; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Sets the given prefix to the collection |
|
| 49 | - * |
|
| 50 | - * @param string $prefix |
|
| 51 | - * |
|
| 52 | - * @return RouteCollectionInterface |
|
| 53 | - */ |
|
| 54 | - public function setPrefix(string $prefix) : RouteCollectionInterface; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Adds the given middleware(s) to the collection |
|
| 58 | - * |
|
| 59 | - * @param MiddlewareInterface ...$middlewares |
|
| 60 | - * |
|
| 61 | - * @return RouteCollectionInterface |
|
| 62 | - */ |
|
| 63 | - public function addMiddlewares(MiddlewareInterface ...$middlewares) : RouteCollectionInterface; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Adds the given route(s) to the collection |
|
| 67 | - * |
|
| 68 | - * @param RouteInterface ...$routes |
|
| 69 | - * |
|
| 70 | - * @return RouteCollectionInterface |
|
| 71 | - */ |
|
| 72 | - public function addRoutes(RouteInterface ...$routes) : RouteCollectionInterface; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Makes a new route from the given parameters |
|
| 76 | - * |
|
| 77 | - * @param string $name |
|
| 78 | - * @param string $path |
|
| 79 | - * @param string[] $methods |
|
| 80 | - * @param RequestHandlerInterface $requestHandler |
|
| 81 | - * @param MiddlewareInterface[] $middlewares |
|
| 82 | - * @param array $attributes |
|
| 83 | - * |
|
| 84 | - * @return RouteInterface |
|
| 85 | - */ |
|
| 86 | - public function route( |
|
| 87 | - string $name, |
|
| 88 | - string $path, |
|
| 89 | - array $methods, |
|
| 90 | - RequestHandlerInterface $requestHandler, |
|
| 91 | - array $middlewares, |
|
| 92 | - array $attributes |
|
| 93 | - ) : RouteInterface; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Makes a new route that will respond to HEAD requests |
|
| 97 | - * |
|
| 98 | - * @param string $name |
|
| 99 | - * @param string $path |
|
| 100 | - * @param RequestHandlerInterface $requestHandler |
|
| 101 | - * @param MiddlewareInterface[] $middlewares |
|
| 102 | - * @param array $attributes |
|
| 103 | - * |
|
| 104 | - * @return RouteInterface |
|
| 105 | - */ |
|
| 106 | - public function head( |
|
| 107 | - string $name, |
|
| 108 | - string $path, |
|
| 109 | - RequestHandlerInterface $requestHandler, |
|
| 110 | - array $middlewares, |
|
| 111 | - array $attributes |
|
| 112 | - ) : RouteInterface; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Makes a new route that will respond to GET requests |
|
| 116 | - * |
|
| 117 | - * @param string $name |
|
| 118 | - * @param string $path |
|
| 119 | - * @param RequestHandlerInterface $requestHandler |
|
| 120 | - * @param MiddlewareInterface[] $middlewares |
|
| 121 | - * @param array $attributes |
|
| 122 | - * |
|
| 123 | - * @return RouteInterface |
|
| 124 | - */ |
|
| 125 | - public function get( |
|
| 126 | - string $name, |
|
| 127 | - string $path, |
|
| 128 | - RequestHandlerInterface $requestHandler, |
|
| 129 | - array $middlewares, |
|
| 130 | - array $attributes |
|
| 131 | - ) : RouteInterface; |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Makes a new route that will respond to POST requests |
|
| 135 | - * |
|
| 136 | - * @param string $name |
|
| 137 | - * @param string $path |
|
| 138 | - * @param RequestHandlerInterface $requestHandler |
|
| 139 | - * @param MiddlewareInterface[] $middlewares |
|
| 140 | - * @param array $attributes |
|
| 141 | - * |
|
| 142 | - * @return RouteInterface |
|
| 143 | - */ |
|
| 144 | - public function post( |
|
| 145 | - string $name, |
|
| 146 | - string $path, |
|
| 147 | - RequestHandlerInterface $requestHandler, |
|
| 148 | - array $middlewares, |
|
| 149 | - array $attributes |
|
| 150 | - ) : RouteInterface; |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Makes a new route that will respond to PUT requests |
|
| 154 | - * |
|
| 155 | - * @param string $name |
|
| 156 | - * @param string $path |
|
| 157 | - * @param RequestHandlerInterface $requestHandler |
|
| 158 | - * @param MiddlewareInterface[] $middlewares |
|
| 159 | - * @param array $attributes |
|
| 160 | - * |
|
| 161 | - * @return RouteInterface |
|
| 162 | - */ |
|
| 163 | - public function put( |
|
| 164 | - string $name, |
|
| 165 | - string $path, |
|
| 166 | - RequestHandlerInterface $requestHandler, |
|
| 167 | - array $middlewares, |
|
| 168 | - array $attributes |
|
| 169 | - ) : RouteInterface; |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Makes a new route that will respond to PATCH requests |
|
| 173 | - * |
|
| 174 | - * @param string $name |
|
| 175 | - * @param string $path |
|
| 176 | - * @param RequestHandlerInterface $requestHandler |
|
| 177 | - * @param MiddlewareInterface[] $middlewares |
|
| 178 | - * @param array $attributes |
|
| 179 | - * |
|
| 180 | - * @return RouteInterface |
|
| 181 | - */ |
|
| 182 | - public function patch( |
|
| 183 | - string $name, |
|
| 184 | - string $path, |
|
| 185 | - RequestHandlerInterface $requestHandler, |
|
| 186 | - array $middlewares, |
|
| 187 | - array $attributes |
|
| 188 | - ) : RouteInterface; |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Makes a new route that will respond to DELETE requests |
|
| 192 | - * |
|
| 193 | - * @param string $name |
|
| 194 | - * @param string $path |
|
| 195 | - * @param RequestHandlerInterface $requestHandler |
|
| 196 | - * @param MiddlewareInterface[] $middlewares |
|
| 197 | - * @param array $attributes |
|
| 198 | - * |
|
| 199 | - * @return RouteInterface |
|
| 200 | - */ |
|
| 201 | - public function delete( |
|
| 202 | - string $name, |
|
| 203 | - string $path, |
|
| 204 | - RequestHandlerInterface $requestHandler, |
|
| 205 | - array $middlewares, |
|
| 206 | - array $attributes |
|
| 207 | - ) : RouteInterface; |
|
| 208 | - |
|
| 209 | - /** |
|
| 210 | - * Makes a new route that will respond to PURGE requests |
|
| 211 | - * |
|
| 212 | - * @param string $name |
|
| 213 | - * @param string $path |
|
| 214 | - * @param RequestHandlerInterface $requestHandler |
|
| 215 | - * @param MiddlewareInterface[] $middlewares |
|
| 216 | - * @param array $attributes |
|
| 217 | - * |
|
| 218 | - * @return RouteInterface |
|
| 219 | - */ |
|
| 220 | - public function purge( |
|
| 221 | - string $name, |
|
| 222 | - string $path, |
|
| 223 | - RequestHandlerInterface $requestHandler, |
|
| 224 | - array $middlewares, |
|
| 225 | - array $attributes |
|
| 226 | - ) : RouteInterface; |
|
| 26 | + /** |
|
| 27 | + * Gets the collection prefix |
|
| 28 | + * |
|
| 29 | + * @return null|string |
|
| 30 | + */ |
|
| 31 | + public function getPrefix() : ?string; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Gets the collection middlewares |
|
| 35 | + * |
|
| 36 | + * @return MiddlewareInterface[] |
|
| 37 | + */ |
|
| 38 | + public function getMiddlewares() : array; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Gets the collection routes |
|
| 42 | + * |
|
| 43 | + * @return RouteInterface[] |
|
| 44 | + */ |
|
| 45 | + public function getRoutes() : array; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Sets the given prefix to the collection |
|
| 49 | + * |
|
| 50 | + * @param string $prefix |
|
| 51 | + * |
|
| 52 | + * @return RouteCollectionInterface |
|
| 53 | + */ |
|
| 54 | + public function setPrefix(string $prefix) : RouteCollectionInterface; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Adds the given middleware(s) to the collection |
|
| 58 | + * |
|
| 59 | + * @param MiddlewareInterface ...$middlewares |
|
| 60 | + * |
|
| 61 | + * @return RouteCollectionInterface |
|
| 62 | + */ |
|
| 63 | + public function addMiddlewares(MiddlewareInterface ...$middlewares) : RouteCollectionInterface; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Adds the given route(s) to the collection |
|
| 67 | + * |
|
| 68 | + * @param RouteInterface ...$routes |
|
| 69 | + * |
|
| 70 | + * @return RouteCollectionInterface |
|
| 71 | + */ |
|
| 72 | + public function addRoutes(RouteInterface ...$routes) : RouteCollectionInterface; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Makes a new route from the given parameters |
|
| 76 | + * |
|
| 77 | + * @param string $name |
|
| 78 | + * @param string $path |
|
| 79 | + * @param string[] $methods |
|
| 80 | + * @param RequestHandlerInterface $requestHandler |
|
| 81 | + * @param MiddlewareInterface[] $middlewares |
|
| 82 | + * @param array $attributes |
|
| 83 | + * |
|
| 84 | + * @return RouteInterface |
|
| 85 | + */ |
|
| 86 | + public function route( |
|
| 87 | + string $name, |
|
| 88 | + string $path, |
|
| 89 | + array $methods, |
|
| 90 | + RequestHandlerInterface $requestHandler, |
|
| 91 | + array $middlewares, |
|
| 92 | + array $attributes |
|
| 93 | + ) : RouteInterface; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Makes a new route that will respond to HEAD requests |
|
| 97 | + * |
|
| 98 | + * @param string $name |
|
| 99 | + * @param string $path |
|
| 100 | + * @param RequestHandlerInterface $requestHandler |
|
| 101 | + * @param MiddlewareInterface[] $middlewares |
|
| 102 | + * @param array $attributes |
|
| 103 | + * |
|
| 104 | + * @return RouteInterface |
|
| 105 | + */ |
|
| 106 | + public function head( |
|
| 107 | + string $name, |
|
| 108 | + string $path, |
|
| 109 | + RequestHandlerInterface $requestHandler, |
|
| 110 | + array $middlewares, |
|
| 111 | + array $attributes |
|
| 112 | + ) : RouteInterface; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Makes a new route that will respond to GET requests |
|
| 116 | + * |
|
| 117 | + * @param string $name |
|
| 118 | + * @param string $path |
|
| 119 | + * @param RequestHandlerInterface $requestHandler |
|
| 120 | + * @param MiddlewareInterface[] $middlewares |
|
| 121 | + * @param array $attributes |
|
| 122 | + * |
|
| 123 | + * @return RouteInterface |
|
| 124 | + */ |
|
| 125 | + public function get( |
|
| 126 | + string $name, |
|
| 127 | + string $path, |
|
| 128 | + RequestHandlerInterface $requestHandler, |
|
| 129 | + array $middlewares, |
|
| 130 | + array $attributes |
|
| 131 | + ) : RouteInterface; |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Makes a new route that will respond to POST requests |
|
| 135 | + * |
|
| 136 | + * @param string $name |
|
| 137 | + * @param string $path |
|
| 138 | + * @param RequestHandlerInterface $requestHandler |
|
| 139 | + * @param MiddlewareInterface[] $middlewares |
|
| 140 | + * @param array $attributes |
|
| 141 | + * |
|
| 142 | + * @return RouteInterface |
|
| 143 | + */ |
|
| 144 | + public function post( |
|
| 145 | + string $name, |
|
| 146 | + string $path, |
|
| 147 | + RequestHandlerInterface $requestHandler, |
|
| 148 | + array $middlewares, |
|
| 149 | + array $attributes |
|
| 150 | + ) : RouteInterface; |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Makes a new route that will respond to PUT requests |
|
| 154 | + * |
|
| 155 | + * @param string $name |
|
| 156 | + * @param string $path |
|
| 157 | + * @param RequestHandlerInterface $requestHandler |
|
| 158 | + * @param MiddlewareInterface[] $middlewares |
|
| 159 | + * @param array $attributes |
|
| 160 | + * |
|
| 161 | + * @return RouteInterface |
|
| 162 | + */ |
|
| 163 | + public function put( |
|
| 164 | + string $name, |
|
| 165 | + string $path, |
|
| 166 | + RequestHandlerInterface $requestHandler, |
|
| 167 | + array $middlewares, |
|
| 168 | + array $attributes |
|
| 169 | + ) : RouteInterface; |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Makes a new route that will respond to PATCH requests |
|
| 173 | + * |
|
| 174 | + * @param string $name |
|
| 175 | + * @param string $path |
|
| 176 | + * @param RequestHandlerInterface $requestHandler |
|
| 177 | + * @param MiddlewareInterface[] $middlewares |
|
| 178 | + * @param array $attributes |
|
| 179 | + * |
|
| 180 | + * @return RouteInterface |
|
| 181 | + */ |
|
| 182 | + public function patch( |
|
| 183 | + string $name, |
|
| 184 | + string $path, |
|
| 185 | + RequestHandlerInterface $requestHandler, |
|
| 186 | + array $middlewares, |
|
| 187 | + array $attributes |
|
| 188 | + ) : RouteInterface; |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Makes a new route that will respond to DELETE requests |
|
| 192 | + * |
|
| 193 | + * @param string $name |
|
| 194 | + * @param string $path |
|
| 195 | + * @param RequestHandlerInterface $requestHandler |
|
| 196 | + * @param MiddlewareInterface[] $middlewares |
|
| 197 | + * @param array $attributes |
|
| 198 | + * |
|
| 199 | + * @return RouteInterface |
|
| 200 | + */ |
|
| 201 | + public function delete( |
|
| 202 | + string $name, |
|
| 203 | + string $path, |
|
| 204 | + RequestHandlerInterface $requestHandler, |
|
| 205 | + array $middlewares, |
|
| 206 | + array $attributes |
|
| 207 | + ) : RouteInterface; |
|
| 208 | + |
|
| 209 | + /** |
|
| 210 | + * Makes a new route that will respond to PURGE requests |
|
| 211 | + * |
|
| 212 | + * @param string $name |
|
| 213 | + * @param string $path |
|
| 214 | + * @param RequestHandlerInterface $requestHandler |
|
| 215 | + * @param MiddlewareInterface[] $middlewares |
|
| 216 | + * @param array $attributes |
|
| 217 | + * |
|
| 218 | + * @return RouteInterface |
|
| 219 | + */ |
|
| 220 | + public function purge( |
|
| 221 | + string $name, |
|
| 222 | + string $path, |
|
| 223 | + RequestHandlerInterface $requestHandler, |
|
| 224 | + array $middlewares, |
|
| 225 | + array $attributes |
|
| 226 | + ) : RouteInterface; |
|
| 227 | 227 | } |
@@ -29,259 +29,259 @@ |
||
| 29 | 29 | class RouteCollection implements RouteCollectionInterface |
| 30 | 30 | { |
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * The collection prefix |
|
| 34 | - * |
|
| 35 | - * @var null|string |
|
| 36 | - */ |
|
| 37 | - private $prefix; |
|
| 32 | + /** |
|
| 33 | + * The collection prefix |
|
| 34 | + * |
|
| 35 | + * @var null|string |
|
| 36 | + */ |
|
| 37 | + private $prefix; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * The collection middlewares |
|
| 41 | - * |
|
| 42 | - * @var MiddlewareInterface[] |
|
| 43 | - */ |
|
| 44 | - private $middlewares = []; |
|
| 39 | + /** |
|
| 40 | + * The collection middlewares |
|
| 41 | + * |
|
| 42 | + * @var MiddlewareInterface[] |
|
| 43 | + */ |
|
| 44 | + private $middlewares = []; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * The collection routes |
|
| 48 | - * |
|
| 49 | - * @var RouteInterface[] |
|
| 50 | - */ |
|
| 51 | - private $routes = []; |
|
| 46 | + /** |
|
| 47 | + * The collection routes |
|
| 48 | + * |
|
| 49 | + * @var RouteInterface[] |
|
| 50 | + */ |
|
| 51 | + private $routes = []; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * {@inheritDoc} |
|
| 55 | - */ |
|
| 56 | - public function getPrefix() : ?string |
|
| 57 | - { |
|
| 58 | - return $this->prefix; |
|
| 59 | - } |
|
| 53 | + /** |
|
| 54 | + * {@inheritDoc} |
|
| 55 | + */ |
|
| 56 | + public function getPrefix() : ?string |
|
| 57 | + { |
|
| 58 | + return $this->prefix; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * {@inheritDoc} |
|
| 63 | - */ |
|
| 64 | - public function getMiddlewares() : array |
|
| 65 | - { |
|
| 66 | - return $this->middlewares; |
|
| 67 | - } |
|
| 61 | + /** |
|
| 62 | + * {@inheritDoc} |
|
| 63 | + */ |
|
| 64 | + public function getMiddlewares() : array |
|
| 65 | + { |
|
| 66 | + return $this->middlewares; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * {@inheritDoc} |
|
| 71 | - */ |
|
| 72 | - public function getRoutes() : array |
|
| 73 | - { |
|
| 74 | - return $this->routes; |
|
| 75 | - } |
|
| 69 | + /** |
|
| 70 | + * {@inheritDoc} |
|
| 71 | + */ |
|
| 72 | + public function getRoutes() : array |
|
| 73 | + { |
|
| 74 | + return $this->routes; |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * {@inheritDoc} |
|
| 79 | - */ |
|
| 80 | - public function setPrefix(string $prefix) : RouteCollectionInterface |
|
| 81 | - { |
|
| 82 | - // https://github.com/sunrise-php/http-router/issues/26 |
|
| 83 | - $prefix = rtrim($prefix, '/'); |
|
| 77 | + /** |
|
| 78 | + * {@inheritDoc} |
|
| 79 | + */ |
|
| 80 | + public function setPrefix(string $prefix) : RouteCollectionInterface |
|
| 81 | + { |
|
| 82 | + // https://github.com/sunrise-php/http-router/issues/26 |
|
| 83 | + $prefix = rtrim($prefix, '/'); |
|
| 84 | 84 | |
| 85 | - $this->prefix = $prefix; |
|
| 85 | + $this->prefix = $prefix; |
|
| 86 | 86 | |
| 87 | - return $this; |
|
| 88 | - } |
|
| 87 | + return $this; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * {@inheritDoc} |
|
| 92 | - */ |
|
| 93 | - public function addMiddlewares(MiddlewareInterface ...$middlewares) : RouteCollectionInterface |
|
| 94 | - { |
|
| 95 | - foreach ($middlewares as $middleware) { |
|
| 96 | - $this->middlewares[] = $middleware; |
|
| 97 | - } |
|
| 90 | + /** |
|
| 91 | + * {@inheritDoc} |
|
| 92 | + */ |
|
| 93 | + public function addMiddlewares(MiddlewareInterface ...$middlewares) : RouteCollectionInterface |
|
| 94 | + { |
|
| 95 | + foreach ($middlewares as $middleware) { |
|
| 96 | + $this->middlewares[] = $middleware; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - return $this; |
|
| 100 | - } |
|
| 99 | + return $this; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * {@inheritDoc} |
|
| 104 | - */ |
|
| 105 | - public function addRoutes(RouteInterface ...$routes) : RouteCollectionInterface |
|
| 106 | - { |
|
| 107 | - foreach ($routes as $route) { |
|
| 108 | - $this->routes[] = $route; |
|
| 109 | - } |
|
| 102 | + /** |
|
| 103 | + * {@inheritDoc} |
|
| 104 | + */ |
|
| 105 | + public function addRoutes(RouteInterface ...$routes) : RouteCollectionInterface |
|
| 106 | + { |
|
| 107 | + foreach ($routes as $route) { |
|
| 108 | + $this->routes[] = $route; |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - return $this; |
|
| 112 | - } |
|
| 111 | + return $this; |
|
| 112 | + } |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * {@inheritDoc} |
|
| 116 | - * |
|
| 117 | - * @todo Maybe create a route factory? |
|
| 118 | - */ |
|
| 119 | - public function route( |
|
| 120 | - string $name, |
|
| 121 | - string $path, |
|
| 122 | - array $methods, |
|
| 123 | - RequestHandlerInterface $requestHandler, |
|
| 124 | - array $middlewares = [], |
|
| 125 | - array $attributes = [] |
|
| 126 | - ) : RouteInterface { |
|
| 127 | - $path = $this->prefix . $path; |
|
| 114 | + /** |
|
| 115 | + * {@inheritDoc} |
|
| 116 | + * |
|
| 117 | + * @todo Maybe create a route factory? |
|
| 118 | + */ |
|
| 119 | + public function route( |
|
| 120 | + string $name, |
|
| 121 | + string $path, |
|
| 122 | + array $methods, |
|
| 123 | + RequestHandlerInterface $requestHandler, |
|
| 124 | + array $middlewares = [], |
|
| 125 | + array $attributes = [] |
|
| 126 | + ) : RouteInterface { |
|
| 127 | + $path = $this->prefix . $path; |
|
| 128 | 128 | |
| 129 | - $middlewares = array_merge( |
|
| 130 | - $this->middlewares, |
|
| 131 | - $middlewares |
|
| 132 | - ); |
|
| 129 | + $middlewares = array_merge( |
|
| 130 | + $this->middlewares, |
|
| 131 | + $middlewares |
|
| 132 | + ); |
|
| 133 | 133 | |
| 134 | - $route = new Route( |
|
| 135 | - $name, |
|
| 136 | - $path, |
|
| 137 | - $methods, |
|
| 138 | - $requestHandler, |
|
| 139 | - $middlewares, |
|
| 140 | - $attributes |
|
| 141 | - ); |
|
| 134 | + $route = new Route( |
|
| 135 | + $name, |
|
| 136 | + $path, |
|
| 137 | + $methods, |
|
| 138 | + $requestHandler, |
|
| 139 | + $middlewares, |
|
| 140 | + $attributes |
|
| 141 | + ); |
|
| 142 | 142 | |
| 143 | - $this->addRoutes($route); |
|
| 143 | + $this->addRoutes($route); |
|
| 144 | 144 | |
| 145 | - return $route; |
|
| 146 | - } |
|
| 145 | + return $route; |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | - /** |
|
| 149 | - * {@inheritDoc} |
|
| 150 | - */ |
|
| 151 | - public function head( |
|
| 152 | - string $name, |
|
| 153 | - string $path, |
|
| 154 | - RequestHandlerInterface $requestHandler, |
|
| 155 | - array $middlewares = [], |
|
| 156 | - array $attributes = [] |
|
| 157 | - ) : RouteInterface { |
|
| 158 | - return $this->route( |
|
| 159 | - $name, |
|
| 160 | - $path, |
|
| 161 | - ['HEAD'], |
|
| 162 | - $requestHandler, |
|
| 163 | - $middlewares, |
|
| 164 | - $attributes |
|
| 165 | - ); |
|
| 166 | - } |
|
| 148 | + /** |
|
| 149 | + * {@inheritDoc} |
|
| 150 | + */ |
|
| 151 | + public function head( |
|
| 152 | + string $name, |
|
| 153 | + string $path, |
|
| 154 | + RequestHandlerInterface $requestHandler, |
|
| 155 | + array $middlewares = [], |
|
| 156 | + array $attributes = [] |
|
| 157 | + ) : RouteInterface { |
|
| 158 | + return $this->route( |
|
| 159 | + $name, |
|
| 160 | + $path, |
|
| 161 | + ['HEAD'], |
|
| 162 | + $requestHandler, |
|
| 163 | + $middlewares, |
|
| 164 | + $attributes |
|
| 165 | + ); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * {@inheritDoc} |
|
| 170 | - */ |
|
| 171 | - public function get( |
|
| 172 | - string $name, |
|
| 173 | - string $path, |
|
| 174 | - RequestHandlerInterface $requestHandler, |
|
| 175 | - array $middlewares = [], |
|
| 176 | - array $attributes = [] |
|
| 177 | - ) : RouteInterface { |
|
| 178 | - return $this->route( |
|
| 179 | - $name, |
|
| 180 | - $path, |
|
| 181 | - ['GET'], |
|
| 182 | - $requestHandler, |
|
| 183 | - $middlewares, |
|
| 184 | - $attributes |
|
| 185 | - ); |
|
| 186 | - } |
|
| 168 | + /** |
|
| 169 | + * {@inheritDoc} |
|
| 170 | + */ |
|
| 171 | + public function get( |
|
| 172 | + string $name, |
|
| 173 | + string $path, |
|
| 174 | + RequestHandlerInterface $requestHandler, |
|
| 175 | + array $middlewares = [], |
|
| 176 | + array $attributes = [] |
|
| 177 | + ) : RouteInterface { |
|
| 178 | + return $this->route( |
|
| 179 | + $name, |
|
| 180 | + $path, |
|
| 181 | + ['GET'], |
|
| 182 | + $requestHandler, |
|
| 183 | + $middlewares, |
|
| 184 | + $attributes |
|
| 185 | + ); |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * {@inheritDoc} |
|
| 190 | - */ |
|
| 191 | - public function post( |
|
| 192 | - string $name, |
|
| 193 | - string $path, |
|
| 194 | - RequestHandlerInterface $requestHandler, |
|
| 195 | - array $middlewares = [], |
|
| 196 | - array $attributes = [] |
|
| 197 | - ) : RouteInterface { |
|
| 198 | - return $this->route( |
|
| 199 | - $name, |
|
| 200 | - $path, |
|
| 201 | - ['POST'], |
|
| 202 | - $requestHandler, |
|
| 203 | - $middlewares, |
|
| 204 | - $attributes |
|
| 205 | - ); |
|
| 206 | - } |
|
| 188 | + /** |
|
| 189 | + * {@inheritDoc} |
|
| 190 | + */ |
|
| 191 | + public function post( |
|
| 192 | + string $name, |
|
| 193 | + string $path, |
|
| 194 | + RequestHandlerInterface $requestHandler, |
|
| 195 | + array $middlewares = [], |
|
| 196 | + array $attributes = [] |
|
| 197 | + ) : RouteInterface { |
|
| 198 | + return $this->route( |
|
| 199 | + $name, |
|
| 200 | + $path, |
|
| 201 | + ['POST'], |
|
| 202 | + $requestHandler, |
|
| 203 | + $middlewares, |
|
| 204 | + $attributes |
|
| 205 | + ); |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | - /** |
|
| 209 | - * {@inheritDoc} |
|
| 210 | - */ |
|
| 211 | - public function put( |
|
| 212 | - string $name, |
|
| 213 | - string $path, |
|
| 214 | - RequestHandlerInterface $requestHandler, |
|
| 215 | - array $middlewares = [], |
|
| 216 | - array $attributes = [] |
|
| 217 | - ) : RouteInterface { |
|
| 218 | - return $this->route( |
|
| 219 | - $name, |
|
| 220 | - $path, |
|
| 221 | - ['PUT'], |
|
| 222 | - $requestHandler, |
|
| 223 | - $middlewares, |
|
| 224 | - $attributes |
|
| 225 | - ); |
|
| 226 | - } |
|
| 208 | + /** |
|
| 209 | + * {@inheritDoc} |
|
| 210 | + */ |
|
| 211 | + public function put( |
|
| 212 | + string $name, |
|
| 213 | + string $path, |
|
| 214 | + RequestHandlerInterface $requestHandler, |
|
| 215 | + array $middlewares = [], |
|
| 216 | + array $attributes = [] |
|
| 217 | + ) : RouteInterface { |
|
| 218 | + return $this->route( |
|
| 219 | + $name, |
|
| 220 | + $path, |
|
| 221 | + ['PUT'], |
|
| 222 | + $requestHandler, |
|
| 223 | + $middlewares, |
|
| 224 | + $attributes |
|
| 225 | + ); |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - /** |
|
| 229 | - * {@inheritDoc} |
|
| 230 | - */ |
|
| 231 | - public function patch( |
|
| 232 | - string $name, |
|
| 233 | - string $path, |
|
| 234 | - RequestHandlerInterface $requestHandler, |
|
| 235 | - array $middlewares = [], |
|
| 236 | - array $attributes = [] |
|
| 237 | - ) : RouteInterface { |
|
| 238 | - return $this->route( |
|
| 239 | - $name, |
|
| 240 | - $path, |
|
| 241 | - ['PATCH'], |
|
| 242 | - $requestHandler, |
|
| 243 | - $middlewares, |
|
| 244 | - $attributes |
|
| 245 | - ); |
|
| 246 | - } |
|
| 228 | + /** |
|
| 229 | + * {@inheritDoc} |
|
| 230 | + */ |
|
| 231 | + public function patch( |
|
| 232 | + string $name, |
|
| 233 | + string $path, |
|
| 234 | + RequestHandlerInterface $requestHandler, |
|
| 235 | + array $middlewares = [], |
|
| 236 | + array $attributes = [] |
|
| 237 | + ) : RouteInterface { |
|
| 238 | + return $this->route( |
|
| 239 | + $name, |
|
| 240 | + $path, |
|
| 241 | + ['PATCH'], |
|
| 242 | + $requestHandler, |
|
| 243 | + $middlewares, |
|
| 244 | + $attributes |
|
| 245 | + ); |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - /** |
|
| 249 | - * {@inheritDoc} |
|
| 250 | - */ |
|
| 251 | - public function delete( |
|
| 252 | - string $name, |
|
| 253 | - string $path, |
|
| 254 | - RequestHandlerInterface $requestHandler, |
|
| 255 | - array $middlewares = [], |
|
| 256 | - array $attributes = [] |
|
| 257 | - ) : RouteInterface { |
|
| 258 | - return $this->route( |
|
| 259 | - $name, |
|
| 260 | - $path, |
|
| 261 | - ['DELETE'], |
|
| 262 | - $requestHandler, |
|
| 263 | - $middlewares, |
|
| 264 | - $attributes |
|
| 265 | - ); |
|
| 266 | - } |
|
| 248 | + /** |
|
| 249 | + * {@inheritDoc} |
|
| 250 | + */ |
|
| 251 | + public function delete( |
|
| 252 | + string $name, |
|
| 253 | + string $path, |
|
| 254 | + RequestHandlerInterface $requestHandler, |
|
| 255 | + array $middlewares = [], |
|
| 256 | + array $attributes = [] |
|
| 257 | + ) : RouteInterface { |
|
| 258 | + return $this->route( |
|
| 259 | + $name, |
|
| 260 | + $path, |
|
| 261 | + ['DELETE'], |
|
| 262 | + $requestHandler, |
|
| 263 | + $middlewares, |
|
| 264 | + $attributes |
|
| 265 | + ); |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - /** |
|
| 269 | - * {@inheritDoc} |
|
| 270 | - */ |
|
| 271 | - public function purge( |
|
| 272 | - string $name, |
|
| 273 | - string $path, |
|
| 274 | - RequestHandlerInterface $requestHandler, |
|
| 275 | - array $middlewares = [], |
|
| 276 | - array $attributes = [] |
|
| 277 | - ) : RouteInterface { |
|
| 278 | - return $this->route( |
|
| 279 | - $name, |
|
| 280 | - $path, |
|
| 281 | - ['PURGE'], |
|
| 282 | - $requestHandler, |
|
| 283 | - $middlewares, |
|
| 284 | - $attributes |
|
| 285 | - ); |
|
| 286 | - } |
|
| 268 | + /** |
|
| 269 | + * {@inheritDoc} |
|
| 270 | + */ |
|
| 271 | + public function purge( |
|
| 272 | + string $name, |
|
| 273 | + string $path, |
|
| 274 | + RequestHandlerInterface $requestHandler, |
|
| 275 | + array $middlewares = [], |
|
| 276 | + array $attributes = [] |
|
| 277 | + ) : RouteInterface { |
|
| 278 | + return $this->route( |
|
| 279 | + $name, |
|
| 280 | + $path, |
|
| 281 | + ['PURGE'], |
|
| 282 | + $requestHandler, |
|
| 283 | + $middlewares, |
|
| 284 | + $attributes |
|
| 285 | + ); |
|
| 286 | + } |
|
| 287 | 287 | } |