@@ -27,6 +27,10 @@ |
||
| 27 | 27 | { |
| 28 | 28 | |
| 29 | 29 | abstract public function getWildcards(); |
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @return Reflector |
|
| 33 | + */ |
|
| 30 | 34 | abstract public function set($method, $pattern, $action); |
| 31 | 35 | |
| 32 | 36 | /** |
@@ -26,220 +26,220 @@ |
||
| 26 | 26 | trait ControllerCollectorTrait |
| 27 | 27 | { |
| 28 | 28 | |
| 29 | - abstract public function getWildcards(); |
|
| 30 | - abstract public function set($method, $pattern, $action); |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Define how controller actions names will be joined to form the route pattern. |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - |
|
| 38 | - protected $controllerActionJoin = "/"; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Maps all the controller methods that begins with a HTTP method, and maps the rest of |
|
| 42 | - * name as a path. The path will be the method name with slashes before every camelcased |
|
| 43 | - * word and without the HTTP method prefix, and the controller name will be used to prefix |
|
| 44 | - * the route pattern. e.g. ArticlesController::getCreate will generate a route to: GET articles/create |
|
| 45 | - * |
|
| 46 | - * @param string $controller The controller name |
|
| 47 | - * @param string $prefix |
|
| 48 | - * |
|
| 49 | - * @throws \ReflectionException |
|
| 50 | - * @return Group |
|
| 51 | - */ |
|
| 52 | - |
|
| 53 | - public function controller($controller, $prefix = null) |
|
| 54 | - { |
|
| 55 | - $controller = new ReflectionClass($controller); |
|
| 56 | - $prefix = $prefix === null ? $this->getControllerPrefix($controller) : $prefix; |
|
| 57 | - $methods = $controller->getMethods(ReflectionMethod::IS_PUBLIC); |
|
| 58 | - return $this->collectControllerRoutes($controller, $methods, "/$prefix/"); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Maps several controllers at same time. |
|
| 63 | - * |
|
| 64 | - * @param string[] $controllers Controllers name. |
|
| 65 | - * @throws \ReflectionException |
|
| 66 | - * @return Group |
|
| 67 | - */ |
|
| 68 | - |
|
| 69 | - public function controllers(array $controllers) |
|
| 70 | - { |
|
| 71 | - $group = []; |
|
| 72 | - foreach ($controllers as $controller) |
|
| 73 | - $group[] = $this->controller($controller); |
|
| 74 | - return new Group($group); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Alias for Collector::controller but maps a controller without using the controller name as prefix. |
|
| 79 | - * |
|
| 80 | - * @param string $controller The controller name |
|
| 81 | - * @throws \ReflectionException |
|
| 82 | - * @return Group |
|
| 83 | - */ |
|
| 84 | - |
|
| 85 | - public function controllerWithoutPrefix($controller) |
|
| 86 | - { |
|
| 87 | - $controller = new ReflectionClass($controller); |
|
| 88 | - $methods = $controller->getMethods(ReflectionMethod::IS_PUBLIC); |
|
| 89 | - return $this->collectControllerRoutes($controller, $methods, "/"); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Alias for Collector::controllers but maps a controller without using the controller name as prefix. |
|
| 94 | - * |
|
| 95 | - * @param string[] $controllers |
|
| 96 | - * @throws \ReflectionException |
|
| 97 | - * @return Group |
|
| 98 | - */ |
|
| 99 | - |
|
| 100 | - public function controllersWithoutPrefix(array $controllers) |
|
| 101 | - { |
|
| 102 | - $group = []; |
|
| 103 | - foreach ($controllers as $controller) |
|
| 104 | - $group[] = $this->controllerWithoutPrefix($controller); |
|
| 105 | - return new Group($group); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @param ReflectionClass $controller |
|
| 110 | - * @param string[] $methods |
|
| 111 | - * @param string $prefix |
|
| 112 | - * |
|
| 113 | - * @return Group |
|
| 114 | - */ |
|
| 115 | - |
|
| 116 | - protected function collectControllerRoutes(ReflectionClass $controller, array $methods, $prefix) |
|
| 117 | - { |
|
| 118 | - $group = []; |
|
| 119 | - $controllerDefaultStrategy = $this->getAnnotatedStrategy($controller); |
|
| 120 | - |
|
| 121 | - /** @var ReflectionMethod $method */ |
|
| 122 | - foreach ($methods as $method) { |
|
| 123 | - $name = preg_split("~(?=[A-Z])~", $method->name); |
|
| 124 | - $http = $name[0]; |
|
| 125 | - unset($name[0]); |
|
| 29 | + abstract public function getWildcards(); |
|
| 30 | + abstract public function set($method, $pattern, $action); |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Define how controller actions names will be joined to form the route pattern. |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + |
|
| 38 | + protected $controllerActionJoin = "/"; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Maps all the controller methods that begins with a HTTP method, and maps the rest of |
|
| 42 | + * name as a path. The path will be the method name with slashes before every camelcased |
|
| 43 | + * word and without the HTTP method prefix, and the controller name will be used to prefix |
|
| 44 | + * the route pattern. e.g. ArticlesController::getCreate will generate a route to: GET articles/create |
|
| 45 | + * |
|
| 46 | + * @param string $controller The controller name |
|
| 47 | + * @param string $prefix |
|
| 48 | + * |
|
| 49 | + * @throws \ReflectionException |
|
| 50 | + * @return Group |
|
| 51 | + */ |
|
| 52 | + |
|
| 53 | + public function controller($controller, $prefix = null) |
|
| 54 | + { |
|
| 55 | + $controller = new ReflectionClass($controller); |
|
| 56 | + $prefix = $prefix === null ? $this->getControllerPrefix($controller) : $prefix; |
|
| 57 | + $methods = $controller->getMethods(ReflectionMethod::IS_PUBLIC); |
|
| 58 | + return $this->collectControllerRoutes($controller, $methods, "/$prefix/"); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Maps several controllers at same time. |
|
| 63 | + * |
|
| 64 | + * @param string[] $controllers Controllers name. |
|
| 65 | + * @throws \ReflectionException |
|
| 66 | + * @return Group |
|
| 67 | + */ |
|
| 68 | + |
|
| 69 | + public function controllers(array $controllers) |
|
| 70 | + { |
|
| 71 | + $group = []; |
|
| 72 | + foreach ($controllers as $controller) |
|
| 73 | + $group[] = $this->controller($controller); |
|
| 74 | + return new Group($group); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Alias for Collector::controller but maps a controller without using the controller name as prefix. |
|
| 79 | + * |
|
| 80 | + * @param string $controller The controller name |
|
| 81 | + * @throws \ReflectionException |
|
| 82 | + * @return Group |
|
| 83 | + */ |
|
| 84 | + |
|
| 85 | + public function controllerWithoutPrefix($controller) |
|
| 86 | + { |
|
| 87 | + $controller = new ReflectionClass($controller); |
|
| 88 | + $methods = $controller->getMethods(ReflectionMethod::IS_PUBLIC); |
|
| 89 | + return $this->collectControllerRoutes($controller, $methods, "/"); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Alias for Collector::controllers but maps a controller without using the controller name as prefix. |
|
| 94 | + * |
|
| 95 | + * @param string[] $controllers |
|
| 96 | + * @throws \ReflectionException |
|
| 97 | + * @return Group |
|
| 98 | + */ |
|
| 99 | + |
|
| 100 | + public function controllersWithoutPrefix(array $controllers) |
|
| 101 | + { |
|
| 102 | + $group = []; |
|
| 103 | + foreach ($controllers as $controller) |
|
| 104 | + $group[] = $this->controllerWithoutPrefix($controller); |
|
| 105 | + return new Group($group); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @param ReflectionClass $controller |
|
| 110 | + * @param string[] $methods |
|
| 111 | + * @param string $prefix |
|
| 112 | + * |
|
| 113 | + * @return Group |
|
| 114 | + */ |
|
| 115 | + |
|
| 116 | + protected function collectControllerRoutes(ReflectionClass $controller, array $methods, $prefix) |
|
| 117 | + { |
|
| 118 | + $group = []; |
|
| 119 | + $controllerDefaultStrategy = $this->getAnnotatedStrategy($controller); |
|
| 120 | + |
|
| 121 | + /** @var ReflectionMethod $method */ |
|
| 122 | + foreach ($methods as $method) { |
|
| 123 | + $name = preg_split("~(?=[A-Z])~", $method->name); |
|
| 124 | + $http = $name[0]; |
|
| 125 | + unset($name[0]); |
|
| 126 | 126 | |
| 127 | - if (strpos(Collector::HTTP_METHODS, $http) !== false) { |
|
| 128 | - $action = $prefix . strtolower(implode($this->controllerActionJoin, $name)); |
|
| 129 | - $dynamic = $this->getMethodConstraints($method); |
|
| 130 | - |
|
| 131 | - /** @var \Codeburner\Router\Route $route */ |
|
| 132 | - $route = $this->set($http, "$action$dynamic", [$controller->name, $method->name]); |
|
| 133 | - $route->setStrategy($this->getAnnotatedStrategy($method) || $controllerDefaultStrategy); |
|
| 134 | - |
|
| 135 | - $group[] = $route; |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return new Group($group); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @param ReflectionClass $controller |
|
| 144 | - * |
|
| 145 | - * @return string |
|
| 146 | - */ |
|
| 147 | - |
|
| 148 | - protected function getControllerPrefix(ReflectionClass $controller) |
|
| 149 | - { |
|
| 150 | - preg_match("~\@prefix\s([a-zA-Z\\\_]+)~i", (string) $controller->getDocComment(), $prefix); |
|
| 151 | - return isset($prefix[1]) ? $prefix[1] : str_replace("controller", "", strtolower($controller->getShortName())); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @param \ReflectionMethod |
|
| 156 | - * @return string |
|
| 157 | - */ |
|
| 158 | - |
|
| 159 | - protected function getMethodConstraints(ReflectionMethod $method) |
|
| 160 | - { |
|
| 161 | - $beginPath = ""; |
|
| 162 | - $endPath = ""; |
|
| 163 | - |
|
| 164 | - if ($parameters = $method->getParameters()) { |
|
| 165 | - $types = $this->getParamsConstraint($method); |
|
| 166 | - |
|
| 167 | - foreach ($parameters as $parameter) { |
|
| 168 | - if ($parameter->isOptional()) { |
|
| 169 | - $beginPath .= "["; |
|
| 170 | - $endPath .= "]"; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - $beginPath .= $this->getPathConstraint($parameter, $types); |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - return $beginPath . $endPath; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * @param ReflectionParameter $parameter |
|
| 182 | - * @param string[] $types |
|
| 183 | - * @return string |
|
| 184 | - */ |
|
| 185 | - |
|
| 186 | - protected function getPathConstraint(ReflectionParameter $parameter, $types) |
|
| 187 | - { |
|
| 188 | - $name = $parameter->name; |
|
| 189 | - $path = "/{" . $name; |
|
| 190 | - return isset($types[$name]) ? "$path:{$types[$name]}}" : "$path}"; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /** |
|
| 194 | - * @param ReflectionMethod $method |
|
| 195 | - * @return string[] |
|
| 196 | - */ |
|
| 197 | - |
|
| 198 | - protected function getParamsConstraint(ReflectionMethod $method) |
|
| 199 | - { |
|
| 200 | - $params = []; |
|
| 201 | - preg_match_all("~\@param\s(" . implode("|", array_keys($this->getWildcards())) . "|\(.+\))\s\\$([a-zA-Z0-1_]+)~i", |
|
| 202 | - $method->getDocComment(), $types, PREG_SET_ORDER); |
|
| 203 | - |
|
| 204 | - foreach ((array) $types as $type) { |
|
| 205 | - // if a pattern is defined on Match take it otherwise take the param type by PHPDoc. |
|
| 206 | - $params[$type[2]] = isset($type[4]) ? $type[4] : $type[1]; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return $params; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * @param Reflector $reflector |
|
| 214 | - * @return string|null |
|
| 215 | - */ |
|
| 216 | - |
|
| 217 | - protected function getAnnotatedStrategy(Reflector $reflector) |
|
| 218 | - { |
|
| 219 | - preg_match("~\@strategy\s([a-zA-Z\\\_]+)~i", (string) $reflector->getDocComment(), $strategy); |
|
| 220 | - return isset($strategy[1]) ? $strategy[1] : null; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Define how controller actions names will be joined to form the route pattern. |
|
| 225 | - * Defaults to "/" so actions like "getMyAction" will be "/my/action". If changed to |
|
| 226 | - * "-" the new pattern will be "/my-action". |
|
| 227 | - * |
|
| 228 | - * @param string $join |
|
| 229 | - */ |
|
| 230 | - |
|
| 231 | - public function setControllerActionJoin($join) |
|
| 232 | - { |
|
| 233 | - $this->controllerActionJoin = $join; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * @return string |
|
| 238 | - */ |
|
| 239 | - |
|
| 240 | - public function getControllerActionJoin() |
|
| 241 | - { |
|
| 242 | - return $this->controllerActionJoin; |
|
| 243 | - } |
|
| 127 | + if (strpos(Collector::HTTP_METHODS, $http) !== false) { |
|
| 128 | + $action = $prefix . strtolower(implode($this->controllerActionJoin, $name)); |
|
| 129 | + $dynamic = $this->getMethodConstraints($method); |
|
| 130 | + |
|
| 131 | + /** @var \Codeburner\Router\Route $route */ |
|
| 132 | + $route = $this->set($http, "$action$dynamic", [$controller->name, $method->name]); |
|
| 133 | + $route->setStrategy($this->getAnnotatedStrategy($method) || $controllerDefaultStrategy); |
|
| 134 | + |
|
| 135 | + $group[] = $route; |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return new Group($group); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @param ReflectionClass $controller |
|
| 144 | + * |
|
| 145 | + * @return string |
|
| 146 | + */ |
|
| 147 | + |
|
| 148 | + protected function getControllerPrefix(ReflectionClass $controller) |
|
| 149 | + { |
|
| 150 | + preg_match("~\@prefix\s([a-zA-Z\\\_]+)~i", (string) $controller->getDocComment(), $prefix); |
|
| 151 | + return isset($prefix[1]) ? $prefix[1] : str_replace("controller", "", strtolower($controller->getShortName())); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @param \ReflectionMethod |
|
| 156 | + * @return string |
|
| 157 | + */ |
|
| 158 | + |
|
| 159 | + protected function getMethodConstraints(ReflectionMethod $method) |
|
| 160 | + { |
|
| 161 | + $beginPath = ""; |
|
| 162 | + $endPath = ""; |
|
| 163 | + |
|
| 164 | + if ($parameters = $method->getParameters()) { |
|
| 165 | + $types = $this->getParamsConstraint($method); |
|
| 166 | + |
|
| 167 | + foreach ($parameters as $parameter) { |
|
| 168 | + if ($parameter->isOptional()) { |
|
| 169 | + $beginPath .= "["; |
|
| 170 | + $endPath .= "]"; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + $beginPath .= $this->getPathConstraint($parameter, $types); |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + return $beginPath . $endPath; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * @param ReflectionParameter $parameter |
|
| 182 | + * @param string[] $types |
|
| 183 | + * @return string |
|
| 184 | + */ |
|
| 185 | + |
|
| 186 | + protected function getPathConstraint(ReflectionParameter $parameter, $types) |
|
| 187 | + { |
|
| 188 | + $name = $parameter->name; |
|
| 189 | + $path = "/{" . $name; |
|
| 190 | + return isset($types[$name]) ? "$path:{$types[$name]}}" : "$path}"; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /** |
|
| 194 | + * @param ReflectionMethod $method |
|
| 195 | + * @return string[] |
|
| 196 | + */ |
|
| 197 | + |
|
| 198 | + protected function getParamsConstraint(ReflectionMethod $method) |
|
| 199 | + { |
|
| 200 | + $params = []; |
|
| 201 | + preg_match_all("~\@param\s(" . implode("|", array_keys($this->getWildcards())) . "|\(.+\))\s\\$([a-zA-Z0-1_]+)~i", |
|
| 202 | + $method->getDocComment(), $types, PREG_SET_ORDER); |
|
| 203 | + |
|
| 204 | + foreach ((array) $types as $type) { |
|
| 205 | + // if a pattern is defined on Match take it otherwise take the param type by PHPDoc. |
|
| 206 | + $params[$type[2]] = isset($type[4]) ? $type[4] : $type[1]; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return $params; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * @param Reflector $reflector |
|
| 214 | + * @return string|null |
|
| 215 | + */ |
|
| 216 | + |
|
| 217 | + protected function getAnnotatedStrategy(Reflector $reflector) |
|
| 218 | + { |
|
| 219 | + preg_match("~\@strategy\s([a-zA-Z\\\_]+)~i", (string) $reflector->getDocComment(), $strategy); |
|
| 220 | + return isset($strategy[1]) ? $strategy[1] : null; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Define how controller actions names will be joined to form the route pattern. |
|
| 225 | + * Defaults to "/" so actions like "getMyAction" will be "/my/action". If changed to |
|
| 226 | + * "-" the new pattern will be "/my-action". |
|
| 227 | + * |
|
| 228 | + * @param string $join |
|
| 229 | + */ |
|
| 230 | + |
|
| 231 | + public function setControllerActionJoin($join) |
|
| 232 | + { |
|
| 233 | + $this->controllerActionJoin = $join; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * @return string |
|
| 238 | + */ |
|
| 239 | + |
|
| 240 | + public function getControllerActionJoin() |
|
| 241 | + { |
|
| 242 | + return $this->controllerActionJoin; |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | 245 | } |
@@ -69,8 +69,9 @@ discard block |
||
| 69 | 69 | public function controllers(array $controllers) |
| 70 | 70 | { |
| 71 | 71 | $group = []; |
| 72 | - foreach ($controllers as $controller) |
|
| 73 | - $group[] = $this->controller($controller); |
|
| 72 | + foreach ($controllers as $controller) { |
|
| 73 | + $group[] = $this->controller($controller); |
|
| 74 | + } |
|
| 74 | 75 | return new Group($group); |
| 75 | 76 | } |
| 76 | 77 | |
@@ -100,8 +101,9 @@ discard block |
||
| 100 | 101 | public function controllersWithoutPrefix(array $controllers) |
| 101 | 102 | { |
| 102 | 103 | $group = []; |
| 103 | - foreach ($controllers as $controller) |
|
| 104 | - $group[] = $this->controllerWithoutPrefix($controller); |
|
| 104 | + foreach ($controllers as $controller) { |
|
| 105 | + $group[] = $this->controllerWithoutPrefix($controller); |
|
| 106 | + } |
|
| 105 | 107 | return new Group($group); |
| 106 | 108 | } |
| 107 | 109 | |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | * @param string $targetHttpMethod The HTTP method that must not be checked |
| 247 | 247 | * @param string $path The Path that must be matched. |
| 248 | 248 | * |
| 249 | - * @return array |
|
| 249 | + * @return string |
|
| 250 | 250 | */ |
| 251 | 251 | |
| 252 | 252 | protected function checkDynamicRouteInOtherMethods($targetHttpMethod, $path) |
@@ -260,6 +260,7 @@ discard block |
||
| 260 | 260 | * Strip the given http methods and return all the others. |
| 261 | 261 | * |
| 262 | 262 | * @param array|string |
| 263 | + * @param string $targetHttpMethod |
|
| 263 | 264 | * @return array |
| 264 | 265 | */ |
| 265 | 266 | |
@@ -23,279 +23,279 @@ |
||
| 23 | 23 | class Matcher |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var Collector |
|
| 28 | - */ |
|
| 29 | - |
|
| 30 | - protected $collector; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Define a basepath to all routes. |
|
| 34 | - * |
|
| 35 | - * @var string |
|
| 36 | - */ |
|
| 37 | - |
|
| 38 | - protected $basepath = ""; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Construct the route dispatcher. |
|
| 42 | - * |
|
| 43 | - * @param Collector $collector |
|
| 44 | - * @param string $basepath Define a Path prefix that must be excluded on matches. |
|
| 45 | - */ |
|
| 46 | - |
|
| 47 | - public function __construct(Collector $collector, $basepath = "") |
|
| 48 | - { |
|
| 49 | - $this->collector = $collector; |
|
| 50 | - $this->basepath = $basepath; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Find a route that matches the given arguments. |
|
| 55 | - * |
|
| 56 | - * @param string $httpMethod |
|
| 57 | - * @param string $path |
|
| 58 | - * |
|
| 59 | - * @throws NotFoundException |
|
| 60 | - * @throws MethodNotAllowedException |
|
| 61 | - * |
|
| 62 | - * @return Route |
|
| 63 | - */ |
|
| 64 | - |
|
| 65 | - public function match($httpMethod, $path) |
|
| 66 | - { |
|
| 67 | - $path = $this->parsePath($path); |
|
| 68 | - |
|
| 69 | - if ($route = $this->collector->findStaticRoute($httpMethod, $path)) { |
|
| 70 | - return $route; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - if ($route = $this->matchDynamicRoute($httpMethod, $path)) { |
|
| 74 | - return $route; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - $this->matchSimilarRoute($httpMethod, $path); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Find and return the request dynamic route based on the compiled data and Path. |
|
| 82 | - * |
|
| 83 | - * @param string $httpMethod |
|
| 84 | - * @param string $path |
|
| 85 | - * |
|
| 86 | - * @return Route|false If the request match an array with the action and parameters will |
|
| 87 | - * be returned otherwise a false will. |
|
| 88 | - */ |
|
| 89 | - |
|
| 90 | - protected function matchDynamicRoute($httpMethod, $path) |
|
| 91 | - { |
|
| 92 | - $routes = $this->collector->findDynamicRoutes($httpMethod, $path); |
|
| 93 | - |
|
| 94 | - if (!$routes) { |
|
| 95 | - return false; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - // chunk routes for smaller regex groups using the Sturges' Formula |
|
| 99 | - foreach (array_chunk($routes, round(1 + 3.3 * log(count($routes))), true) as $chunk) |
|
| 100 | - { |
|
| 101 | - array_map([$this, "buildRoute"], $chunk); |
|
| 102 | - list($pattern, $map) = $this->buildGroup($chunk); |
|
| 103 | - |
|
| 104 | - if (!preg_match($pattern, $path, $matches)) { |
|
| 105 | - continue; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** @var Route $route */ |
|
| 109 | - $route = $map[count($matches)]; |
|
| 110 | - // removing the Path from array. |
|
| 111 | - unset($matches[0]); |
|
| 112 | - // sometimes null values come with the matches so the array_filter must be called first. |
|
| 113 | - $route->setParams(array_combine($route->getParams(), array_filter($matches))); |
|
| 114 | - // route must know who match them |
|
| 115 | - $route->setMatcher($this); |
|
| 116 | - |
|
| 117 | - return $route; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return false; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Parse the dynamic segments of the pattern and replace then for |
|
| 125 | - * corresponding regex. |
|
| 126 | - * |
|
| 127 | - * @param Route $route |
|
| 128 | - * @return Route |
|
| 129 | - */ |
|
| 130 | - |
|
| 131 | - protected function buildRoute(Route $route) |
|
| 132 | - { |
|
| 133 | - if ($route->blocked()) { |
|
| 134 | - return $route; |
|
| 135 | - } |
|
| 26 | + /** |
|
| 27 | + * @var Collector |
|
| 28 | + */ |
|
| 29 | + |
|
| 30 | + protected $collector; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Define a basepath to all routes. |
|
| 34 | + * |
|
| 35 | + * @var string |
|
| 36 | + */ |
|
| 37 | + |
|
| 38 | + protected $basepath = ""; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Construct the route dispatcher. |
|
| 42 | + * |
|
| 43 | + * @param Collector $collector |
|
| 44 | + * @param string $basepath Define a Path prefix that must be excluded on matches. |
|
| 45 | + */ |
|
| 46 | + |
|
| 47 | + public function __construct(Collector $collector, $basepath = "") |
|
| 48 | + { |
|
| 49 | + $this->collector = $collector; |
|
| 50 | + $this->basepath = $basepath; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Find a route that matches the given arguments. |
|
| 55 | + * |
|
| 56 | + * @param string $httpMethod |
|
| 57 | + * @param string $path |
|
| 58 | + * |
|
| 59 | + * @throws NotFoundException |
|
| 60 | + * @throws MethodNotAllowedException |
|
| 61 | + * |
|
| 62 | + * @return Route |
|
| 63 | + */ |
|
| 64 | + |
|
| 65 | + public function match($httpMethod, $path) |
|
| 66 | + { |
|
| 67 | + $path = $this->parsePath($path); |
|
| 68 | + |
|
| 69 | + if ($route = $this->collector->findStaticRoute($httpMethod, $path)) { |
|
| 70 | + return $route; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + if ($route = $this->matchDynamicRoute($httpMethod, $path)) { |
|
| 74 | + return $route; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + $this->matchSimilarRoute($httpMethod, $path); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Find and return the request dynamic route based on the compiled data and Path. |
|
| 82 | + * |
|
| 83 | + * @param string $httpMethod |
|
| 84 | + * @param string $path |
|
| 85 | + * |
|
| 86 | + * @return Route|false If the request match an array with the action and parameters will |
|
| 87 | + * be returned otherwise a false will. |
|
| 88 | + */ |
|
| 89 | + |
|
| 90 | + protected function matchDynamicRoute($httpMethod, $path) |
|
| 91 | + { |
|
| 92 | + $routes = $this->collector->findDynamicRoutes($httpMethod, $path); |
|
| 93 | + |
|
| 94 | + if (!$routes) { |
|
| 95 | + return false; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + // chunk routes for smaller regex groups using the Sturges' Formula |
|
| 99 | + foreach (array_chunk($routes, round(1 + 3.3 * log(count($routes))), true) as $chunk) |
|
| 100 | + { |
|
| 101 | + array_map([$this, "buildRoute"], $chunk); |
|
| 102 | + list($pattern, $map) = $this->buildGroup($chunk); |
|
| 103 | + |
|
| 104 | + if (!preg_match($pattern, $path, $matches)) { |
|
| 105 | + continue; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** @var Route $route */ |
|
| 109 | + $route = $map[count($matches)]; |
|
| 110 | + // removing the Path from array. |
|
| 111 | + unset($matches[0]); |
|
| 112 | + // sometimes null values come with the matches so the array_filter must be called first. |
|
| 113 | + $route->setParams(array_combine($route->getParams(), array_filter($matches))); |
|
| 114 | + // route must know who match them |
|
| 115 | + $route->setMatcher($this); |
|
| 116 | + |
|
| 117 | + return $route; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return false; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Parse the dynamic segments of the pattern and replace then for |
|
| 125 | + * corresponding regex. |
|
| 126 | + * |
|
| 127 | + * @param Route $route |
|
| 128 | + * @return Route |
|
| 129 | + */ |
|
| 130 | + |
|
| 131 | + protected function buildRoute(Route $route) |
|
| 132 | + { |
|
| 133 | + if ($route->blocked()) { |
|
| 134 | + return $route; |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | - list($pattern, $params) = $this->parsePlaceholders($route->getPattern()); |
|
| 138 | - return $route->setPatternWithoutReset($pattern)->setParams($params)->block(); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * Group several dynamic routes patterns into one big regex and maps |
|
| 143 | - * the routes to the pattern positions in the big regex. |
|
| 144 | - * |
|
| 145 | - * @param array $routes |
|
| 146 | - * @return array |
|
| 147 | - */ |
|
| 148 | - |
|
| 149 | - protected function buildGroup(array $routes) |
|
| 150 | - { |
|
| 151 | - $groupCount = (int) $map = $regex = []; |
|
| 152 | - |
|
| 153 | - /** @var Route $route */ |
|
| 154 | - foreach ($routes as $route) { |
|
| 155 | - $params = $route->getParams(); |
|
| 156 | - $paramsCount = count($params); |
|
| 157 | - $groupCount = max($groupCount, $paramsCount) + 1; |
|
| 158 | - $regex[] = $route->getPattern() . str_repeat("()", $groupCount - $paramsCount - 1); |
|
| 159 | - $map[$groupCount] = $route; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - return ["~^(?|" . implode("|", $regex) . ")$~", $map]; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Parse an route pattern seeking for parameters and build the route regex. |
|
| 167 | - * |
|
| 168 | - * @param string $pattern |
|
| 169 | - * @return array 0 => new route regex, 1 => map of parameter names |
|
| 170 | - */ |
|
| 171 | - |
|
| 172 | - protected function parsePlaceholders($pattern) |
|
| 173 | - { |
|
| 174 | - $params = []; |
|
| 175 | - preg_match_all("~" . Collector::DYNAMIC_REGEX . "~x", $pattern, $matches, PREG_SET_ORDER); |
|
| 176 | - |
|
| 177 | - foreach ((array) $matches as $key => $match) { |
|
| 178 | - $pattern = str_replace($match[0], isset($match[2]) ? "({$match[2]})" : "([^/]+)", $pattern); |
|
| 179 | - $params[$key] = $match[1]; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return [$pattern, $params]; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Get only the path of a given url. |
|
| 187 | - * |
|
| 188 | - * @param string $path The given URL |
|
| 189 | - * |
|
| 190 | - * @throws Exception |
|
| 191 | - * @return string |
|
| 192 | - */ |
|
| 193 | - |
|
| 194 | - protected function parsePath($path) |
|
| 195 | - { |
|
| 196 | - $path = parse_url(substr(strstr(";" . $path, ";" . $this->basepath), strlen(";" . $this->basepath)), PHP_URL_PATH); |
|
| 197 | - |
|
| 198 | - if ($path === false) { |
|
| 199 | - throw new Exception("Seriously malformed URL passed to route dispatcher."); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - return $path; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * Generate an HTTP error request with method not allowed or not found. |
|
| 207 | - * |
|
| 208 | - * @param string $httpMethod |
|
| 209 | - * @param string $path |
|
| 210 | - * |
|
| 211 | - * @throws NotFoundException |
|
| 212 | - * @throws MethodNotAllowedException |
|
| 213 | - */ |
|
| 214 | - |
|
| 215 | - protected function matchSimilarRoute($httpMethod, $path) |
|
| 216 | - { |
|
| 217 | - $sm = $dm = []; |
|
| 218 | - |
|
| 219 | - if ($sm = ($this->checkStaticRouteInOtherMethods($httpMethod, $path)) |
|
| 220 | - || $dm = ($this->checkDynamicRouteInOtherMethods($httpMethod, $path))) { |
|
| 221 | - throw new MethodNotAllowedException($httpMethod, $path, array_merge((array) $sm, (array) $dm)); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - throw new NotFoundException($httpMethod, $path); |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Verify if a static route match in another method than the requested. |
|
| 229 | - * |
|
| 230 | - * @param string $targetHttpMethod The HTTP method that must not be checked |
|
| 231 | - * @param string $path The Path that must be matched. |
|
| 232 | - * |
|
| 233 | - * @return array |
|
| 234 | - */ |
|
| 235 | - |
|
| 236 | - protected function checkStaticRouteInOtherMethods($targetHttpMethod, $path) |
|
| 237 | - { |
|
| 238 | - return array_filter($this->getHttpMethodsBut($targetHttpMethod), function ($httpMethod) use ($path) { |
|
| 239 | - return (bool) $this->collector->findStaticRoute($httpMethod, $path); |
|
| 240 | - }); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * Verify if a dynamic route match in another method than the requested. |
|
| 245 | - * |
|
| 246 | - * @param string $targetHttpMethod The HTTP method that must not be checked |
|
| 247 | - * @param string $path The Path that must be matched. |
|
| 248 | - * |
|
| 249 | - * @return array |
|
| 250 | - */ |
|
| 251 | - |
|
| 252 | - protected function checkDynamicRouteInOtherMethods($targetHttpMethod, $path) |
|
| 253 | - { |
|
| 254 | - return array_filter($this->getHttpMethodsBut($targetHttpMethod), function ($httpMethod) use ($path) { |
|
| 255 | - return (bool) $this->matchDynamicRoute($httpMethod, $path); |
|
| 256 | - }); |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - /** |
|
| 260 | - * Strip the given http methods and return all the others. |
|
| 261 | - * |
|
| 262 | - * @param array|string |
|
| 263 | - * @return array |
|
| 264 | - */ |
|
| 265 | - |
|
| 266 | - protected function getHttpMethodsBut($targetHttpMethod) |
|
| 267 | - { |
|
| 268 | - return array_diff(explode(" ", Collector::HTTP_METHODS), (array) $targetHttpMethod); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * @return Collector |
|
| 273 | - */ |
|
| 274 | - |
|
| 275 | - public function getCollector() |
|
| 276 | - { |
|
| 277 | - return $this->collector; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * @return string |
|
| 282 | - */ |
|
| 283 | - |
|
| 284 | - public function getBasePath() |
|
| 285 | - { |
|
| 286 | - return $this->basepath; |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Set a new basepath, this will be a prefix that must be excluded in |
|
| 291 | - * every requested Path. |
|
| 292 | - * |
|
| 293 | - * @param string $basepath The new basepath |
|
| 294 | - */ |
|
| 137 | + list($pattern, $params) = $this->parsePlaceholders($route->getPattern()); |
|
| 138 | + return $route->setPatternWithoutReset($pattern)->setParams($params)->block(); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * Group several dynamic routes patterns into one big regex and maps |
|
| 143 | + * the routes to the pattern positions in the big regex. |
|
| 144 | + * |
|
| 145 | + * @param array $routes |
|
| 146 | + * @return array |
|
| 147 | + */ |
|
| 148 | + |
|
| 149 | + protected function buildGroup(array $routes) |
|
| 150 | + { |
|
| 151 | + $groupCount = (int) $map = $regex = []; |
|
| 152 | + |
|
| 153 | + /** @var Route $route */ |
|
| 154 | + foreach ($routes as $route) { |
|
| 155 | + $params = $route->getParams(); |
|
| 156 | + $paramsCount = count($params); |
|
| 157 | + $groupCount = max($groupCount, $paramsCount) + 1; |
|
| 158 | + $regex[] = $route->getPattern() . str_repeat("()", $groupCount - $paramsCount - 1); |
|
| 159 | + $map[$groupCount] = $route; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + return ["~^(?|" . implode("|", $regex) . ")$~", $map]; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Parse an route pattern seeking for parameters and build the route regex. |
|
| 167 | + * |
|
| 168 | + * @param string $pattern |
|
| 169 | + * @return array 0 => new route regex, 1 => map of parameter names |
|
| 170 | + */ |
|
| 171 | + |
|
| 172 | + protected function parsePlaceholders($pattern) |
|
| 173 | + { |
|
| 174 | + $params = []; |
|
| 175 | + preg_match_all("~" . Collector::DYNAMIC_REGEX . "~x", $pattern, $matches, PREG_SET_ORDER); |
|
| 176 | + |
|
| 177 | + foreach ((array) $matches as $key => $match) { |
|
| 178 | + $pattern = str_replace($match[0], isset($match[2]) ? "({$match[2]})" : "([^/]+)", $pattern); |
|
| 179 | + $params[$key] = $match[1]; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return [$pattern, $params]; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Get only the path of a given url. |
|
| 187 | + * |
|
| 188 | + * @param string $path The given URL |
|
| 189 | + * |
|
| 190 | + * @throws Exception |
|
| 191 | + * @return string |
|
| 192 | + */ |
|
| 193 | + |
|
| 194 | + protected function parsePath($path) |
|
| 195 | + { |
|
| 196 | + $path = parse_url(substr(strstr(";" . $path, ";" . $this->basepath), strlen(";" . $this->basepath)), PHP_URL_PATH); |
|
| 197 | + |
|
| 198 | + if ($path === false) { |
|
| 199 | + throw new Exception("Seriously malformed URL passed to route dispatcher."); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + return $path; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * Generate an HTTP error request with method not allowed or not found. |
|
| 207 | + * |
|
| 208 | + * @param string $httpMethod |
|
| 209 | + * @param string $path |
|
| 210 | + * |
|
| 211 | + * @throws NotFoundException |
|
| 212 | + * @throws MethodNotAllowedException |
|
| 213 | + */ |
|
| 214 | + |
|
| 215 | + protected function matchSimilarRoute($httpMethod, $path) |
|
| 216 | + { |
|
| 217 | + $sm = $dm = []; |
|
| 218 | + |
|
| 219 | + if ($sm = ($this->checkStaticRouteInOtherMethods($httpMethod, $path)) |
|
| 220 | + || $dm = ($this->checkDynamicRouteInOtherMethods($httpMethod, $path))) { |
|
| 221 | + throw new MethodNotAllowedException($httpMethod, $path, array_merge((array) $sm, (array) $dm)); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + throw new NotFoundException($httpMethod, $path); |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Verify if a static route match in another method than the requested. |
|
| 229 | + * |
|
| 230 | + * @param string $targetHttpMethod The HTTP method that must not be checked |
|
| 231 | + * @param string $path The Path that must be matched. |
|
| 232 | + * |
|
| 233 | + * @return array |
|
| 234 | + */ |
|
| 235 | + |
|
| 236 | + protected function checkStaticRouteInOtherMethods($targetHttpMethod, $path) |
|
| 237 | + { |
|
| 238 | + return array_filter($this->getHttpMethodsBut($targetHttpMethod), function ($httpMethod) use ($path) { |
|
| 239 | + return (bool) $this->collector->findStaticRoute($httpMethod, $path); |
|
| 240 | + }); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * Verify if a dynamic route match in another method than the requested. |
|
| 245 | + * |
|
| 246 | + * @param string $targetHttpMethod The HTTP method that must not be checked |
|
| 247 | + * @param string $path The Path that must be matched. |
|
| 248 | + * |
|
| 249 | + * @return array |
|
| 250 | + */ |
|
| 251 | + |
|
| 252 | + protected function checkDynamicRouteInOtherMethods($targetHttpMethod, $path) |
|
| 253 | + { |
|
| 254 | + return array_filter($this->getHttpMethodsBut($targetHttpMethod), function ($httpMethod) use ($path) { |
|
| 255 | + return (bool) $this->matchDynamicRoute($httpMethod, $path); |
|
| 256 | + }); |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + /** |
|
| 260 | + * Strip the given http methods and return all the others. |
|
| 261 | + * |
|
| 262 | + * @param array|string |
|
| 263 | + * @return array |
|
| 264 | + */ |
|
| 265 | + |
|
| 266 | + protected function getHttpMethodsBut($targetHttpMethod) |
|
| 267 | + { |
|
| 268 | + return array_diff(explode(" ", Collector::HTTP_METHODS), (array) $targetHttpMethod); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * @return Collector |
|
| 273 | + */ |
|
| 274 | + |
|
| 275 | + public function getCollector() |
|
| 276 | + { |
|
| 277 | + return $this->collector; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * @return string |
|
| 282 | + */ |
|
| 283 | + |
|
| 284 | + public function getBasePath() |
|
| 285 | + { |
|
| 286 | + return $this->basepath; |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Set a new basepath, this will be a prefix that must be excluded in |
|
| 291 | + * every requested Path. |
|
| 292 | + * |
|
| 293 | + * @param string $basepath The new basepath |
|
| 294 | + */ |
|
| 295 | 295 | |
| 296 | - public function setBasePath($basepath) |
|
| 297 | - { |
|
| 298 | - $this->basepath = $basepath; |
|
| 299 | - } |
|
| 296 | + public function setBasePath($basepath) |
|
| 297 | + { |
|
| 298 | + $this->basepath = $basepath; |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | 301 | } |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | |
| 236 | 236 | protected function checkStaticRouteInOtherMethods($targetHttpMethod, $path) |
| 237 | 237 | { |
| 238 | - return array_filter($this->getHttpMethodsBut($targetHttpMethod), function ($httpMethod) use ($path) { |
|
| 238 | + return array_filter($this->getHttpMethodsBut($targetHttpMethod), function($httpMethod) use ($path) { |
|
| 239 | 239 | return (bool) $this->collector->findStaticRoute($httpMethod, $path); |
| 240 | 240 | }); |
| 241 | 241 | } |
@@ -251,7 +251,7 @@ discard block |
||
| 251 | 251 | |
| 252 | 252 | protected function checkDynamicRouteInOtherMethods($targetHttpMethod, $path) |
| 253 | 253 | { |
| 254 | - return array_filter($this->getHttpMethodsBut($targetHttpMethod), function ($httpMethod) use ($path) { |
|
| 254 | + return array_filter($this->getHttpMethodsBut($targetHttpMethod), function($httpMethod) use ($path) { |
|
| 255 | 255 | return (bool) $this->matchDynamicRoute($httpMethod, $path); |
| 256 | 256 | }); |
| 257 | 257 | } |
@@ -33,321 +33,321 @@ |
||
| 33 | 33 | class Collector |
| 34 | 34 | { |
| 35 | 35 | |
| 36 | - use Collectors\ControllerCollectorTrait; |
|
| 37 | - use Collectors\ResourceCollectorTrait; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * These regex define the structure of a dynamic segment in a pattern. |
|
| 41 | - * |
|
| 42 | - * @var string |
|
| 43 | - */ |
|
| 44 | - |
|
| 45 | - const DYNAMIC_REGEX = "{\s*(\w*)\s*(?::\s*([^{}]*(?:{(?-1)}*)*))?\s*}"; |
|
| 46 | - |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * All the supported http methods separated by spaces. |
|
| 50 | - * |
|
| 51 | - * @var string |
|
| 52 | - */ |
|
| 53 | - |
|
| 54 | - const HTTP_METHODS = "get post put patch delete"; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * The static routes are simple stored in a multidimensional array, the first |
|
| 58 | - * dimension is indexed by an http method and hold an array indexed with the patterns |
|
| 59 | - * and holding the route. ex. [METHOD => [PATTERN => ROUTE]] |
|
| 60 | - * |
|
| 61 | - * @var array |
|
| 62 | - */ |
|
| 63 | - |
|
| 64 | - protected $statics = []; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * The dynamic routes have parameters and are stored in a hashtable that every cell have |
|
| 68 | - * an array with route patterns as indexes and routes as values. ex. [INDEX => [PATTERN => ROUTE]] |
|
| 69 | - * |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - |
|
| 73 | - protected $dynamics = []; |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Some regex wildcards for easily definition of dynamic routes. |
|
| 77 | - * |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - |
|
| 81 | - protected $wildcards = [ |
|
| 82 | - "uid" => "uid-[a-zA-Z0-9]", |
|
| 83 | - "slug" => "[a-z0-9-]", |
|
| 84 | - "string" => "\w", |
|
| 85 | - "int" => "\d", |
|
| 86 | - "integer" => "\d", |
|
| 87 | - "float" => "[-+]?\d*?[.]?\d", |
|
| 88 | - "double" => "[-+]?\d*?[.]?\d", |
|
| 89 | - "hex" => "0[xX][0-9a-fA-F]", |
|
| 90 | - "octal" => "0[1-7][0-7]", |
|
| 91 | - "bool" => "1|0|true|false|yes|no", |
|
| 92 | - "boolean" => "1|0|true|false|yes|no", |
|
| 93 | - ]; |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @param string $method |
|
| 97 | - * @param string $pattern |
|
| 98 | - * @param callable $action |
|
| 99 | - * |
|
| 100 | - * @throws BadRouteException |
|
| 101 | - * @throws MethodNotSupportedException |
|
| 102 | - * |
|
| 103 | - * @return Group |
|
| 104 | - */ |
|
| 105 | - |
|
| 106 | - public function set($method, $pattern, $action) |
|
| 107 | - { |
|
| 108 | - $method = $this->parseMethod($method); |
|
| 109 | - $patterns = $this->parsePattern($pattern); |
|
| 110 | - $group = []; |
|
| 111 | - |
|
| 112 | - foreach ($patterns as $pattern) |
|
| 113 | - { |
|
| 114 | - $route = new Route($this, $method, $pattern, $action); |
|
| 115 | - $group[] = $route; |
|
| 116 | - |
|
| 117 | - if (strpos($pattern, "{") !== false) { |
|
| 118 | - $index = $this->getDynamicIndex($method, $pattern); |
|
| 119 | - $this->dynamics[$index][$pattern] = $route; |
|
| 120 | - } else $this->statics[$method][$pattern] = $route; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - return new Group($group); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - public function get ($pattern, $action) { return $this->set("get" , $pattern, $action); } |
|
| 127 | - public function post ($pattern, $action) { return $this->set("post" , $pattern, $action); } |
|
| 128 | - public function put ($pattern, $action) { return $this->set("put" , $pattern, $action); } |
|
| 129 | - public function patch ($pattern, $action) { return $this->set("patch" , $pattern, $action); } |
|
| 130 | - public function delete($pattern, $action) { return $this->set("delete", $pattern, $action); } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Insert a route into several http methods. |
|
| 134 | - * |
|
| 135 | - * @param string[] $methods |
|
| 136 | - * @param string $pattern |
|
| 137 | - * @param Callable $action |
|
| 138 | - * |
|
| 139 | - * @return Group |
|
| 140 | - */ |
|
| 141 | - |
|
| 142 | - public function match(array $methods, $pattern, $action) |
|
| 143 | - { |
|
| 144 | - $group = []; |
|
| 145 | - foreach ($methods as $method) |
|
| 146 | - $group[] = $this->set($method, $pattern, $action); |
|
| 147 | - return new Group($group); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Insert a route into every http method supported. |
|
| 152 | - * |
|
| 153 | - * @param string $pattern |
|
| 154 | - * @param Callable $action |
|
| 155 | - * |
|
| 156 | - * @return Group |
|
| 157 | - */ |
|
| 158 | - |
|
| 159 | - public function any($pattern, $action) |
|
| 160 | - { |
|
| 161 | - return $this->match(explode(" ", self::HTTP_METHODS), $pattern, $action); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Insert a route into every http method supported but the given ones. |
|
| 166 | - * |
|
| 167 | - * @param string[] $methods |
|
| 168 | - * @param string $pattern |
|
| 169 | - * @param Callable $action |
|
| 170 | - * |
|
| 171 | - * @return Group |
|
| 172 | - */ |
|
| 173 | - |
|
| 174 | - public function except(array $methods, $pattern, $action) |
|
| 175 | - { |
|
| 176 | - return $this->match(array_diff(explode(" ", self::HTTP_METHODS), $methods), $pattern, $action); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Group all given routes. |
|
| 181 | - * |
|
| 182 | - * @param Route[] $routes |
|
| 183 | - * @return Group |
|
| 184 | - */ |
|
| 185 | - |
|
| 186 | - public function group(array $routes) |
|
| 187 | - { |
|
| 188 | - $group = []; |
|
| 189 | - foreach ($routes as $route) |
|
| 190 | - $group[] = $route; |
|
| 191 | - return new Group($group); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Remove a route from collector. |
|
| 196 | - * |
|
| 197 | - * @param string $method |
|
| 198 | - * @param string $pattern |
|
| 199 | - */ |
|
| 200 | - |
|
| 201 | - public function forget($method, $pattern) |
|
| 202 | - { |
|
| 203 | - if (strpos($pattern, "{") === false) { |
|
| 204 | - unset($this->statics[$method][$pattern]); |
|
| 205 | - } else unset($this->dynamics[$this->getDynamicIndex($method, $pattern)][$pattern]); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * Determine if the http method is valid. |
|
| 210 | - * |
|
| 211 | - * @param string $method |
|
| 212 | - * @throws MethodNotSupportedException |
|
| 213 | - * @return string |
|
| 214 | - */ |
|
| 215 | - |
|
| 216 | - protected function parseMethod($method) |
|
| 217 | - { |
|
| 218 | - $method = strtolower($method); |
|
| 219 | - |
|
| 220 | - if (strpos(self::HTTP_METHODS, $method) === false) { |
|
| 221 | - throw new MethodNotSupportedException($method); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - return $method; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Separate routes pattern with optional parts into n new patterns. |
|
| 229 | - * |
|
| 230 | - * @param string $pattern |
|
| 231 | - * @return array |
|
| 232 | - */ |
|
| 233 | - |
|
| 234 | - protected function parsePattern($pattern) |
|
| 235 | - { |
|
| 236 | - $withoutClosing = rtrim($pattern, "]"); |
|
| 237 | - $closingNumber = strlen($pattern) - strlen($withoutClosing); |
|
| 238 | - |
|
| 239 | - $segments = preg_split("~" . self::DYNAMIC_REGEX . "(*SKIP)(*F)|\[~x", $withoutClosing); |
|
| 240 | - $this->parseSegments($segments, $closingNumber, $withoutClosing); |
|
| 241 | - |
|
| 242 | - return $this->buildSegments($segments); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Parse all the possible patterns seeking for an incorrect or incompatible pattern. |
|
| 247 | - * |
|
| 248 | - * @param string[] $segments Segments are all the possible patterns made on top of a pattern with optional segments. |
|
| 249 | - * @param int $closingNumber The count of optional segments. |
|
| 250 | - * @param string $withoutClosing The pattern without the closing token of an optional segment. aka: ] |
|
| 251 | - * |
|
| 252 | - * @throws BadRouteException |
|
| 253 | - */ |
|
| 254 | - |
|
| 255 | - protected function parseSegments(array $segments, $closingNumber, $withoutClosing) |
|
| 256 | - { |
|
| 257 | - if ($closingNumber !== count($segments) - 1) { |
|
| 258 | - if (preg_match("~" . self::DYNAMIC_REGEX . "(*SKIP)(*F)|\]~x", $withoutClosing)) { |
|
| 259 | - throw new BadRouteException(BadRouteException::OPTIONAL_SEGMENTS_ON_MIDDLE); |
|
| 260 | - } else throw new BadRouteException(BadRouteException::UNCLOSED_OPTIONAL_SEGMENTS); |
|
| 261 | - } |
|
| 262 | - } |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * @param string[] $segments |
|
| 266 | - * |
|
| 267 | - * @throws BadRouteException |
|
| 268 | - * @return array |
|
| 269 | - */ |
|
| 270 | - |
|
| 271 | - protected function buildSegments(array $segments) |
|
| 272 | - { |
|
| 273 | - $pattern = ""; |
|
| 274 | - $patterns = []; |
|
| 275 | - $wildcardTokens = []; |
|
| 276 | - $wildcardRegex = []; |
|
| 277 | - |
|
| 278 | - foreach ($this->wildcards as $token => $regex) { |
|
| 279 | - $wildcardTokens[] = ":$token"; |
|
| 280 | - $wildcardRegex [] = ":$regex"; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - foreach ($segments as $n => $segment) { |
|
| 284 | - if ($segment === "" && $n !== 0) { |
|
| 285 | - throw new BadRouteException(BadRouteException::EMPTY_OPTIONAL_PARTS); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - $patterns[] = $pattern .= str_replace($wildcardTokens, $wildcardRegex, $segment); |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - return $patterns; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * @param string $method |
|
| 296 | - * @param string $pattern |
|
| 297 | - * |
|
| 298 | - * @return array|false |
|
| 299 | - */ |
|
| 300 | - |
|
| 301 | - public function findStaticRoute($method, $pattern) |
|
| 302 | - { |
|
| 303 | - $method = strtolower($method); |
|
| 304 | - if (isset($this->statics[$method]) && isset($this->statics[$method][$pattern])) |
|
| 305 | - return $this->statics[$method][$pattern]; |
|
| 306 | - return false; |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * @param string $method |
|
| 311 | - * @param string $pattern |
|
| 312 | - * |
|
| 313 | - * @return array|false |
|
| 314 | - */ |
|
| 315 | - |
|
| 316 | - public function findDynamicRoutes($method, $pattern) |
|
| 317 | - { |
|
| 318 | - $index = $this->getDynamicIndex($method, $pattern); |
|
| 319 | - return isset($this->dynamics[$index]) ? $this->dynamics[$index] : false; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - /** |
|
| 323 | - * @param string $method |
|
| 324 | - * @param string $pattern |
|
| 325 | - * |
|
| 326 | - * @return int |
|
| 327 | - */ |
|
| 328 | - |
|
| 329 | - protected function getDynamicIndex($method, $pattern) |
|
| 330 | - { |
|
| 331 | - return crc32(strtolower($method)) + substr_count($pattern, "/"); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * @return string[] |
|
| 336 | - */ |
|
| 337 | - |
|
| 338 | - public function getWildcards() |
|
| 339 | - { |
|
| 340 | - return $this->wildcards; |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * @param string $pattern |
|
| 345 | - * @param string $wildcard |
|
| 346 | - */ |
|
| 347 | - |
|
| 348 | - public function setWildcard($pattern, $wildcard) |
|
| 349 | - { |
|
| 350 | - $this->wildcards[$pattern] = $wildcard; |
|
| 351 | - } |
|
| 36 | + use Collectors\ControllerCollectorTrait; |
|
| 37 | + use Collectors\ResourceCollectorTrait; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * These regex define the structure of a dynamic segment in a pattern. |
|
| 41 | + * |
|
| 42 | + * @var string |
|
| 43 | + */ |
|
| 44 | + |
|
| 45 | + const DYNAMIC_REGEX = "{\s*(\w*)\s*(?::\s*([^{}]*(?:{(?-1)}*)*))?\s*}"; |
|
| 46 | + |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * All the supported http methods separated by spaces. |
|
| 50 | + * |
|
| 51 | + * @var string |
|
| 52 | + */ |
|
| 53 | + |
|
| 54 | + const HTTP_METHODS = "get post put patch delete"; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * The static routes are simple stored in a multidimensional array, the first |
|
| 58 | + * dimension is indexed by an http method and hold an array indexed with the patterns |
|
| 59 | + * and holding the route. ex. [METHOD => [PATTERN => ROUTE]] |
|
| 60 | + * |
|
| 61 | + * @var array |
|
| 62 | + */ |
|
| 63 | + |
|
| 64 | + protected $statics = []; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * The dynamic routes have parameters and are stored in a hashtable that every cell have |
|
| 68 | + * an array with route patterns as indexes and routes as values. ex. [INDEX => [PATTERN => ROUTE]] |
|
| 69 | + * |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + |
|
| 73 | + protected $dynamics = []; |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Some regex wildcards for easily definition of dynamic routes. |
|
| 77 | + * |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + |
|
| 81 | + protected $wildcards = [ |
|
| 82 | + "uid" => "uid-[a-zA-Z0-9]", |
|
| 83 | + "slug" => "[a-z0-9-]", |
|
| 84 | + "string" => "\w", |
|
| 85 | + "int" => "\d", |
|
| 86 | + "integer" => "\d", |
|
| 87 | + "float" => "[-+]?\d*?[.]?\d", |
|
| 88 | + "double" => "[-+]?\d*?[.]?\d", |
|
| 89 | + "hex" => "0[xX][0-9a-fA-F]", |
|
| 90 | + "octal" => "0[1-7][0-7]", |
|
| 91 | + "bool" => "1|0|true|false|yes|no", |
|
| 92 | + "boolean" => "1|0|true|false|yes|no", |
|
| 93 | + ]; |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @param string $method |
|
| 97 | + * @param string $pattern |
|
| 98 | + * @param callable $action |
|
| 99 | + * |
|
| 100 | + * @throws BadRouteException |
|
| 101 | + * @throws MethodNotSupportedException |
|
| 102 | + * |
|
| 103 | + * @return Group |
|
| 104 | + */ |
|
| 105 | + |
|
| 106 | + public function set($method, $pattern, $action) |
|
| 107 | + { |
|
| 108 | + $method = $this->parseMethod($method); |
|
| 109 | + $patterns = $this->parsePattern($pattern); |
|
| 110 | + $group = []; |
|
| 111 | + |
|
| 112 | + foreach ($patterns as $pattern) |
|
| 113 | + { |
|
| 114 | + $route = new Route($this, $method, $pattern, $action); |
|
| 115 | + $group[] = $route; |
|
| 116 | + |
|
| 117 | + if (strpos($pattern, "{") !== false) { |
|
| 118 | + $index = $this->getDynamicIndex($method, $pattern); |
|
| 119 | + $this->dynamics[$index][$pattern] = $route; |
|
| 120 | + } else $this->statics[$method][$pattern] = $route; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + return new Group($group); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + public function get ($pattern, $action) { return $this->set("get" , $pattern, $action); } |
|
| 127 | + public function post ($pattern, $action) { return $this->set("post" , $pattern, $action); } |
|
| 128 | + public function put ($pattern, $action) { return $this->set("put" , $pattern, $action); } |
|
| 129 | + public function patch ($pattern, $action) { return $this->set("patch" , $pattern, $action); } |
|
| 130 | + public function delete($pattern, $action) { return $this->set("delete", $pattern, $action); } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Insert a route into several http methods. |
|
| 134 | + * |
|
| 135 | + * @param string[] $methods |
|
| 136 | + * @param string $pattern |
|
| 137 | + * @param Callable $action |
|
| 138 | + * |
|
| 139 | + * @return Group |
|
| 140 | + */ |
|
| 141 | + |
|
| 142 | + public function match(array $methods, $pattern, $action) |
|
| 143 | + { |
|
| 144 | + $group = []; |
|
| 145 | + foreach ($methods as $method) |
|
| 146 | + $group[] = $this->set($method, $pattern, $action); |
|
| 147 | + return new Group($group); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Insert a route into every http method supported. |
|
| 152 | + * |
|
| 153 | + * @param string $pattern |
|
| 154 | + * @param Callable $action |
|
| 155 | + * |
|
| 156 | + * @return Group |
|
| 157 | + */ |
|
| 158 | + |
|
| 159 | + public function any($pattern, $action) |
|
| 160 | + { |
|
| 161 | + return $this->match(explode(" ", self::HTTP_METHODS), $pattern, $action); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Insert a route into every http method supported but the given ones. |
|
| 166 | + * |
|
| 167 | + * @param string[] $methods |
|
| 168 | + * @param string $pattern |
|
| 169 | + * @param Callable $action |
|
| 170 | + * |
|
| 171 | + * @return Group |
|
| 172 | + */ |
|
| 173 | + |
|
| 174 | + public function except(array $methods, $pattern, $action) |
|
| 175 | + { |
|
| 176 | + return $this->match(array_diff(explode(" ", self::HTTP_METHODS), $methods), $pattern, $action); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Group all given routes. |
|
| 181 | + * |
|
| 182 | + * @param Route[] $routes |
|
| 183 | + * @return Group |
|
| 184 | + */ |
|
| 185 | + |
|
| 186 | + public function group(array $routes) |
|
| 187 | + { |
|
| 188 | + $group = []; |
|
| 189 | + foreach ($routes as $route) |
|
| 190 | + $group[] = $route; |
|
| 191 | + return new Group($group); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Remove a route from collector. |
|
| 196 | + * |
|
| 197 | + * @param string $method |
|
| 198 | + * @param string $pattern |
|
| 199 | + */ |
|
| 200 | + |
|
| 201 | + public function forget($method, $pattern) |
|
| 202 | + { |
|
| 203 | + if (strpos($pattern, "{") === false) { |
|
| 204 | + unset($this->statics[$method][$pattern]); |
|
| 205 | + } else unset($this->dynamics[$this->getDynamicIndex($method, $pattern)][$pattern]); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * Determine if the http method is valid. |
|
| 210 | + * |
|
| 211 | + * @param string $method |
|
| 212 | + * @throws MethodNotSupportedException |
|
| 213 | + * @return string |
|
| 214 | + */ |
|
| 215 | + |
|
| 216 | + protected function parseMethod($method) |
|
| 217 | + { |
|
| 218 | + $method = strtolower($method); |
|
| 219 | + |
|
| 220 | + if (strpos(self::HTTP_METHODS, $method) === false) { |
|
| 221 | + throw new MethodNotSupportedException($method); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + return $method; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Separate routes pattern with optional parts into n new patterns. |
|
| 229 | + * |
|
| 230 | + * @param string $pattern |
|
| 231 | + * @return array |
|
| 232 | + */ |
|
| 233 | + |
|
| 234 | + protected function parsePattern($pattern) |
|
| 235 | + { |
|
| 236 | + $withoutClosing = rtrim($pattern, "]"); |
|
| 237 | + $closingNumber = strlen($pattern) - strlen($withoutClosing); |
|
| 238 | + |
|
| 239 | + $segments = preg_split("~" . self::DYNAMIC_REGEX . "(*SKIP)(*F)|\[~x", $withoutClosing); |
|
| 240 | + $this->parseSegments($segments, $closingNumber, $withoutClosing); |
|
| 241 | + |
|
| 242 | + return $this->buildSegments($segments); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Parse all the possible patterns seeking for an incorrect or incompatible pattern. |
|
| 247 | + * |
|
| 248 | + * @param string[] $segments Segments are all the possible patterns made on top of a pattern with optional segments. |
|
| 249 | + * @param int $closingNumber The count of optional segments. |
|
| 250 | + * @param string $withoutClosing The pattern without the closing token of an optional segment. aka: ] |
|
| 251 | + * |
|
| 252 | + * @throws BadRouteException |
|
| 253 | + */ |
|
| 254 | + |
|
| 255 | + protected function parseSegments(array $segments, $closingNumber, $withoutClosing) |
|
| 256 | + { |
|
| 257 | + if ($closingNumber !== count($segments) - 1) { |
|
| 258 | + if (preg_match("~" . self::DYNAMIC_REGEX . "(*SKIP)(*F)|\]~x", $withoutClosing)) { |
|
| 259 | + throw new BadRouteException(BadRouteException::OPTIONAL_SEGMENTS_ON_MIDDLE); |
|
| 260 | + } else throw new BadRouteException(BadRouteException::UNCLOSED_OPTIONAL_SEGMENTS); |
|
| 261 | + } |
|
| 262 | + } |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * @param string[] $segments |
|
| 266 | + * |
|
| 267 | + * @throws BadRouteException |
|
| 268 | + * @return array |
|
| 269 | + */ |
|
| 270 | + |
|
| 271 | + protected function buildSegments(array $segments) |
|
| 272 | + { |
|
| 273 | + $pattern = ""; |
|
| 274 | + $patterns = []; |
|
| 275 | + $wildcardTokens = []; |
|
| 276 | + $wildcardRegex = []; |
|
| 277 | + |
|
| 278 | + foreach ($this->wildcards as $token => $regex) { |
|
| 279 | + $wildcardTokens[] = ":$token"; |
|
| 280 | + $wildcardRegex [] = ":$regex"; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + foreach ($segments as $n => $segment) { |
|
| 284 | + if ($segment === "" && $n !== 0) { |
|
| 285 | + throw new BadRouteException(BadRouteException::EMPTY_OPTIONAL_PARTS); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + $patterns[] = $pattern .= str_replace($wildcardTokens, $wildcardRegex, $segment); |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + return $patterns; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * @param string $method |
|
| 296 | + * @param string $pattern |
|
| 297 | + * |
|
| 298 | + * @return array|false |
|
| 299 | + */ |
|
| 300 | + |
|
| 301 | + public function findStaticRoute($method, $pattern) |
|
| 302 | + { |
|
| 303 | + $method = strtolower($method); |
|
| 304 | + if (isset($this->statics[$method]) && isset($this->statics[$method][$pattern])) |
|
| 305 | + return $this->statics[$method][$pattern]; |
|
| 306 | + return false; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * @param string $method |
|
| 311 | + * @param string $pattern |
|
| 312 | + * |
|
| 313 | + * @return array|false |
|
| 314 | + */ |
|
| 315 | + |
|
| 316 | + public function findDynamicRoutes($method, $pattern) |
|
| 317 | + { |
|
| 318 | + $index = $this->getDynamicIndex($method, $pattern); |
|
| 319 | + return isset($this->dynamics[$index]) ? $this->dynamics[$index] : false; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + /** |
|
| 323 | + * @param string $method |
|
| 324 | + * @param string $pattern |
|
| 325 | + * |
|
| 326 | + * @return int |
|
| 327 | + */ |
|
| 328 | + |
|
| 329 | + protected function getDynamicIndex($method, $pattern) |
|
| 330 | + { |
|
| 331 | + return crc32(strtolower($method)) + substr_count($pattern, "/"); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * @return string[] |
|
| 336 | + */ |
|
| 337 | + |
|
| 338 | + public function getWildcards() |
|
| 339 | + { |
|
| 340 | + return $this->wildcards; |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * @param string $pattern |
|
| 345 | + * @param string $wildcard |
|
| 346 | + */ |
|
| 347 | + |
|
| 348 | + public function setWildcard($pattern, $wildcard) |
|
| 349 | + { |
|
| 350 | + $this->wildcards[$pattern] = $wildcard; |
|
| 351 | + } |
|
| 352 | 352 | |
| 353 | 353 | } |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | * @var array |
| 62 | 62 | */ |
| 63 | 63 | |
| 64 | - protected $statics = []; |
|
| 64 | + protected $statics = []; |
|
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | 67 | * The dynamic routes have parameters and are stored in a hashtable that every cell have |
@@ -123,10 +123,10 @@ discard block |
||
| 123 | 123 | return new Group($group); |
| 124 | 124 | } |
| 125 | 125 | |
| 126 | - public function get ($pattern, $action) { return $this->set("get" , $pattern, $action); } |
|
| 127 | - public function post ($pattern, $action) { return $this->set("post" , $pattern, $action); } |
|
| 128 | - public function put ($pattern, $action) { return $this->set("put" , $pattern, $action); } |
|
| 129 | - public function patch ($pattern, $action) { return $this->set("patch" , $pattern, $action); } |
|
| 126 | + public function get($pattern, $action) { return $this->set("get", $pattern, $action); } |
|
| 127 | + public function post($pattern, $action) { return $this->set("post", $pattern, $action); } |
|
| 128 | + public function put($pattern, $action) { return $this->set("put", $pattern, $action); } |
|
| 129 | + public function patch($pattern, $action) { return $this->set("patch", $pattern, $action); } |
|
| 130 | 130 | public function delete($pattern, $action) { return $this->set("delete", $pattern, $action); } |
| 131 | 131 | |
| 132 | 132 | /** |
@@ -117,7 +117,9 @@ discard block |
||
| 117 | 117 | if (strpos($pattern, "{") !== false) { |
| 118 | 118 | $index = $this->getDynamicIndex($method, $pattern); |
| 119 | 119 | $this->dynamics[$index][$pattern] = $route; |
| 120 | - } else $this->statics[$method][$pattern] = $route; |
|
| 120 | + } else { |
|
| 121 | + $this->statics[$method][$pattern] = $route; |
|
| 122 | + } |
|
| 121 | 123 | } |
| 122 | 124 | |
| 123 | 125 | return new Group($group); |
@@ -142,8 +144,9 @@ discard block |
||
| 142 | 144 | public function match(array $methods, $pattern, $action) |
| 143 | 145 | { |
| 144 | 146 | $group = []; |
| 145 | - foreach ($methods as $method) |
|
| 146 | - $group[] = $this->set($method, $pattern, $action); |
|
| 147 | + foreach ($methods as $method) { |
|
| 148 | + $group[] = $this->set($method, $pattern, $action); |
|
| 149 | + } |
|
| 147 | 150 | return new Group($group); |
| 148 | 151 | } |
| 149 | 152 | |
@@ -186,8 +189,9 @@ discard block |
||
| 186 | 189 | public function group(array $routes) |
| 187 | 190 | { |
| 188 | 191 | $group = []; |
| 189 | - foreach ($routes as $route) |
|
| 190 | - $group[] = $route; |
|
| 192 | + foreach ($routes as $route) { |
|
| 193 | + $group[] = $route; |
|
| 194 | + } |
|
| 191 | 195 | return new Group($group); |
| 192 | 196 | } |
| 193 | 197 | |
@@ -202,7 +206,9 @@ discard block |
||
| 202 | 206 | { |
| 203 | 207 | if (strpos($pattern, "{") === false) { |
| 204 | 208 | unset($this->statics[$method][$pattern]); |
| 205 | - } else unset($this->dynamics[$this->getDynamicIndex($method, $pattern)][$pattern]); |
|
| 209 | + } else { |
|
| 210 | + unset($this->dynamics[$this->getDynamicIndex($method, $pattern)][$pattern]); |
|
| 211 | + } |
|
| 206 | 212 | } |
| 207 | 213 | |
| 208 | 214 | /** |
@@ -257,7 +263,9 @@ discard block |
||
| 257 | 263 | if ($closingNumber !== count($segments) - 1) { |
| 258 | 264 | if (preg_match("~" . self::DYNAMIC_REGEX . "(*SKIP)(*F)|\]~x", $withoutClosing)) { |
| 259 | 265 | throw new BadRouteException(BadRouteException::OPTIONAL_SEGMENTS_ON_MIDDLE); |
| 260 | - } else throw new BadRouteException(BadRouteException::UNCLOSED_OPTIONAL_SEGMENTS); |
|
| 266 | + } else { |
|
| 267 | + throw new BadRouteException(BadRouteException::UNCLOSED_OPTIONAL_SEGMENTS); |
|
| 268 | + } |
|
| 261 | 269 | } |
| 262 | 270 | } |
| 263 | 271 | |
@@ -301,8 +309,9 @@ discard block |
||
| 301 | 309 | public function findStaticRoute($method, $pattern) |
| 302 | 310 | { |
| 303 | 311 | $method = strtolower($method); |
| 304 | - if (isset($this->statics[$method]) && isset($this->statics[$method][$pattern])) |
|
| 305 | - return $this->statics[$method][$pattern]; |
|
| 312 | + if (isset($this->statics[$method]) && isset($this->statics[$method][$pattern])) { |
|
| 313 | + return $this->statics[$method][$pattern]; |
|
| 314 | + } |
|
| 306 | 315 | return false; |
| 307 | 316 | } |
| 308 | 317 | |
@@ -22,102 +22,102 @@ |
||
| 22 | 22 | trait ResourceCollectorTrait |
| 23 | 23 | { |
| 24 | 24 | |
| 25 | - abstract public function set($method, $pattern, $action); |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * A map of all routes of resources. |
|
| 29 | - * |
|
| 30 | - * @var array |
|
| 31 | - */ |
|
| 32 | - |
|
| 33 | - protected $map = [ |
|
| 34 | - "index" => ["get", "/{name}"], |
|
| 35 | - "make" => ["get", "/{name}/make"], |
|
| 36 | - "create" => ["post", "/{name}"], |
|
| 37 | - "show" => ["get", "/{name}/{id:int+}"], |
|
| 38 | - "edit" => ["get", "/{name}/{id:int+}/edit"], |
|
| 39 | - "update" => ["put", "/{name}/{id:int+}"], |
|
| 40 | - "delete" => ["delete", "/{name}/{id:int+}"], |
|
| 41 | - ]; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. |
|
| 45 | - * Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, |
|
| 46 | - * a resourceful route declares them in a single line of code. |
|
| 47 | - * |
|
| 48 | - * @param string $controller The controller name. |
|
| 49 | - * @param array $options Some options like, "as" to name the route pattern, "only" to |
|
| 50 | - * explicit say that only this routes will be registered, and |
|
| 51 | - * "except" that register all the routes except the indicates. |
|
| 52 | - * @return Resource |
|
| 53 | - */ |
|
| 54 | - |
|
| 55 | - public function resource($controller, array $options = array()) |
|
| 56 | - { |
|
| 57 | - $name = isset($options["prefix"]) ? $options["prefix"] : ""; |
|
| 58 | - $name .= $this->getResourceName($controller, $options); |
|
| 59 | - $actions = $this->getResourceActions($options); |
|
| 60 | - $resource = new Resource; |
|
| 61 | - |
|
| 62 | - foreach ($actions as $action => $map) { |
|
| 63 | - $resource->set($this->set($map[0], $this->getResourcePath($action, $map[1], $name, $options), [$controller, $action])); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - return $resource; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * Collect several resources at same time. |
|
| 71 | - * |
|
| 72 | - * @param array $controllers Several controller names as parameters or an array with all controller names. |
|
| 73 | - * @return Resource |
|
| 74 | - */ |
|
| 75 | - |
|
| 76 | - public function resources(array $controllers) |
|
| 77 | - { |
|
| 78 | - $resource = new Resource; |
|
| 79 | - foreach ($controllers as $controller) |
|
| 80 | - $resource->set($this->resource($controller)); |
|
| 81 | - return $resource; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * @param string $controller |
|
| 86 | - * @param array $options |
|
| 87 | - * |
|
| 88 | - * @return mixed |
|
| 89 | - */ |
|
| 90 | - |
|
| 91 | - protected function getResourceName($controller, array $options) |
|
| 92 | - { |
|
| 93 | - return isset($options["as"]) ? $options["as"] : str_replace("controller", "", strtolower($controller)); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @param array $options |
|
| 98 | - * @return array |
|
| 99 | - */ |
|
| 100 | - |
|
| 101 | - protected function getResourceActions(array $options) |
|
| 102 | - { |
|
| 103 | - return isset($options["only"]) ? array_intersect_key($this->map, array_flip((array) $options["only"])) : |
|
| 104 | - (isset($options["except"]) ? array_diff_key($this->map, array_flip((array) $options["except"])) : $this->map); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param string $action |
|
| 109 | - * @param string $path |
|
| 110 | - * @param string $name |
|
| 111 | - * @param string[] $options |
|
| 112 | - * |
|
| 113 | - * @return string |
|
| 114 | - */ |
|
| 115 | - |
|
| 116 | - protected function getResourcePath($action, $path, $name, array $options) |
|
| 117 | - { |
|
| 118 | - return str_replace("{name}", $name, |
|
| 119 | - $action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) : |
|
| 120 | - ($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path)); |
|
| 121 | - } |
|
| 25 | + abstract public function set($method, $pattern, $action); |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * A map of all routes of resources. |
|
| 29 | + * |
|
| 30 | + * @var array |
|
| 31 | + */ |
|
| 32 | + |
|
| 33 | + protected $map = [ |
|
| 34 | + "index" => ["get", "/{name}"], |
|
| 35 | + "make" => ["get", "/{name}/make"], |
|
| 36 | + "create" => ["post", "/{name}"], |
|
| 37 | + "show" => ["get", "/{name}/{id:int+}"], |
|
| 38 | + "edit" => ["get", "/{name}/{id:int+}/edit"], |
|
| 39 | + "update" => ["put", "/{name}/{id:int+}"], |
|
| 40 | + "delete" => ["delete", "/{name}/{id:int+}"], |
|
| 41 | + ]; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. |
|
| 45 | + * Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, |
|
| 46 | + * a resourceful route declares them in a single line of code. |
|
| 47 | + * |
|
| 48 | + * @param string $controller The controller name. |
|
| 49 | + * @param array $options Some options like, "as" to name the route pattern, "only" to |
|
| 50 | + * explicit say that only this routes will be registered, and |
|
| 51 | + * "except" that register all the routes except the indicates. |
|
| 52 | + * @return Resource |
|
| 53 | + */ |
|
| 54 | + |
|
| 55 | + public function resource($controller, array $options = array()) |
|
| 56 | + { |
|
| 57 | + $name = isset($options["prefix"]) ? $options["prefix"] : ""; |
|
| 58 | + $name .= $this->getResourceName($controller, $options); |
|
| 59 | + $actions = $this->getResourceActions($options); |
|
| 60 | + $resource = new Resource; |
|
| 61 | + |
|
| 62 | + foreach ($actions as $action => $map) { |
|
| 63 | + $resource->set($this->set($map[0], $this->getResourcePath($action, $map[1], $name, $options), [$controller, $action])); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + return $resource; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * Collect several resources at same time. |
|
| 71 | + * |
|
| 72 | + * @param array $controllers Several controller names as parameters or an array with all controller names. |
|
| 73 | + * @return Resource |
|
| 74 | + */ |
|
| 75 | + |
|
| 76 | + public function resources(array $controllers) |
|
| 77 | + { |
|
| 78 | + $resource = new Resource; |
|
| 79 | + foreach ($controllers as $controller) |
|
| 80 | + $resource->set($this->resource($controller)); |
|
| 81 | + return $resource; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * @param string $controller |
|
| 86 | + * @param array $options |
|
| 87 | + * |
|
| 88 | + * @return mixed |
|
| 89 | + */ |
|
| 90 | + |
|
| 91 | + protected function getResourceName($controller, array $options) |
|
| 92 | + { |
|
| 93 | + return isset($options["as"]) ? $options["as"] : str_replace("controller", "", strtolower($controller)); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @param array $options |
|
| 98 | + * @return array |
|
| 99 | + */ |
|
| 100 | + |
|
| 101 | + protected function getResourceActions(array $options) |
|
| 102 | + { |
|
| 103 | + return isset($options["only"]) ? array_intersect_key($this->map, array_flip((array) $options["only"])) : |
|
| 104 | + (isset($options["except"]) ? array_diff_key($this->map, array_flip((array) $options["except"])) : $this->map); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param string $action |
|
| 109 | + * @param string $path |
|
| 110 | + * @param string $name |
|
| 111 | + * @param string[] $options |
|
| 112 | + * |
|
| 113 | + * @return string |
|
| 114 | + */ |
|
| 115 | + |
|
| 116 | + protected function getResourcePath($action, $path, $name, array $options) |
|
| 117 | + { |
|
| 118 | + return str_replace("{name}", $name, |
|
| 119 | + $action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) : |
|
| 120 | + ($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path)); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | 123 | } |
@@ -31,12 +31,12 @@ discard block |
||
| 31 | 31 | */ |
| 32 | 32 | |
| 33 | 33 | protected $map = [ |
| 34 | - "index" => ["get", "/{name}"], |
|
| 35 | - "make" => ["get", "/{name}/make"], |
|
| 36 | - "create" => ["post", "/{name}"], |
|
| 37 | - "show" => ["get", "/{name}/{id:int+}"], |
|
| 38 | - "edit" => ["get", "/{name}/{id:int+}/edit"], |
|
| 39 | - "update" => ["put", "/{name}/{id:int+}"], |
|
| 34 | + "index" => ["get", "/{name}"], |
|
| 35 | + "make" => ["get", "/{name}/make"], |
|
| 36 | + "create" => ["post", "/{name}"], |
|
| 37 | + "show" => ["get", "/{name}/{id:int+}"], |
|
| 38 | + "edit" => ["get", "/{name}/{id:int+}/edit"], |
|
| 39 | + "update" => ["put", "/{name}/{id:int+}"], |
|
| 40 | 40 | "delete" => ["delete", "/{name}/{id:int+}"], |
| 41 | 41 | ]; |
| 42 | 42 | |
@@ -100,8 +100,7 @@ discard block |
||
| 100 | 100 | |
| 101 | 101 | protected function getResourceActions(array $options) |
| 102 | 102 | { |
| 103 | - return isset($options["only"]) ? array_intersect_key($this->map, array_flip((array) $options["only"])) : |
|
| 104 | - (isset($options["except"]) ? array_diff_key($this->map, array_flip((array) $options["except"])) : $this->map); |
|
| 103 | + return isset($options["only"]) ? array_intersect_key($this->map, array_flip((array) $options["only"])) : (isset($options["except"]) ? array_diff_key($this->map, array_flip((array) $options["except"])) : $this->map); |
|
| 105 | 104 | } |
| 106 | 105 | |
| 107 | 106 | /** |
@@ -116,8 +115,7 @@ discard block |
||
| 116 | 115 | protected function getResourcePath($action, $path, $name, array $options) |
| 117 | 116 | { |
| 118 | 117 | return str_replace("{name}", $name, |
| 119 | - $action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) : |
|
| 120 | - ($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path)); |
|
| 118 | + $action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) : ($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path)); |
|
| 121 | 119 | } |
| 122 | 120 | |
| 123 | 121 | } |
@@ -76,8 +76,9 @@ |
||
| 76 | 76 | public function resources(array $controllers) |
| 77 | 77 | { |
| 78 | 78 | $resource = new Resource; |
| 79 | - foreach ($controllers as $controller) |
|
| 80 | - $resource->set($this->resource($controller)); |
|
| 79 | + foreach ($controllers as $controller) { |
|
| 80 | + $resource->set($this->resource($controller)); |
|
| 81 | + } |
|
| 81 | 82 | return $resource; |
| 82 | 83 | } |
| 83 | 84 | |
@@ -20,8 +20,8 @@ |
||
| 20 | 20 | class BadDispatchStrategyException extends BadRouteException |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - public function __construct($strategy) { |
|
| 24 | - parent::__construct("`$strategy` is not a valid route dispatch strategy, it must implement the `Codeburner\Router\DispatchStrategies\DispatchStrategyInterface` interface."); |
|
| 25 | - } |
|
| 23 | + public function __construct($strategy) { |
|
| 24 | + parent::__construct("`$strategy` is not a valid route dispatch strategy, it must implement the `Codeburner\Router\DispatchStrategies\DispatchStrategyInterface` interface."); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | 27 | } |
@@ -20,8 +20,8 @@ |
||
| 20 | 20 | class BadRouteException extends \Exception |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - const OPTIONAL_SEGMENTS_ON_MIDDLE = "Optional segments can only occur at the end of a route."; |
|
| 24 | - const UNCLOSED_OPTIONAL_SEGMENTS = "Number of opening [ and closing ] does not match."; |
|
| 25 | - const EMPTY_OPTIONAL_PARTS = "Empty optional part."; |
|
| 23 | + const OPTIONAL_SEGMENTS_ON_MIDDLE = "Optional segments can only occur at the end of a route."; |
|
| 24 | + const UNCLOSED_OPTIONAL_SEGMENTS = "Number of opening [ and closing ] does not match."; |
|
| 25 | + const EMPTY_OPTIONAL_PARTS = "Empty optional part."; |
|
| 26 | 26 | |
| 27 | 27 | } |
@@ -20,71 +20,71 @@ |
||
| 20 | 20 | class MethodNotAllowedException extends BadRouteException |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * The HTTP method from request. |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 23 | + /** |
|
| 24 | + * The HTTP method from request. |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | 28 | |
| 29 | - public $requestedMethod; |
|
| 29 | + public $requestedMethod; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * The requested URi. |
|
| 33 | - * |
|
| 34 | - * @var string |
|
| 35 | - */ |
|
| 31 | + /** |
|
| 32 | + * The requested URi. |
|
| 33 | + * |
|
| 34 | + * @var string |
|
| 35 | + */ |
|
| 36 | 36 | |
| 37 | - public $requestedUri; |
|
| 37 | + public $requestedUri; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * All the allowed HTTP methods and routes for the request. |
|
| 41 | - * |
|
| 42 | - * @var array |
|
| 43 | - */ |
|
| 39 | + /** |
|
| 40 | + * All the allowed HTTP methods and routes for the request. |
|
| 41 | + * |
|
| 42 | + * @var array |
|
| 43 | + */ |
|
| 44 | 44 | |
| 45 | - public $allowedMethods; |
|
| 45 | + public $allowedMethods; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Exception constructor. |
|
| 49 | - * |
|
| 50 | - * @param string $requestedMethod The request HTTP method. |
|
| 51 | - * @param string $requestedUri The request URi. |
|
| 52 | - * @param array $allowedMethods All the allowed HTTP methods and routes for the request. |
|
| 53 | - * @param string $message The exception error message. |
|
| 54 | - * @param integer $code The exception error code. |
|
| 55 | - */ |
|
| 47 | + /** |
|
| 48 | + * Exception constructor. |
|
| 49 | + * |
|
| 50 | + * @param string $requestedMethod The request HTTP method. |
|
| 51 | + * @param string $requestedUri The request URi. |
|
| 52 | + * @param array $allowedMethods All the allowed HTTP methods and routes for the request. |
|
| 53 | + * @param string $message The exception error message. |
|
| 54 | + * @param integer $code The exception error code. |
|
| 55 | + */ |
|
| 56 | 56 | |
| 57 | - public function __construct($requestedMethod, $requestedUri, array $allowedMethods, $message = null, $code = 405) |
|
| 58 | - { |
|
| 59 | - $this->requestedMethod = $requestedMethod; |
|
| 60 | - $this->requestedUri = $requestedUri; |
|
| 61 | - $this->allowedMethods = $allowedMethods; |
|
| 62 | - parent::__construct($message, $code); |
|
| 63 | - } |
|
| 57 | + public function __construct($requestedMethod, $requestedUri, array $allowedMethods, $message = null, $code = 405) |
|
| 58 | + { |
|
| 59 | + $this->requestedMethod = $requestedMethod; |
|
| 60 | + $this->requestedUri = $requestedUri; |
|
| 61 | + $this->allowedMethods = $allowedMethods; |
|
| 62 | + parent::__construct($message, $code); |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * Verify if the given HTTP method is allowed for the request. |
|
| 67 | - * |
|
| 68 | - * @param string An HTTP method |
|
| 69 | - * @return bool |
|
| 70 | - */ |
|
| 65 | + /** |
|
| 66 | + * Verify if the given HTTP method is allowed for the request. |
|
| 67 | + * |
|
| 68 | + * @param string An HTTP method |
|
| 69 | + * @return bool |
|
| 70 | + */ |
|
| 71 | 71 | |
| 72 | - public function can($method) |
|
| 73 | - { |
|
| 74 | - return isset($this->allowedMethods[strtoupper($method)]); |
|
| 75 | - } |
|
| 72 | + public function can($method) |
|
| 73 | + { |
|
| 74 | + return isset($this->allowedMethods[strtoupper($method)]); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - /** |
|
| 78 | - * The HTTP specification requires that a 405 Method Not Allowed response include the |
|
| 79 | - * Allow: header to detail available methods for the requested resource. |
|
| 80 | - * |
|
| 81 | - * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html section 14.7 |
|
| 82 | - * @return string |
|
| 83 | - */ |
|
| 77 | + /** |
|
| 78 | + * The HTTP specification requires that a 405 Method Not Allowed response include the |
|
| 79 | + * Allow: header to detail available methods for the requested resource. |
|
| 80 | + * |
|
| 81 | + * @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html section 14.7 |
|
| 82 | + * @return string |
|
| 83 | + */ |
|
| 84 | 84 | |
| 85 | - public function allowed() |
|
| 86 | - { |
|
| 87 | - return implode(', ', array_keys($this->allowedMethods)); |
|
| 88 | - } |
|
| 85 | + public function allowed() |
|
| 86 | + { |
|
| 87 | + return implode(', ', array_keys($this->allowedMethods)); |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | 90 | } |
@@ -20,8 +20,8 @@ |
||
| 20 | 20 | class MethodNotSupportedException extends BadRouteException |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - public function __construct($method) { |
|
| 24 | - parent::__construct("The HTTP method '$method' is not supported by the route collector."); |
|
| 25 | - } |
|
| 23 | + public function __construct($method) { |
|
| 24 | + parent::__construct("The HTTP method '$method' is not supported by the route collector."); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | 27 | } |
@@ -19,36 +19,36 @@ |
||
| 19 | 19 | class NotFoundException extends \Exception |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * The HTTP method from request. |
|
| 24 | - * |
|
| 25 | - * @var string |
|
| 26 | - */ |
|
| 22 | + /** |
|
| 23 | + * The HTTP method from request. |
|
| 24 | + * |
|
| 25 | + * @var string |
|
| 26 | + */ |
|
| 27 | 27 | |
| 28 | - public $requestedMethod; |
|
| 28 | + public $requestedMethod; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * The requested Path. |
|
| 32 | - * |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 30 | + /** |
|
| 31 | + * The requested Path. |
|
| 32 | + * |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | 35 | |
| 36 | - public $requestedPath; |
|
| 36 | + public $requestedPath; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * Exception constructor. |
|
| 40 | - * |
|
| 41 | - * @param string $requestedMethod The request HTTP method. |
|
| 42 | - * @param string $requestedPath The request Path. |
|
| 43 | - * @param string $message The exception error message. |
|
| 44 | - * @param integer $code The exception error code. |
|
| 45 | - */ |
|
| 46 | - |
|
| 47 | - public function __construct($requestedMethod, $requestedPath, $message = null, $code = 405) |
|
| 48 | - { |
|
| 49 | - $this->requestedMethod = $requestedMethod; |
|
| 50 | - $this->requestedPath = $requestedPath; |
|
| 51 | - parent::__construct($message, $code); |
|
| 52 | - } |
|
| 38 | + /** |
|
| 39 | + * Exception constructor. |
|
| 40 | + * |
|
| 41 | + * @param string $requestedMethod The request HTTP method. |
|
| 42 | + * @param string $requestedPath The request Path. |
|
| 43 | + * @param string $message The exception error message. |
|
| 44 | + * @param integer $code The exception error code. |
|
| 45 | + */ |
|
| 46 | + |
|
| 47 | + public function __construct($requestedMethod, $requestedPath, $message = null, $code = 405) |
|
| 48 | + { |
|
| 49 | + $this->requestedMethod = $requestedMethod; |
|
| 50 | + $this->requestedPath = $requestedPath; |
|
| 51 | + parent::__construct($message, $code); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | 54 | } |