@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | { |
| 86 | 86 | $attributes = \array_filter($matches, function($value, $name) |
| 87 | 87 | { |
| 88 | - return ! ('' === $value || \is_int($name)); |
|
| 88 | + return !('' === $value || \is_int($name)); |
|
| 89 | 89 | |
| 90 | 90 | }, \ARRAY_FILTER_USE_BOTH); |
| 91 | 91 | |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | } |
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - if (! empty($allowed)) |
|
| 97 | + if (!empty($allowed)) |
|
| 98 | 98 | { |
| 99 | 99 | throw new MethodNotAllowedException($request, $allowed); |
| 100 | 100 | } |
@@ -26,131 +26,131 @@ |
||
| 26 | 26 | class Router implements RouterInterface |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * The router map |
|
| 31 | - * |
|
| 32 | - * @var RouteInterface[] |
|
| 33 | - */ |
|
| 34 | - protected $routes = []; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * The router middleware stack |
|
| 38 | - * |
|
| 39 | - * @var MiddlewareInterface[] |
|
| 40 | - */ |
|
| 41 | - protected $middlewareStack = []; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * {@inheritDoc} |
|
| 45 | - */ |
|
| 46 | - public function addRoute(RouteInterface $route) : RouterInterface |
|
| 47 | - { |
|
| 48 | - $this->routes[] = $route; |
|
| 49 | - |
|
| 50 | - return $this; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * {@inheritDoc} |
|
| 55 | - */ |
|
| 56 | - public function addRoutes(RouteCollectionInterface $collection) : RouterInterface |
|
| 57 | - { |
|
| 58 | - foreach ($collection->getRoutes() as $route) |
|
| 59 | - { |
|
| 60 | - $this->addRoute($route); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - return $this; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * {@inheritDoc} |
|
| 68 | - */ |
|
| 69 | - public function addMiddleware(MiddlewareInterface $middleware) : RouterInterface |
|
| 70 | - { |
|
| 71 | - $this->middlewareStack[] = $middleware; |
|
| 72 | - |
|
| 73 | - return $this; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * {@inheritDoc} |
|
| 78 | - */ |
|
| 79 | - public function getRoutes() : array |
|
| 80 | - { |
|
| 81 | - return $this->routes; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * {@inheritDoc} |
|
| 86 | - */ |
|
| 87 | - public function getMiddlewareStack() : array |
|
| 88 | - { |
|
| 89 | - return $this->middlewareStack; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * {@inheritDoc} |
|
| 94 | - */ |
|
| 95 | - public function match(ServerRequestInterface $request) : RouteInterface |
|
| 96 | - { |
|
| 97 | - $allowed = []; |
|
| 98 | - |
|
| 99 | - foreach ($this->getRoutes() as $route) |
|
| 100 | - { |
|
| 101 | - $regex = $route->buildRegex(); |
|
| 102 | - |
|
| 103 | - if (\preg_match($regex, $request->getUri()->getPath(), $matches)) |
|
| 104 | - { |
|
| 105 | - $allowed = \array_merge($allowed, $route->getMethods()); |
|
| 106 | - |
|
| 107 | - if (\in_array($request->getMethod(), $route->getMethods())) |
|
| 108 | - { |
|
| 109 | - $attributes = \array_filter($matches, function($value, $name) |
|
| 110 | - { |
|
| 111 | - return ! ('' === $value || \is_int($name)); |
|
| 112 | - |
|
| 113 | - }, \ARRAY_FILTER_USE_BOTH); |
|
| 114 | - |
|
| 115 | - return $route->withAttributes($attributes); |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - if (! empty($allowed)) |
|
| 121 | - { |
|
| 122 | - throw new MethodNotAllowedException($request, $allowed); |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - throw new RouteNotFoundException($request); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * {@inheritDoc} |
|
| 130 | - */ |
|
| 131 | - public function handle(ServerRequestInterface $request) : ResponseInterface |
|
| 132 | - { |
|
| 133 | - $route = $this->match($request); |
|
| 134 | - |
|
| 135 | - $requestHandler = new RequestHandler(); |
|
| 136 | - |
|
| 137 | - foreach ($this->getMiddlewareStack() as $middleware) |
|
| 138 | - { |
|
| 139 | - $requestHandler->add($middleware); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - foreach ($route->getMiddlewareStack() as $middleware) |
|
| 143 | - { |
|
| 144 | - $requestHandler->add($middleware); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - foreach ($route->getAttributes() as $name => $value) |
|
| 148 | - { |
|
| 149 | - $request = $request->withAttribute($name, $value); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - $request = $request->withAttribute('@route', $route->getId()); |
|
| 153 | - |
|
| 154 | - return $requestHandler->handle($request); |
|
| 155 | - } |
|
| 29 | + /** |
|
| 30 | + * The router map |
|
| 31 | + * |
|
| 32 | + * @var RouteInterface[] |
|
| 33 | + */ |
|
| 34 | + protected $routes = []; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * The router middleware stack |
|
| 38 | + * |
|
| 39 | + * @var MiddlewareInterface[] |
|
| 40 | + */ |
|
| 41 | + protected $middlewareStack = []; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * {@inheritDoc} |
|
| 45 | + */ |
|
| 46 | + public function addRoute(RouteInterface $route) : RouterInterface |
|
| 47 | + { |
|
| 48 | + $this->routes[] = $route; |
|
| 49 | + |
|
| 50 | + return $this; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * {@inheritDoc} |
|
| 55 | + */ |
|
| 56 | + public function addRoutes(RouteCollectionInterface $collection) : RouterInterface |
|
| 57 | + { |
|
| 58 | + foreach ($collection->getRoutes() as $route) |
|
| 59 | + { |
|
| 60 | + $this->addRoute($route); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + return $this; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * {@inheritDoc} |
|
| 68 | + */ |
|
| 69 | + public function addMiddleware(MiddlewareInterface $middleware) : RouterInterface |
|
| 70 | + { |
|
| 71 | + $this->middlewareStack[] = $middleware; |
|
| 72 | + |
|
| 73 | + return $this; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * {@inheritDoc} |
|
| 78 | + */ |
|
| 79 | + public function getRoutes() : array |
|
| 80 | + { |
|
| 81 | + return $this->routes; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * {@inheritDoc} |
|
| 86 | + */ |
|
| 87 | + public function getMiddlewareStack() : array |
|
| 88 | + { |
|
| 89 | + return $this->middlewareStack; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * {@inheritDoc} |
|
| 94 | + */ |
|
| 95 | + public function match(ServerRequestInterface $request) : RouteInterface |
|
| 96 | + { |
|
| 97 | + $allowed = []; |
|
| 98 | + |
|
| 99 | + foreach ($this->getRoutes() as $route) |
|
| 100 | + { |
|
| 101 | + $regex = $route->buildRegex(); |
|
| 102 | + |
|
| 103 | + if (\preg_match($regex, $request->getUri()->getPath(), $matches)) |
|
| 104 | + { |
|
| 105 | + $allowed = \array_merge($allowed, $route->getMethods()); |
|
| 106 | + |
|
| 107 | + if (\in_array($request->getMethod(), $route->getMethods())) |
|
| 108 | + { |
|
| 109 | + $attributes = \array_filter($matches, function($value, $name) |
|
| 110 | + { |
|
| 111 | + return ! ('' === $value || \is_int($name)); |
|
| 112 | + |
|
| 113 | + }, \ARRAY_FILTER_USE_BOTH); |
|
| 114 | + |
|
| 115 | + return $route->withAttributes($attributes); |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + if (! empty($allowed)) |
|
| 121 | + { |
|
| 122 | + throw new MethodNotAllowedException($request, $allowed); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + throw new RouteNotFoundException($request); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * {@inheritDoc} |
|
| 130 | + */ |
|
| 131 | + public function handle(ServerRequestInterface $request) : ResponseInterface |
|
| 132 | + { |
|
| 133 | + $route = $this->match($request); |
|
| 134 | + |
|
| 135 | + $requestHandler = new RequestHandler(); |
|
| 136 | + |
|
| 137 | + foreach ($this->getMiddlewareStack() as $middleware) |
|
| 138 | + { |
|
| 139 | + $requestHandler->add($middleware); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + foreach ($route->getMiddlewareStack() as $middleware) |
|
| 143 | + { |
|
| 144 | + $requestHandler->add($middleware); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + foreach ($route->getAttributes() as $name => $value) |
|
| 148 | + { |
|
| 149 | + $request = $request->withAttribute($name, $value); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + $request = $request->withAttribute('@route', $route->getId()); |
|
| 153 | + |
|
| 154 | + return $requestHandler->handle($request); |
|
| 155 | + } |
|
| 156 | 156 | } |
@@ -22,202 +22,202 @@ |
||
| 22 | 22 | class Route implements RouteInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * The route ID |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - protected $id; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * The route path |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - protected $path; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * The route methods |
|
| 41 | - * |
|
| 42 | - * @var string[] |
|
| 43 | - */ |
|
| 44 | - protected $methods = []; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * The route patterns |
|
| 48 | - * |
|
| 49 | - * @var array |
|
| 50 | - */ |
|
| 51 | - protected $patterns = []; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * The route attributes |
|
| 55 | - * |
|
| 56 | - * @var array |
|
| 57 | - */ |
|
| 58 | - protected $attributes = []; |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * The route middleware stack |
|
| 62 | - * |
|
| 63 | - * @var MiddlewareInterface[] |
|
| 64 | - */ |
|
| 65 | - protected $middlewareStack = []; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Constructor of the class |
|
| 69 | - * |
|
| 70 | - * @param string $id |
|
| 71 | - * @param string $path |
|
| 72 | - * @param string[] $methods |
|
| 73 | - */ |
|
| 74 | - public function __construct(string $id, string $path, array $methods) |
|
| 75 | - { |
|
| 76 | - $this->setId($id); |
|
| 77 | - |
|
| 78 | - $this->setPath($path); |
|
| 79 | - |
|
| 80 | - foreach ($methods as $method) |
|
| 81 | - { |
|
| 82 | - $this->addMethod($method); |
|
| 83 | - } |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * {@inheritDoc} |
|
| 88 | - */ |
|
| 89 | - public function setId(string $id) : RouteInterface |
|
| 90 | - { |
|
| 91 | - $this->id = $id; |
|
| 92 | - |
|
| 93 | - return $this; |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * {@inheritDoc} |
|
| 98 | - */ |
|
| 99 | - public function setPath(string $path) : RouteInterface |
|
| 100 | - { |
|
| 101 | - $this->path = $path; |
|
| 102 | - |
|
| 103 | - return $this; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * {@inheritDoc} |
|
| 108 | - */ |
|
| 109 | - public function addPrefix(string $prefix) : RouteInterface |
|
| 110 | - { |
|
| 111 | - $this->path = $prefix . $this->path; |
|
| 112 | - |
|
| 113 | - return $this; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * {@inheritDoc} |
|
| 118 | - */ |
|
| 119 | - public function addSuffix(string $suffix) : RouteInterface |
|
| 120 | - { |
|
| 121 | - $this->path .= $suffix; |
|
| 122 | - |
|
| 123 | - return $this; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * {@inheritDoc} |
|
| 128 | - */ |
|
| 129 | - public function addMethod(string $method) : RouteInterface |
|
| 130 | - { |
|
| 131 | - $this->methods[] = \strtoupper($method); |
|
| 132 | - |
|
| 133 | - return $this; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * {@inheritDoc} |
|
| 138 | - */ |
|
| 139 | - public function addPattern(string $name, string $value) : RouteInterface |
|
| 140 | - { |
|
| 141 | - $this->patterns[$name] = $value; |
|
| 142 | - |
|
| 143 | - return $this; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * {@inheritDoc} |
|
| 148 | - */ |
|
| 149 | - public function addMiddleware(MiddlewareInterface $middleware) : RouteInterface |
|
| 150 | - { |
|
| 151 | - $this->middlewareStack[] = $middleware; |
|
| 152 | - |
|
| 153 | - return $this; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * {@inheritDoc} |
|
| 158 | - */ |
|
| 159 | - public function getId() : string |
|
| 160 | - { |
|
| 161 | - return $this->id; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * {@inheritDoc} |
|
| 166 | - */ |
|
| 167 | - public function getPath() : string |
|
| 168 | - { |
|
| 169 | - return $this->path; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * {@inheritDoc} |
|
| 174 | - */ |
|
| 175 | - public function getMethods() : array |
|
| 176 | - { |
|
| 177 | - return $this->methods; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * {@inheritDoc} |
|
| 182 | - */ |
|
| 183 | - public function getPatterns() : array |
|
| 184 | - { |
|
| 185 | - return $this->patterns; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * {@inheritDoc} |
|
| 190 | - */ |
|
| 191 | - public function getAttributes() : array |
|
| 192 | - { |
|
| 193 | - return $this->attributes; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * {@inheritDoc} |
|
| 198 | - */ |
|
| 199 | - public function getMiddlewareStack() : array |
|
| 200 | - { |
|
| 201 | - return $this->middlewareStack; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * {@inheritDoc} |
|
| 206 | - */ |
|
| 207 | - public function withAttributes(array $attributes) : RouteInterface |
|
| 208 | - { |
|
| 209 | - $clone = clone $this; |
|
| 210 | - |
|
| 211 | - $clone->attributes = \array_merge($clone->attributes, $attributes); |
|
| 212 | - |
|
| 213 | - return $clone; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * {@inheritDoc} |
|
| 218 | - */ |
|
| 219 | - public function buildRegex() : string |
|
| 220 | - { |
|
| 221 | - return route_regex($this->path, $this->patterns); |
|
| 222 | - } |
|
| 25 | + /** |
|
| 26 | + * The route ID |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + protected $id; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * The route path |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + protected $path; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * The route methods |
|
| 41 | + * |
|
| 42 | + * @var string[] |
|
| 43 | + */ |
|
| 44 | + protected $methods = []; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * The route patterns |
|
| 48 | + * |
|
| 49 | + * @var array |
|
| 50 | + */ |
|
| 51 | + protected $patterns = []; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * The route attributes |
|
| 55 | + * |
|
| 56 | + * @var array |
|
| 57 | + */ |
|
| 58 | + protected $attributes = []; |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * The route middleware stack |
|
| 62 | + * |
|
| 63 | + * @var MiddlewareInterface[] |
|
| 64 | + */ |
|
| 65 | + protected $middlewareStack = []; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Constructor of the class |
|
| 69 | + * |
|
| 70 | + * @param string $id |
|
| 71 | + * @param string $path |
|
| 72 | + * @param string[] $methods |
|
| 73 | + */ |
|
| 74 | + public function __construct(string $id, string $path, array $methods) |
|
| 75 | + { |
|
| 76 | + $this->setId($id); |
|
| 77 | + |
|
| 78 | + $this->setPath($path); |
|
| 79 | + |
|
| 80 | + foreach ($methods as $method) |
|
| 81 | + { |
|
| 82 | + $this->addMethod($method); |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * {@inheritDoc} |
|
| 88 | + */ |
|
| 89 | + public function setId(string $id) : RouteInterface |
|
| 90 | + { |
|
| 91 | + $this->id = $id; |
|
| 92 | + |
|
| 93 | + return $this; |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * {@inheritDoc} |
|
| 98 | + */ |
|
| 99 | + public function setPath(string $path) : RouteInterface |
|
| 100 | + { |
|
| 101 | + $this->path = $path; |
|
| 102 | + |
|
| 103 | + return $this; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * {@inheritDoc} |
|
| 108 | + */ |
|
| 109 | + public function addPrefix(string $prefix) : RouteInterface |
|
| 110 | + { |
|
| 111 | + $this->path = $prefix . $this->path; |
|
| 112 | + |
|
| 113 | + return $this; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * {@inheritDoc} |
|
| 118 | + */ |
|
| 119 | + public function addSuffix(string $suffix) : RouteInterface |
|
| 120 | + { |
|
| 121 | + $this->path .= $suffix; |
|
| 122 | + |
|
| 123 | + return $this; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * {@inheritDoc} |
|
| 128 | + */ |
|
| 129 | + public function addMethod(string $method) : RouteInterface |
|
| 130 | + { |
|
| 131 | + $this->methods[] = \strtoupper($method); |
|
| 132 | + |
|
| 133 | + return $this; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * {@inheritDoc} |
|
| 138 | + */ |
|
| 139 | + public function addPattern(string $name, string $value) : RouteInterface |
|
| 140 | + { |
|
| 141 | + $this->patterns[$name] = $value; |
|
| 142 | + |
|
| 143 | + return $this; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * {@inheritDoc} |
|
| 148 | + */ |
|
| 149 | + public function addMiddleware(MiddlewareInterface $middleware) : RouteInterface |
|
| 150 | + { |
|
| 151 | + $this->middlewareStack[] = $middleware; |
|
| 152 | + |
|
| 153 | + return $this; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * {@inheritDoc} |
|
| 158 | + */ |
|
| 159 | + public function getId() : string |
|
| 160 | + { |
|
| 161 | + return $this->id; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * {@inheritDoc} |
|
| 166 | + */ |
|
| 167 | + public function getPath() : string |
|
| 168 | + { |
|
| 169 | + return $this->path; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * {@inheritDoc} |
|
| 174 | + */ |
|
| 175 | + public function getMethods() : array |
|
| 176 | + { |
|
| 177 | + return $this->methods; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * {@inheritDoc} |
|
| 182 | + */ |
|
| 183 | + public function getPatterns() : array |
|
| 184 | + { |
|
| 185 | + return $this->patterns; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * {@inheritDoc} |
|
| 190 | + */ |
|
| 191 | + public function getAttributes() : array |
|
| 192 | + { |
|
| 193 | + return $this->attributes; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * {@inheritDoc} |
|
| 198 | + */ |
|
| 199 | + public function getMiddlewareStack() : array |
|
| 200 | + { |
|
| 201 | + return $this->middlewareStack; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * {@inheritDoc} |
|
| 206 | + */ |
|
| 207 | + public function withAttributes(array $attributes) : RouteInterface |
|
| 208 | + { |
|
| 209 | + $clone = clone $this; |
|
| 210 | + |
|
| 211 | + $clone->attributes = \array_merge($clone->attributes, $attributes); |
|
| 212 | + |
|
| 213 | + return $clone; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * {@inheritDoc} |
|
| 218 | + */ |
|
| 219 | + public function buildRegex() : string |
|
| 220 | + { |
|
| 221 | + return route_regex($this->path, $this->patterns); |
|
| 222 | + } |
|
| 223 | 223 | } |
@@ -24,59 +24,59 @@ |
||
| 24 | 24 | interface RouterInterface extends RequestHandlerInterface |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Adds the given route to the router map |
|
| 29 | - * |
|
| 30 | - * @param RouteInterface $route |
|
| 31 | - * |
|
| 32 | - * @return RouterInterface |
|
| 33 | - */ |
|
| 34 | - public function addRoute(RouteInterface $route) : RouterInterface; |
|
| 27 | + /** |
|
| 28 | + * Adds the given route to the router map |
|
| 29 | + * |
|
| 30 | + * @param RouteInterface $route |
|
| 31 | + * |
|
| 32 | + * @return RouterInterface |
|
| 33 | + */ |
|
| 34 | + public function addRoute(RouteInterface $route) : RouterInterface; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Adds the given routes to the router map |
|
| 38 | - * |
|
| 39 | - * @param RouteCollectionInterface $collection |
|
| 40 | - * |
|
| 41 | - * @return RouterInterface |
|
| 42 | - */ |
|
| 43 | - public function addRoutes(RouteCollectionInterface $collection) : RouterInterface; |
|
| 36 | + /** |
|
| 37 | + * Adds the given routes to the router map |
|
| 38 | + * |
|
| 39 | + * @param RouteCollectionInterface $collection |
|
| 40 | + * |
|
| 41 | + * @return RouterInterface |
|
| 42 | + */ |
|
| 43 | + public function addRoutes(RouteCollectionInterface $collection) : RouterInterface; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * Adds the given middleware to the router middleware stack |
|
| 47 | - * |
|
| 48 | - * @param MiddlewareInterface $middleware |
|
| 49 | - * |
|
| 50 | - * @return RouterInterface |
|
| 51 | - */ |
|
| 52 | - public function addMiddleware(MiddlewareInterface $middleware) : RouterInterface; |
|
| 45 | + /** |
|
| 46 | + * Adds the given middleware to the router middleware stack |
|
| 47 | + * |
|
| 48 | + * @param MiddlewareInterface $middleware |
|
| 49 | + * |
|
| 50 | + * @return RouterInterface |
|
| 51 | + */ |
|
| 52 | + public function addMiddleware(MiddlewareInterface $middleware) : RouterInterface; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Gets the router map |
|
| 56 | - * |
|
| 57 | - * @return RouteInterface[] |
|
| 58 | - */ |
|
| 59 | - public function getRoutes() : array; |
|
| 54 | + /** |
|
| 55 | + * Gets the router map |
|
| 56 | + * |
|
| 57 | + * @return RouteInterface[] |
|
| 58 | + */ |
|
| 59 | + public function getRoutes() : array; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Gets the router middleware stack |
|
| 63 | - * |
|
| 64 | - * @return MiddlewareInterface[] |
|
| 65 | - */ |
|
| 66 | - public function getMiddlewareStack() : array; |
|
| 61 | + /** |
|
| 62 | + * Gets the router middleware stack |
|
| 63 | + * |
|
| 64 | + * @return MiddlewareInterface[] |
|
| 65 | + */ |
|
| 66 | + public function getMiddlewareStack() : array; |
|
| 67 | 67 | |
| 68 | - /** |
|
| 69 | - * Looks for a route that matches the given request |
|
| 70 | - * |
|
| 71 | - * @param ServerRequestInterface $request |
|
| 72 | - * |
|
| 73 | - * @return RouteInterface |
|
| 74 | - * |
|
| 75 | - * @throws Exception\MethodNotAllowedException |
|
| 76 | - * If the route found does not support the requested HTTP method. |
|
| 77 | - * |
|
| 78 | - * @throws Exception\RouteNotFoundException |
|
| 79 | - * If a route was not matched. |
|
| 80 | - */ |
|
| 81 | - public function match(ServerRequestInterface $request) : RouteInterface; |
|
| 68 | + /** |
|
| 69 | + * Looks for a route that matches the given request |
|
| 70 | + * |
|
| 71 | + * @param ServerRequestInterface $request |
|
| 72 | + * |
|
| 73 | + * @return RouteInterface |
|
| 74 | + * |
|
| 75 | + * @throws Exception\MethodNotAllowedException |
|
| 76 | + * If the route found does not support the requested HTTP method. |
|
| 77 | + * |
|
| 78 | + * @throws Exception\RouteNotFoundException |
|
| 79 | + * If a route was not matched. |
|
| 80 | + */ |
|
| 81 | + public function match(ServerRequestInterface $request) : RouteInterface; |
|
| 82 | 82 | } |
@@ -17,130 +17,130 @@ |
||
| 17 | 17 | interface RouteCollectionInterface |
| 18 | 18 | { |
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Adds the given route to the collection |
|
| 22 | - * |
|
| 23 | - * @param RouteInterface $route |
|
| 24 | - * |
|
| 25 | - * @return RouteCollectionInterface |
|
| 26 | - */ |
|
| 27 | - public function addRoute(RouteInterface $route) : RouteCollectionInterface; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Gets the collection routes |
|
| 31 | - * |
|
| 32 | - * @return RouteInterface[] |
|
| 33 | - */ |
|
| 34 | - public function getRoutes() : array; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Adds a new route to the collection |
|
| 38 | - * |
|
| 39 | - * @param string $id |
|
| 40 | - * @param string $path |
|
| 41 | - * @param string[] $methods |
|
| 42 | - * |
|
| 43 | - * @return RouteInterface |
|
| 44 | - */ |
|
| 45 | - public function route(string $id, string $path, array $methods) : RouteInterface; |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Adds a new route to the collection that will respond to HEAD requests |
|
| 49 | - * |
|
| 50 | - * @param string $id |
|
| 51 | - * @param string $path |
|
| 52 | - * |
|
| 53 | - * @return RouteInterface |
|
| 54 | - */ |
|
| 55 | - public function head(string $id, string $path) : RouteInterface; |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Adds a new route to the collection that will respond to GET requests |
|
| 59 | - * |
|
| 60 | - * @param string $id |
|
| 61 | - * @param string $path |
|
| 62 | - * |
|
| 63 | - * @return RouteInterface |
|
| 64 | - */ |
|
| 65 | - public function get(string $id, string $path) : RouteInterface; |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Adds a new route to the collection that will respond to POST requests |
|
| 69 | - * |
|
| 70 | - * @param string $id |
|
| 71 | - * @param string $path |
|
| 72 | - * |
|
| 73 | - * @return RouteInterface |
|
| 74 | - */ |
|
| 75 | - public function post(string $id, string $path) : RouteInterface; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Adds a new route to the collection that will respond to PUT requests |
|
| 79 | - * |
|
| 80 | - * @param string $id |
|
| 81 | - * @param string $path |
|
| 82 | - * |
|
| 83 | - * @return RouteInterface |
|
| 84 | - */ |
|
| 85 | - public function put(string $id, string $path) : RouteInterface; |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Adds a new route to the collection that will respond to PATCH requests |
|
| 89 | - * |
|
| 90 | - * @param string $id |
|
| 91 | - * @param string $path |
|
| 92 | - * |
|
| 93 | - * @return RouteInterface |
|
| 94 | - */ |
|
| 95 | - public function patch(string $id, string $path) : RouteInterface; |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Adds a new route to the collection that will respond to DELETE requests |
|
| 99 | - * |
|
| 100 | - * @param string $id |
|
| 101 | - * @param string $path |
|
| 102 | - * |
|
| 103 | - * @return RouteInterface |
|
| 104 | - */ |
|
| 105 | - public function delete(string $id, string $path) : RouteInterface; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Adds a new route to the collection that will respond to PURGE requests |
|
| 109 | - * |
|
| 110 | - * @param string $id |
|
| 111 | - * @param string $path |
|
| 112 | - * |
|
| 113 | - * @return RouteInterface |
|
| 114 | - */ |
|
| 115 | - public function purge(string $id, string $path) : RouteInterface; |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Adds a new route to the collection that will respond to safe requests |
|
| 119 | - * |
|
| 120 | - * @param string $id |
|
| 121 | - * @param string $path |
|
| 122 | - * |
|
| 123 | - * @return RouteInterface |
|
| 124 | - */ |
|
| 125 | - public function safe(string $id, string $path) : RouteInterface; |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Adds a new route to the collection that will respond to any requests |
|
| 129 | - * |
|
| 130 | - * @param string $id |
|
| 131 | - * @param string $path |
|
| 132 | - * |
|
| 133 | - * @return RouteInterface |
|
| 134 | - */ |
|
| 135 | - public function any(string $id, string $path) : RouteInterface; |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Route grouping |
|
| 139 | - * |
|
| 140 | - * @param string $prefix |
|
| 141 | - * @param callable $callback |
|
| 142 | - * |
|
| 143 | - * @return void |
|
| 144 | - */ |
|
| 145 | - public function group(string $prefix, callable $callback) : void; |
|
| 20 | + /** |
|
| 21 | + * Adds the given route to the collection |
|
| 22 | + * |
|
| 23 | + * @param RouteInterface $route |
|
| 24 | + * |
|
| 25 | + * @return RouteCollectionInterface |
|
| 26 | + */ |
|
| 27 | + public function addRoute(RouteInterface $route) : RouteCollectionInterface; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Gets the collection routes |
|
| 31 | + * |
|
| 32 | + * @return RouteInterface[] |
|
| 33 | + */ |
|
| 34 | + public function getRoutes() : array; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Adds a new route to the collection |
|
| 38 | + * |
|
| 39 | + * @param string $id |
|
| 40 | + * @param string $path |
|
| 41 | + * @param string[] $methods |
|
| 42 | + * |
|
| 43 | + * @return RouteInterface |
|
| 44 | + */ |
|
| 45 | + public function route(string $id, string $path, array $methods) : RouteInterface; |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Adds a new route to the collection that will respond to HEAD requests |
|
| 49 | + * |
|
| 50 | + * @param string $id |
|
| 51 | + * @param string $path |
|
| 52 | + * |
|
| 53 | + * @return RouteInterface |
|
| 54 | + */ |
|
| 55 | + public function head(string $id, string $path) : RouteInterface; |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Adds a new route to the collection that will respond to GET requests |
|
| 59 | + * |
|
| 60 | + * @param string $id |
|
| 61 | + * @param string $path |
|
| 62 | + * |
|
| 63 | + * @return RouteInterface |
|
| 64 | + */ |
|
| 65 | + public function get(string $id, string $path) : RouteInterface; |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Adds a new route to the collection that will respond to POST requests |
|
| 69 | + * |
|
| 70 | + * @param string $id |
|
| 71 | + * @param string $path |
|
| 72 | + * |
|
| 73 | + * @return RouteInterface |
|
| 74 | + */ |
|
| 75 | + public function post(string $id, string $path) : RouteInterface; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Adds a new route to the collection that will respond to PUT requests |
|
| 79 | + * |
|
| 80 | + * @param string $id |
|
| 81 | + * @param string $path |
|
| 82 | + * |
|
| 83 | + * @return RouteInterface |
|
| 84 | + */ |
|
| 85 | + public function put(string $id, string $path) : RouteInterface; |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Adds a new route to the collection that will respond to PATCH requests |
|
| 89 | + * |
|
| 90 | + * @param string $id |
|
| 91 | + * @param string $path |
|
| 92 | + * |
|
| 93 | + * @return RouteInterface |
|
| 94 | + */ |
|
| 95 | + public function patch(string $id, string $path) : RouteInterface; |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Adds a new route to the collection that will respond to DELETE requests |
|
| 99 | + * |
|
| 100 | + * @param string $id |
|
| 101 | + * @param string $path |
|
| 102 | + * |
|
| 103 | + * @return RouteInterface |
|
| 104 | + */ |
|
| 105 | + public function delete(string $id, string $path) : RouteInterface; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Adds a new route to the collection that will respond to PURGE requests |
|
| 109 | + * |
|
| 110 | + * @param string $id |
|
| 111 | + * @param string $path |
|
| 112 | + * |
|
| 113 | + * @return RouteInterface |
|
| 114 | + */ |
|
| 115 | + public function purge(string $id, string $path) : RouteInterface; |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Adds a new route to the collection that will respond to safe requests |
|
| 119 | + * |
|
| 120 | + * @param string $id |
|
| 121 | + * @param string $path |
|
| 122 | + * |
|
| 123 | + * @return RouteInterface |
|
| 124 | + */ |
|
| 125 | + public function safe(string $id, string $path) : RouteInterface; |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Adds a new route to the collection that will respond to any requests |
|
| 129 | + * |
|
| 130 | + * @param string $id |
|
| 131 | + * @param string $path |
|
| 132 | + * |
|
| 133 | + * @return RouteInterface |
|
| 134 | + */ |
|
| 135 | + public function any(string $id, string $path) : RouteInterface; |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Route grouping |
|
| 139 | + * |
|
| 140 | + * @param string $prefix |
|
| 141 | + * @param callable $callback |
|
| 142 | + * |
|
| 143 | + * @return void |
|
| 144 | + */ |
|
| 145 | + public function group(string $prefix, callable $callback) : void; |
|
| 146 | 146 | } |
@@ -22,129 +22,129 @@ |
||
| 22 | 22 | interface RouteInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Sets the given ID to the route |
|
| 27 | - * |
|
| 28 | - * @param string $id |
|
| 29 | - * |
|
| 30 | - * @return RouteInterface |
|
| 31 | - */ |
|
| 32 | - public function setId(string $id) : RouteInterface; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Sets the given path to the route |
|
| 36 | - * |
|
| 37 | - * @param string $path |
|
| 38 | - * |
|
| 39 | - * @return RouteInterface |
|
| 40 | - */ |
|
| 41 | - public function setPath(string $path) : RouteInterface; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Adds the given prefix to the route path |
|
| 45 | - * |
|
| 46 | - * @param string $prefix |
|
| 47 | - * |
|
| 48 | - * @return RouteInterface |
|
| 49 | - */ |
|
| 50 | - public function addPrefix(string $prefix) : RouteInterface; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Adds the given suffix to the route path |
|
| 54 | - * |
|
| 55 | - * @param string $suffix |
|
| 56 | - * |
|
| 57 | - * @return RouteInterface |
|
| 58 | - */ |
|
| 59 | - public function addSuffix(string $suffix) : RouteInterface; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Adds the given method to the route |
|
| 63 | - * |
|
| 64 | - * @param string $method |
|
| 65 | - * |
|
| 66 | - * @return RouteInterface |
|
| 67 | - */ |
|
| 68 | - public function addMethod(string $method) : RouteInterface; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Adds the given pattern to the route |
|
| 72 | - * |
|
| 73 | - * @param string $name |
|
| 74 | - * @param string $value |
|
| 75 | - * |
|
| 76 | - * @return RouteInterface |
|
| 77 | - */ |
|
| 78 | - public function addPattern(string $name, string $value) : RouteInterface; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Adds the given middleware to the route middleware stack |
|
| 82 | - * |
|
| 83 | - * @param MiddlewareInterface $middleware |
|
| 84 | - * |
|
| 85 | - * @return RouteInterface |
|
| 86 | - */ |
|
| 87 | - public function addMiddleware(MiddlewareInterface $middleware) : RouteInterface; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Gets the route ID |
|
| 91 | - * |
|
| 92 | - * @return string |
|
| 93 | - * |
|
| 94 | - * @throws \RuntimeException If the route ID is missing |
|
| 95 | - */ |
|
| 96 | - public function getId() : string; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Gets the route path |
|
| 100 | - * |
|
| 101 | - * @return string |
|
| 102 | - * |
|
| 103 | - * @throws \RuntimeException If the route path is missing |
|
| 104 | - */ |
|
| 105 | - public function getPath() : string; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Gets the route methods |
|
| 109 | - * |
|
| 110 | - * @return string[] |
|
| 111 | - */ |
|
| 112 | - public function getMethods() : array; |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Gets the route patterns |
|
| 116 | - * |
|
| 117 | - * @return array |
|
| 118 | - */ |
|
| 119 | - public function getPatterns() : array; |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Gets the route attributes |
|
| 123 | - * |
|
| 124 | - * @return array |
|
| 125 | - */ |
|
| 126 | - public function getAttributes() : array; |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Gets the route middleware stack |
|
| 130 | - * |
|
| 131 | - * @return MiddlewareInterface[] |
|
| 132 | - */ |
|
| 133 | - public function getMiddlewareStack() : array; |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Returns the route clone with the given attributes |
|
| 137 | - * |
|
| 138 | - * @param array $attributes |
|
| 139 | - * |
|
| 140 | - * @return RouteInterface |
|
| 141 | - */ |
|
| 142 | - public function withAttributes(array $attributes) : RouteInterface; |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Builds the route regular expression |
|
| 146 | - * |
|
| 147 | - * @return string |
|
| 148 | - */ |
|
| 149 | - public function buildRegex() : string; |
|
| 25 | + /** |
|
| 26 | + * Sets the given ID to the route |
|
| 27 | + * |
|
| 28 | + * @param string $id |
|
| 29 | + * |
|
| 30 | + * @return RouteInterface |
|
| 31 | + */ |
|
| 32 | + public function setId(string $id) : RouteInterface; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Sets the given path to the route |
|
| 36 | + * |
|
| 37 | + * @param string $path |
|
| 38 | + * |
|
| 39 | + * @return RouteInterface |
|
| 40 | + */ |
|
| 41 | + public function setPath(string $path) : RouteInterface; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Adds the given prefix to the route path |
|
| 45 | + * |
|
| 46 | + * @param string $prefix |
|
| 47 | + * |
|
| 48 | + * @return RouteInterface |
|
| 49 | + */ |
|
| 50 | + public function addPrefix(string $prefix) : RouteInterface; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Adds the given suffix to the route path |
|
| 54 | + * |
|
| 55 | + * @param string $suffix |
|
| 56 | + * |
|
| 57 | + * @return RouteInterface |
|
| 58 | + */ |
|
| 59 | + public function addSuffix(string $suffix) : RouteInterface; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Adds the given method to the route |
|
| 63 | + * |
|
| 64 | + * @param string $method |
|
| 65 | + * |
|
| 66 | + * @return RouteInterface |
|
| 67 | + */ |
|
| 68 | + public function addMethod(string $method) : RouteInterface; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Adds the given pattern to the route |
|
| 72 | + * |
|
| 73 | + * @param string $name |
|
| 74 | + * @param string $value |
|
| 75 | + * |
|
| 76 | + * @return RouteInterface |
|
| 77 | + */ |
|
| 78 | + public function addPattern(string $name, string $value) : RouteInterface; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Adds the given middleware to the route middleware stack |
|
| 82 | + * |
|
| 83 | + * @param MiddlewareInterface $middleware |
|
| 84 | + * |
|
| 85 | + * @return RouteInterface |
|
| 86 | + */ |
|
| 87 | + public function addMiddleware(MiddlewareInterface $middleware) : RouteInterface; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Gets the route ID |
|
| 91 | + * |
|
| 92 | + * @return string |
|
| 93 | + * |
|
| 94 | + * @throws \RuntimeException If the route ID is missing |
|
| 95 | + */ |
|
| 96 | + public function getId() : string; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Gets the route path |
|
| 100 | + * |
|
| 101 | + * @return string |
|
| 102 | + * |
|
| 103 | + * @throws \RuntimeException If the route path is missing |
|
| 104 | + */ |
|
| 105 | + public function getPath() : string; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Gets the route methods |
|
| 109 | + * |
|
| 110 | + * @return string[] |
|
| 111 | + */ |
|
| 112 | + public function getMethods() : array; |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Gets the route patterns |
|
| 116 | + * |
|
| 117 | + * @return array |
|
| 118 | + */ |
|
| 119 | + public function getPatterns() : array; |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Gets the route attributes |
|
| 123 | + * |
|
| 124 | + * @return array |
|
| 125 | + */ |
|
| 126 | + public function getAttributes() : array; |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Gets the route middleware stack |
|
| 130 | + * |
|
| 131 | + * @return MiddlewareInterface[] |
|
| 132 | + */ |
|
| 133 | + public function getMiddlewareStack() : array; |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Returns the route clone with the given attributes |
|
| 137 | + * |
|
| 138 | + * @param array $attributes |
|
| 139 | + * |
|
| 140 | + * @return RouteInterface |
|
| 141 | + */ |
|
| 142 | + public function withAttributes(array $attributes) : RouteInterface; |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Builds the route regular expression |
|
| 146 | + * |
|
| 147 | + * @return string |
|
| 148 | + */ |
|
| 149 | + public function buildRegex() : string; |
|
| 150 | 150 | } |
@@ -22,161 +22,161 @@ |
||
| 22 | 22 | class RouteCollection implements RouteCollectionInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * The collection routes |
|
| 27 | - * |
|
| 28 | - * @var RouteInterface[] |
|
| 29 | - */ |
|
| 30 | - protected $routes = []; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * {@inheritDoc} |
|
| 34 | - */ |
|
| 35 | - public function addRoute(RouteInterface $route) : RouteCollectionInterface |
|
| 36 | - { |
|
| 37 | - $this->routes[] = $route; |
|
| 38 | - |
|
| 39 | - return $this; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * {@inheritDoc} |
|
| 44 | - */ |
|
| 45 | - public function getRoutes() : array |
|
| 46 | - { |
|
| 47 | - return $this->routes; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * {@inheritDoc} |
|
| 52 | - */ |
|
| 53 | - public function route(string $id, string $path, array $methods) : RouteInterface |
|
| 54 | - { |
|
| 55 | - $route = new Route($id, $path, $methods); |
|
| 56 | - |
|
| 57 | - $this->addRoute($route); |
|
| 58 | - |
|
| 59 | - return $route; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * {@inheritDoc} |
|
| 64 | - */ |
|
| 65 | - public function head(string $id, string $path) : RouteInterface |
|
| 66 | - { |
|
| 67 | - $methods = [RequestMethodInterface::METHOD_HEAD]; |
|
| 68 | - |
|
| 69 | - return $this->route($id, $path, $methods); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * {@inheritDoc} |
|
| 74 | - */ |
|
| 75 | - public function get(string $id, string $path) : RouteInterface |
|
| 76 | - { |
|
| 77 | - $methods = [RequestMethodInterface::METHOD_GET]; |
|
| 78 | - |
|
| 79 | - return $this->route($id, $path, $methods); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * {@inheritDoc} |
|
| 84 | - */ |
|
| 85 | - public function post(string $id, string $path) : RouteInterface |
|
| 86 | - { |
|
| 87 | - $methods = [RequestMethodInterface::METHOD_POST]; |
|
| 88 | - |
|
| 89 | - return $this->route($id, $path, $methods); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * {@inheritDoc} |
|
| 94 | - */ |
|
| 95 | - public function put(string $id, string $path) : RouteInterface |
|
| 96 | - { |
|
| 97 | - $methods = [RequestMethodInterface::METHOD_PUT]; |
|
| 98 | - |
|
| 99 | - return $this->route($id, $path, $methods); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * {@inheritDoc} |
|
| 104 | - */ |
|
| 105 | - public function patch(string $id, string $path) : RouteInterface |
|
| 106 | - { |
|
| 107 | - $methods = [RequestMethodInterface::METHOD_PATCH]; |
|
| 108 | - |
|
| 109 | - return $this->route($id, $path, $methods); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * {@inheritDoc} |
|
| 114 | - */ |
|
| 115 | - public function delete(string $id, string $path) : RouteInterface |
|
| 116 | - { |
|
| 117 | - $methods = [RequestMethodInterface::METHOD_DELETE]; |
|
| 118 | - |
|
| 119 | - return $this->route($id, $path, $methods); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * {@inheritDoc} |
|
| 124 | - */ |
|
| 125 | - public function purge(string $id, string $path) : RouteInterface |
|
| 126 | - { |
|
| 127 | - $methods = [RequestMethodInterface::METHOD_PURGE]; |
|
| 128 | - |
|
| 129 | - return $this->route($id, $path, $methods); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * {@inheritDoc} |
|
| 134 | - */ |
|
| 135 | - public function safe(string $id, string $path) : RouteInterface |
|
| 136 | - { |
|
| 137 | - $methods = [ |
|
| 138 | - RequestMethodInterface::METHOD_HEAD, |
|
| 139 | - RequestMethodInterface::METHOD_GET, |
|
| 140 | - ]; |
|
| 141 | - |
|
| 142 | - return $this->route($id, $path, $methods); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * {@inheritDoc} |
|
| 147 | - */ |
|
| 148 | - public function any(string $id, string $path) : RouteInterface |
|
| 149 | - { |
|
| 150 | - $methods = [ |
|
| 151 | - RequestMethodInterface::METHOD_HEAD, |
|
| 152 | - RequestMethodInterface::METHOD_GET, |
|
| 153 | - RequestMethodInterface::METHOD_POST, |
|
| 154 | - RequestMethodInterface::METHOD_PUT, |
|
| 155 | - RequestMethodInterface::METHOD_PATCH, |
|
| 156 | - RequestMethodInterface::METHOD_DELETE, |
|
| 157 | - RequestMethodInterface::METHOD_PURGE, |
|
| 158 | - RequestMethodInterface::METHOD_OPTIONS, |
|
| 159 | - RequestMethodInterface::METHOD_TRACE, |
|
| 160 | - RequestMethodInterface::METHOD_CONNECT, |
|
| 161 | - ]; |
|
| 162 | - |
|
| 163 | - return $this->route($id, $path, $methods); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * {@inheritDoc} |
|
| 168 | - */ |
|
| 169 | - public function group(string $prefix, callable $callback) : void |
|
| 170 | - { |
|
| 171 | - $collection = new self; |
|
| 172 | - |
|
| 173 | - $callback($collection); |
|
| 174 | - |
|
| 175 | - foreach ($collection->getRoutes() as $route) |
|
| 176 | - { |
|
| 177 | - $route->addPrefix($prefix); |
|
| 178 | - |
|
| 179 | - $this->addRoute($route); |
|
| 180 | - } |
|
| 181 | - } |
|
| 25 | + /** |
|
| 26 | + * The collection routes |
|
| 27 | + * |
|
| 28 | + * @var RouteInterface[] |
|
| 29 | + */ |
|
| 30 | + protected $routes = []; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * {@inheritDoc} |
|
| 34 | + */ |
|
| 35 | + public function addRoute(RouteInterface $route) : RouteCollectionInterface |
|
| 36 | + { |
|
| 37 | + $this->routes[] = $route; |
|
| 38 | + |
|
| 39 | + return $this; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * {@inheritDoc} |
|
| 44 | + */ |
|
| 45 | + public function getRoutes() : array |
|
| 46 | + { |
|
| 47 | + return $this->routes; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * {@inheritDoc} |
|
| 52 | + */ |
|
| 53 | + public function route(string $id, string $path, array $methods) : RouteInterface |
|
| 54 | + { |
|
| 55 | + $route = new Route($id, $path, $methods); |
|
| 56 | + |
|
| 57 | + $this->addRoute($route); |
|
| 58 | + |
|
| 59 | + return $route; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * {@inheritDoc} |
|
| 64 | + */ |
|
| 65 | + public function head(string $id, string $path) : RouteInterface |
|
| 66 | + { |
|
| 67 | + $methods = [RequestMethodInterface::METHOD_HEAD]; |
|
| 68 | + |
|
| 69 | + return $this->route($id, $path, $methods); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * {@inheritDoc} |
|
| 74 | + */ |
|
| 75 | + public function get(string $id, string $path) : RouteInterface |
|
| 76 | + { |
|
| 77 | + $methods = [RequestMethodInterface::METHOD_GET]; |
|
| 78 | + |
|
| 79 | + return $this->route($id, $path, $methods); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * {@inheritDoc} |
|
| 84 | + */ |
|
| 85 | + public function post(string $id, string $path) : RouteInterface |
|
| 86 | + { |
|
| 87 | + $methods = [RequestMethodInterface::METHOD_POST]; |
|
| 88 | + |
|
| 89 | + return $this->route($id, $path, $methods); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * {@inheritDoc} |
|
| 94 | + */ |
|
| 95 | + public function put(string $id, string $path) : RouteInterface |
|
| 96 | + { |
|
| 97 | + $methods = [RequestMethodInterface::METHOD_PUT]; |
|
| 98 | + |
|
| 99 | + return $this->route($id, $path, $methods); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * {@inheritDoc} |
|
| 104 | + */ |
|
| 105 | + public function patch(string $id, string $path) : RouteInterface |
|
| 106 | + { |
|
| 107 | + $methods = [RequestMethodInterface::METHOD_PATCH]; |
|
| 108 | + |
|
| 109 | + return $this->route($id, $path, $methods); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * {@inheritDoc} |
|
| 114 | + */ |
|
| 115 | + public function delete(string $id, string $path) : RouteInterface |
|
| 116 | + { |
|
| 117 | + $methods = [RequestMethodInterface::METHOD_DELETE]; |
|
| 118 | + |
|
| 119 | + return $this->route($id, $path, $methods); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * {@inheritDoc} |
|
| 124 | + */ |
|
| 125 | + public function purge(string $id, string $path) : RouteInterface |
|
| 126 | + { |
|
| 127 | + $methods = [RequestMethodInterface::METHOD_PURGE]; |
|
| 128 | + |
|
| 129 | + return $this->route($id, $path, $methods); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * {@inheritDoc} |
|
| 134 | + */ |
|
| 135 | + public function safe(string $id, string $path) : RouteInterface |
|
| 136 | + { |
|
| 137 | + $methods = [ |
|
| 138 | + RequestMethodInterface::METHOD_HEAD, |
|
| 139 | + RequestMethodInterface::METHOD_GET, |
|
| 140 | + ]; |
|
| 141 | + |
|
| 142 | + return $this->route($id, $path, $methods); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * {@inheritDoc} |
|
| 147 | + */ |
|
| 148 | + public function any(string $id, string $path) : RouteInterface |
|
| 149 | + { |
|
| 150 | + $methods = [ |
|
| 151 | + RequestMethodInterface::METHOD_HEAD, |
|
| 152 | + RequestMethodInterface::METHOD_GET, |
|
| 153 | + RequestMethodInterface::METHOD_POST, |
|
| 154 | + RequestMethodInterface::METHOD_PUT, |
|
| 155 | + RequestMethodInterface::METHOD_PATCH, |
|
| 156 | + RequestMethodInterface::METHOD_DELETE, |
|
| 157 | + RequestMethodInterface::METHOD_PURGE, |
|
| 158 | + RequestMethodInterface::METHOD_OPTIONS, |
|
| 159 | + RequestMethodInterface::METHOD_TRACE, |
|
| 160 | + RequestMethodInterface::METHOD_CONNECT, |
|
| 161 | + ]; |
|
| 162 | + |
|
| 163 | + return $this->route($id, $path, $methods); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * {@inheritDoc} |
|
| 168 | + */ |
|
| 169 | + public function group(string $prefix, callable $callback) : void |
|
| 170 | + { |
|
| 171 | + $collection = new self; |
|
| 172 | + |
|
| 173 | + $callback($collection); |
|
| 174 | + |
|
| 175 | + foreach ($collection->getRoutes() as $route) |
|
| 176 | + { |
|
| 177 | + $route->addPrefix($prefix); |
|
| 178 | + |
|
| 179 | + $this->addRoute($route); |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | 182 | } |
@@ -23,32 +23,32 @@ |
||
| 23 | 23 | class RouteNotFoundException extends RuntimeException implements HttpExceptionInterface |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * Server Request instance |
|
| 28 | - * |
|
| 29 | - * @var ServerRequestInterface |
|
| 30 | - */ |
|
| 31 | - protected $request; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Constructor of the class |
|
| 35 | - * |
|
| 36 | - * @param ServerRequestInterface $request |
|
| 37 | - * @param int $code |
|
| 38 | - * @param null|Throwable $previous |
|
| 39 | - */ |
|
| 40 | - public function __construct(ServerRequestInterface $request, int $code = 0, Throwable $previous = null) |
|
| 41 | - { |
|
| 42 | - $this->request = $request; |
|
| 43 | - |
|
| 44 | - parent::__construct('Unable to find a route for the request', $code, $previous); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * {@inheritDoc} |
|
| 49 | - */ |
|
| 50 | - public function getRequest() : ServerRequestInterface |
|
| 51 | - { |
|
| 52 | - return $this->request; |
|
| 53 | - } |
|
| 26 | + /** |
|
| 27 | + * Server Request instance |
|
| 28 | + * |
|
| 29 | + * @var ServerRequestInterface |
|
| 30 | + */ |
|
| 31 | + protected $request; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Constructor of the class |
|
| 35 | + * |
|
| 36 | + * @param ServerRequestInterface $request |
|
| 37 | + * @param int $code |
|
| 38 | + * @param null|Throwable $previous |
|
| 39 | + */ |
|
| 40 | + public function __construct(ServerRequestInterface $request, int $code = 0, Throwable $previous = null) |
|
| 41 | + { |
|
| 42 | + $this->request = $request; |
|
| 43 | + |
|
| 44 | + parent::__construct('Unable to find a route for the request', $code, $previous); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * {@inheritDoc} |
|
| 49 | + */ |
|
| 50 | + public function getRequest() : ServerRequestInterface |
|
| 51 | + { |
|
| 52 | + return $this->request; |
|
| 53 | + } |
|
| 54 | 54 | } |
@@ -24,61 +24,61 @@ |
||
| 24 | 24 | class MethodNotAllowedException extends RuntimeException implements HttpExceptionInterface |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Server Request instance |
|
| 29 | - * |
|
| 30 | - * @var ServerRequestInterface |
|
| 31 | - */ |
|
| 32 | - protected $request; |
|
| 27 | + /** |
|
| 28 | + * Server Request instance |
|
| 29 | + * |
|
| 30 | + * @var ServerRequestInterface |
|
| 31 | + */ |
|
| 32 | + protected $request; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Allowed HTTP methods |
|
| 36 | - * |
|
| 37 | - * @var string[] |
|
| 38 | - */ |
|
| 39 | - protected $allowedMethods; |
|
| 34 | + /** |
|
| 35 | + * Allowed HTTP methods |
|
| 36 | + * |
|
| 37 | + * @var string[] |
|
| 38 | + */ |
|
| 39 | + protected $allowedMethods; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Constructor of the class |
|
| 43 | - * |
|
| 44 | - * @param ServerRequestInterface $request |
|
| 45 | - * @param string[] $allowedMethods |
|
| 46 | - * @param int $code |
|
| 47 | - * @param null|Throwable $previous |
|
| 48 | - */ |
|
| 49 | - public function __construct(ServerRequestInterface $request, array $allowedMethods, int $code = 0, Throwable $previous = null) |
|
| 50 | - { |
|
| 51 | - $this->request = $request; |
|
| 52 | - $this->allowedMethods = $allowedMethods; |
|
| 41 | + /** |
|
| 42 | + * Constructor of the class |
|
| 43 | + * |
|
| 44 | + * @param ServerRequestInterface $request |
|
| 45 | + * @param string[] $allowedMethods |
|
| 46 | + * @param int $code |
|
| 47 | + * @param null|Throwable $previous |
|
| 48 | + */ |
|
| 49 | + public function __construct(ServerRequestInterface $request, array $allowedMethods, int $code = 0, Throwable $previous = null) |
|
| 50 | + { |
|
| 51 | + $this->request = $request; |
|
| 52 | + $this->allowedMethods = $allowedMethods; |
|
| 53 | 53 | |
| 54 | - parent::__construct('The requested resource is not available for the HTTP method', $code, $previous); |
|
| 55 | - } |
|
| 54 | + parent::__construct('The requested resource is not available for the HTTP method', $code, $previous); |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * {@inheritDoc} |
|
| 59 | - */ |
|
| 60 | - public function getRequest() : ServerRequestInterface |
|
| 61 | - { |
|
| 62 | - return $this->request; |
|
| 63 | - } |
|
| 57 | + /** |
|
| 58 | + * {@inheritDoc} |
|
| 59 | + */ |
|
| 60 | + public function getRequest() : ServerRequestInterface |
|
| 61 | + { |
|
| 62 | + return $this->request; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Gets allowed HTTP methods |
|
| 67 | - * |
|
| 68 | - * @return string[] |
|
| 69 | - */ |
|
| 70 | - public function getAllowedMethods() : array |
|
| 71 | - { |
|
| 72 | - return $this->allowedMethods; |
|
| 73 | - } |
|
| 65 | + /** |
|
| 66 | + * Gets allowed HTTP methods |
|
| 67 | + * |
|
| 68 | + * @return string[] |
|
| 69 | + */ |
|
| 70 | + public function getAllowedMethods() : array |
|
| 71 | + { |
|
| 72 | + return $this->allowedMethods; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * Gets allowed HTTP methods as a string |
|
| 77 | - * |
|
| 78 | - * @return string |
|
| 79 | - */ |
|
| 80 | - public function getAllowedMethodsAsString() : string |
|
| 81 | - { |
|
| 82 | - return \implode(',', $this->allowedMethods); |
|
| 83 | - } |
|
| 75 | + /** |
|
| 76 | + * Gets allowed HTTP methods as a string |
|
| 77 | + * |
|
| 78 | + * @return string |
|
| 79 | + */ |
|
| 80 | + public function getAllowedMethodsAsString() : string |
|
| 81 | + { |
|
| 82 | + return \implode(',', $this->allowedMethods); |
|
| 83 | + } |
|
| 84 | 84 | } |
@@ -22,10 +22,10 @@ |
||
| 22 | 22 | interface HttpExceptionInterface |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Gets Server Request instance |
|
| 27 | - * |
|
| 28 | - * @return ServerRequestInterface |
|
| 29 | - */ |
|
| 30 | - public function getRequest() : ServerRequestInterface; |
|
| 25 | + /** |
|
| 26 | + * Gets Server Request instance |
|
| 27 | + * |
|
| 28 | + * @return ServerRequestInterface |
|
| 29 | + */ |
|
| 30 | + public function getRequest() : ServerRequestInterface; |
|
| 31 | 31 | } |