@@ -20,16 +20,16 @@ |
||
| 20 | 20 | interface MatcherAwareInterface |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @param \Codeburner\Router\Matcher $matcher |
|
| 25 | - */ |
|
| 23 | + /** |
|
| 24 | + * @param \Codeburner\Router\Matcher $matcher |
|
| 25 | + */ |
|
| 26 | 26 | |
| 27 | - public function setMatcher(\Codeburner\Router\Matcher $matcher); |
|
| 27 | + public function setMatcher(\Codeburner\Router\Matcher $matcher); |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * @return \Codeburner\Router\Matcher |
|
| 31 | - */ |
|
| 29 | + /** |
|
| 30 | + * @return \Codeburner\Router\Matcher |
|
| 31 | + */ |
|
| 32 | 32 | |
| 33 | - public function getMatcher(); |
|
| 33 | + public function getMatcher(); |
|
| 34 | 34 | |
| 35 | 35 | } |
| 36 | 36 | \ No newline at end of file |
@@ -19,13 +19,13 @@ |
||
| 19 | 19 | interface StrategyInterface |
| 20 | 20 | { |
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Dispatch the matched route action. |
|
| 24 | - * |
|
| 25 | - * @param \Codeburner\Router\Route $route |
|
| 26 | - * @return mixed The response of request. |
|
| 27 | - */ |
|
| 22 | + /** |
|
| 23 | + * Dispatch the matched route action. |
|
| 24 | + * |
|
| 25 | + * @param \Codeburner\Router\Route $route |
|
| 26 | + * @return mixed The response of request. |
|
| 27 | + */ |
|
| 28 | 28 | |
| 29 | - public function call(\Codeburner\Router\Route $route); |
|
| 29 | + public function call(\Codeburner\Router\Route $route); |
|
| 30 | 30 | |
| 31 | 31 | } |
@@ -15,7 +15,6 @@ |
||
| 15 | 15 | use ReflectionClass; |
| 16 | 16 | use ReflectionMethod; |
| 17 | 17 | use ReflectionParameter; |
| 18 | -use Reflector; |
|
| 19 | 18 | |
| 20 | 19 | /** |
| 21 | 20 | * Methods for enable the collector to make routes from a controller. |
@@ -26,224 +26,224 @@ |
||
| 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 | - $strategy = $this->getAnnotatedStrategy($method); |
|
| 131 | - |
|
| 132 | - /** @var \Codeburner\Router\Route $route */ |
|
| 133 | - $route = $this->set($http, "$action$dynamic", [$controller->name, $method->name]); |
|
| 134 | - |
|
| 135 | - if ($strategy !== null) { |
|
| 136 | - $route->setStrategy($strategy); |
|
| 137 | - } else $route->setStrategy($controllerDefaultStrategy); |
|
| 138 | - |
|
| 139 | - $group[] = $route; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - return new Group($group); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * @param ReflectionClass $controller |
|
| 148 | - * |
|
| 149 | - * @return string |
|
| 150 | - */ |
|
| 151 | - |
|
| 152 | - protected function getControllerPrefix(ReflectionClass $controller) |
|
| 153 | - { |
|
| 154 | - preg_match("~\@prefix\s([a-zA-Z\\\_]+)~i", (string) $controller->getDocComment(), $prefix); |
|
| 155 | - return isset($prefix[1]) ? $prefix[1] : str_replace("controller", "", strtolower($controller->getShortName())); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * @param \ReflectionMethod |
|
| 160 | - * @return string |
|
| 161 | - */ |
|
| 162 | - |
|
| 163 | - protected function getMethodConstraints(ReflectionMethod $method) |
|
| 164 | - { |
|
| 165 | - $beginPath = ""; |
|
| 166 | - $endPath = ""; |
|
| 167 | - |
|
| 168 | - if ($parameters = $method->getParameters()) { |
|
| 169 | - $types = $this->getParamsConstraint($method); |
|
| 170 | - |
|
| 171 | - foreach ($parameters as $parameter) { |
|
| 172 | - if ($parameter->isOptional()) { |
|
| 173 | - $beginPath .= "["; |
|
| 174 | - $endPath .= "]"; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - $beginPath .= $this->getPathConstraint($parameter, $types); |
|
| 178 | - } |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - return $beginPath . $endPath; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * @param ReflectionParameter $parameter |
|
| 186 | - * @param string[] $types |
|
| 187 | - * @return string |
|
| 188 | - */ |
|
| 189 | - |
|
| 190 | - protected function getPathConstraint(ReflectionParameter $parameter, $types) |
|
| 191 | - { |
|
| 192 | - $name = $parameter->name; |
|
| 193 | - $path = "/{" . $name; |
|
| 194 | - return isset($types[$name]) ? "$path:{$types[$name]}}" : "$path}"; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @param ReflectionMethod $method |
|
| 199 | - * @return string[] |
|
| 200 | - */ |
|
| 201 | - |
|
| 202 | - protected function getParamsConstraint(ReflectionMethod $method) |
|
| 203 | - { |
|
| 204 | - $params = []; |
|
| 205 | - preg_match_all("~\@param\s(" . implode("|", array_keys($this->getWildcards())) . "|\(.+\))\s\\$([a-zA-Z0-1_]+)~i", |
|
| 206 | - $method->getDocComment(), $types, PREG_SET_ORDER); |
|
| 207 | - |
|
| 208 | - foreach ((array) $types as $type) { |
|
| 209 | - // if a pattern is defined on Match take it otherwise take the param type by PHPDoc. |
|
| 210 | - $params[$type[2]] = isset($type[4]) ? $type[4] : $type[1]; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - return $params; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * @param ReflectionClass|ReflectionMethod $reflector |
|
| 218 | - * @return string|null |
|
| 219 | - */ |
|
| 220 | - |
|
| 221 | - protected function getAnnotatedStrategy($reflector) |
|
| 222 | - { |
|
| 223 | - preg_match("~\@strategy\s([a-zA-Z\\\_]+)~i", (string) $reflector->getDocComment(), $strategy); |
|
| 224 | - return isset($strategy[1]) ? $strategy[1] : null; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Define how controller actions names will be joined to form the route pattern. |
|
| 229 | - * Defaults to "/" so actions like "getMyAction" will be "/my/action". If changed to |
|
| 230 | - * "-" the new pattern will be "/my-action". |
|
| 231 | - * |
|
| 232 | - * @param string $join |
|
| 233 | - */ |
|
| 234 | - |
|
| 235 | - public function setControllerActionJoin($join) |
|
| 236 | - { |
|
| 237 | - $this->controllerActionJoin = $join; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * @return string |
|
| 242 | - */ |
|
| 243 | - |
|
| 244 | - public function getControllerActionJoin() |
|
| 245 | - { |
|
| 246 | - return $this->controllerActionJoin; |
|
| 247 | - } |
|
| 127 | + if (strpos(Collector::HTTP_METHODS, $http) !== false) { |
|
| 128 | + $action = $prefix . strtolower(implode($this->controllerActionJoin, $name)); |
|
| 129 | + $dynamic = $this->getMethodConstraints($method); |
|
| 130 | + $strategy = $this->getAnnotatedStrategy($method); |
|
| 131 | + |
|
| 132 | + /** @var \Codeburner\Router\Route $route */ |
|
| 133 | + $route = $this->set($http, "$action$dynamic", [$controller->name, $method->name]); |
|
| 134 | + |
|
| 135 | + if ($strategy !== null) { |
|
| 136 | + $route->setStrategy($strategy); |
|
| 137 | + } else $route->setStrategy($controllerDefaultStrategy); |
|
| 138 | + |
|
| 139 | + $group[] = $route; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + return new Group($group); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * @param ReflectionClass $controller |
|
| 148 | + * |
|
| 149 | + * @return string |
|
| 150 | + */ |
|
| 151 | + |
|
| 152 | + protected function getControllerPrefix(ReflectionClass $controller) |
|
| 153 | + { |
|
| 154 | + preg_match("~\@prefix\s([a-zA-Z\\\_]+)~i", (string) $controller->getDocComment(), $prefix); |
|
| 155 | + return isset($prefix[1]) ? $prefix[1] : str_replace("controller", "", strtolower($controller->getShortName())); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * @param \ReflectionMethod |
|
| 160 | + * @return string |
|
| 161 | + */ |
|
| 162 | + |
|
| 163 | + protected function getMethodConstraints(ReflectionMethod $method) |
|
| 164 | + { |
|
| 165 | + $beginPath = ""; |
|
| 166 | + $endPath = ""; |
|
| 167 | + |
|
| 168 | + if ($parameters = $method->getParameters()) { |
|
| 169 | + $types = $this->getParamsConstraint($method); |
|
| 170 | + |
|
| 171 | + foreach ($parameters as $parameter) { |
|
| 172 | + if ($parameter->isOptional()) { |
|
| 173 | + $beginPath .= "["; |
|
| 174 | + $endPath .= "]"; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + $beginPath .= $this->getPathConstraint($parameter, $types); |
|
| 178 | + } |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + return $beginPath . $endPath; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * @param ReflectionParameter $parameter |
|
| 186 | + * @param string[] $types |
|
| 187 | + * @return string |
|
| 188 | + */ |
|
| 189 | + |
|
| 190 | + protected function getPathConstraint(ReflectionParameter $parameter, $types) |
|
| 191 | + { |
|
| 192 | + $name = $parameter->name; |
|
| 193 | + $path = "/{" . $name; |
|
| 194 | + return isset($types[$name]) ? "$path:{$types[$name]}}" : "$path}"; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @param ReflectionMethod $method |
|
| 199 | + * @return string[] |
|
| 200 | + */ |
|
| 201 | + |
|
| 202 | + protected function getParamsConstraint(ReflectionMethod $method) |
|
| 203 | + { |
|
| 204 | + $params = []; |
|
| 205 | + preg_match_all("~\@param\s(" . implode("|", array_keys($this->getWildcards())) . "|\(.+\))\s\\$([a-zA-Z0-1_]+)~i", |
|
| 206 | + $method->getDocComment(), $types, PREG_SET_ORDER); |
|
| 207 | + |
|
| 208 | + foreach ((array) $types as $type) { |
|
| 209 | + // if a pattern is defined on Match take it otherwise take the param type by PHPDoc. |
|
| 210 | + $params[$type[2]] = isset($type[4]) ? $type[4] : $type[1]; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + return $params; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * @param ReflectionClass|ReflectionMethod $reflector |
|
| 218 | + * @return string|null |
|
| 219 | + */ |
|
| 220 | + |
|
| 221 | + protected function getAnnotatedStrategy($reflector) |
|
| 222 | + { |
|
| 223 | + preg_match("~\@strategy\s([a-zA-Z\\\_]+)~i", (string) $reflector->getDocComment(), $strategy); |
|
| 224 | + return isset($strategy[1]) ? $strategy[1] : null; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Define how controller actions names will be joined to form the route pattern. |
|
| 229 | + * Defaults to "/" so actions like "getMyAction" will be "/my/action". If changed to |
|
| 230 | + * "-" the new pattern will be "/my-action". |
|
| 231 | + * |
|
| 232 | + * @param string $join |
|
| 233 | + */ |
|
| 234 | + |
|
| 235 | + public function setControllerActionJoin($join) |
|
| 236 | + { |
|
| 237 | + $this->controllerActionJoin = $join; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * @return string |
|
| 242 | + */ |
|
| 243 | + |
|
| 244 | + public function getControllerActionJoin() |
|
| 245 | + { |
|
| 246 | + return $this->controllerActionJoin; |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | 249 | } |
@@ -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 | |
@@ -134,7 +136,9 @@ discard block |
||
| 134 | 136 | |
| 135 | 137 | if ($strategy !== null) { |
| 136 | 138 | $route->setStrategy($strategy); |
| 137 | - } else $route->setStrategy($controllerDefaultStrategy); |
|
| 139 | + } else { |
|
| 140 | + $route->setStrategy($controllerDefaultStrategy); |
|
| 141 | + } |
|
| 138 | 142 | |
| 139 | 143 | $group[] = $route; |
| 140 | 144 | } |
@@ -526,7 +526,7 @@ |
||
| 526 | 526 | |
| 527 | 527 | /** |
| 528 | 528 | * @param string $key |
| 529 | - * @param mixed $value |
|
| 529 | + * @param string $value |
|
| 530 | 530 | * |
| 531 | 531 | * @return $this |
| 532 | 532 | */ |
@@ -23,586 +23,586 @@ |
||
| 23 | 23 | class Route |
| 24 | 24 | { |
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var Collector |
|
| 28 | - */ |
|
| 26 | + /** |
|
| 27 | + * @var Collector |
|
| 28 | + */ |
|
| 29 | 29 | |
| 30 | - protected $collector; |
|
| 30 | + protected $collector; |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * @var string |
|
| 34 | - */ |
|
| 32 | + /** |
|
| 33 | + * @var string |
|
| 34 | + */ |
|
| 35 | 35 | |
| 36 | - protected $method; |
|
| 36 | + protected $method; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * @var string |
|
| 40 | - */ |
|
| 38 | + /** |
|
| 39 | + * @var string |
|
| 40 | + */ |
|
| 41 | 41 | |
| 42 | - protected $pattern; |
|
| 42 | + protected $pattern; |
|
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @var string|array|\Closure |
|
| 46 | - */ |
|
| 44 | + /** |
|
| 45 | + * @var string|array|\Closure |
|
| 46 | + */ |
|
| 47 | 47 | |
| 48 | - protected $action; |
|
| 48 | + protected $action; |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @var string |
|
| 52 | - */ |
|
| 53 | - |
|
| 54 | - protected $namespace = ""; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var string[] |
|
| 58 | - */ |
|
| 59 | - |
|
| 60 | - protected $params = []; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Defaults are parameters set by the user, and don't |
|
| 64 | - * appear on the pattern. |
|
| 65 | - * |
|
| 66 | - * @var array |
|
| 67 | - */ |
|
| 68 | - |
|
| 69 | - protected $defaults = []; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Metadata can be set to be used on filters, dispatch strategies |
|
| 73 | - * or anywhere the route object is used. |
|
| 74 | - * |
|
| 75 | - * @var array |
|
| 76 | - */ |
|
| 77 | - |
|
| 78 | - protected $metadata = []; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @var null|string|StrategyInterface |
|
| 82 | - */ |
|
| 83 | - |
|
| 84 | - protected $strategy; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Blocked routes are dynamic routes selected to pass by the matcher. |
|
| 88 | - * |
|
| 89 | - * @var boolean |
|
| 90 | - */ |
|
| 91 | - |
|
| 92 | - protected $blocked = false; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * The matcher that dispatched this route. |
|
| 96 | - * |
|
| 97 | - * @var Matcher $matcher |
|
| 98 | - */ |
|
| 99 | - |
|
| 100 | - protected $matcher; |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * The function used to create controllers from name. |
|
| 104 | - * |
|
| 105 | - * @var null|string|array|\Closure |
|
| 106 | - */ |
|
| 107 | - |
|
| 108 | - protected $controllerCreationFunction; |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * @param Collector $collector |
|
| 112 | - * @param string $method |
|
| 113 | - * @param string $pattern |
|
| 114 | - * @param string|array|\Closure $action |
|
| 115 | - */ |
|
| 116 | - |
|
| 117 | - public function __construct(Collector $collector, $method, $pattern, $action) |
|
| 118 | - { |
|
| 119 | - $this->collector = $collector; |
|
| 120 | - $this->method = $method; |
|
| 121 | - $this->pattern = $pattern; |
|
| 122 | - $this->action = $action; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Clone this route and set it into the collector. |
|
| 127 | - * |
|
| 128 | - * @return Route |
|
| 129 | - */ |
|
| 130 | - |
|
| 131 | - public function reset() |
|
| 132 | - { |
|
| 133 | - return $this->collector->set($this->method, $this->pattern, $this->action)->nth(0) |
|
| 134 | - ->setStrategy($this->strategy)->setParams($this->params) |
|
| 135 | - ->setDefaults($this->defaults)->setMetadataArray($this->metadata); |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Remove this route from the collector. |
|
| 140 | - * |
|
| 141 | - * @return self |
|
| 142 | - */ |
|
| 143 | - |
|
| 144 | - public function forget() |
|
| 145 | - { |
|
| 146 | - $this->collector->forget($this->method, $this->pattern); |
|
| 147 | - return $this; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Blocking a route indicate that that route have been selected and |
|
| 152 | - * parsed, now it will be given to the matcher. |
|
| 153 | - * |
|
| 154 | - * @return self |
|
| 155 | - */ |
|
| 156 | - |
|
| 157 | - public function block() |
|
| 158 | - { |
|
| 159 | - $this->blocked = true; |
|
| 160 | - return $this; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Verify if a Route have already been blocked. |
|
| 165 | - * |
|
| 166 | - * @return boolean |
|
| 167 | - */ |
|
| 168 | - |
|
| 169 | - public function blocked() |
|
| 170 | - { |
|
| 171 | - return $this->blocked; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Execute the route action, if no strategy was provided the action |
|
| 176 | - * will be executed by the call_user_func PHP function. |
|
| 177 | - * |
|
| 178 | - * @throws BadRouteException |
|
| 179 | - * @return mixed |
|
| 180 | - */ |
|
| 181 | - |
|
| 182 | - public function call() |
|
| 183 | - { |
|
| 184 | - $this->action = $this->parseCallable($this->action); |
|
| 185 | - |
|
| 186 | - if ($this->strategy === null) { |
|
| 187 | - return call_user_func_array($this->action, array_merge($this->defaults, $this->params)); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - if (!is_object($this->strategy)) { |
|
| 191 | - $this->strategy = new $this->strategy; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - return $this->callWithStrategy(); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * Seek for dynamic content on callables. eg. routes action controller#action |
|
| 199 | - * syntax allow to use the variables to build the string like: {controller}@{action} |
|
| 200 | - * |
|
| 201 | - * @param string|array|\Closure $callable |
|
| 202 | - * @return string|array|\Closure |
|
| 203 | - */ |
|
| 204 | - |
|
| 205 | - private function parseCallable($callable) |
|
| 206 | - { |
|
| 207 | - if (is_string($callable) && strpos($callable, "@")) { |
|
| 208 | - $callable = explode("@", $callable); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - if (is_array($callable)) { |
|
| 212 | - if (is_string($callable[0])) { |
|
| 213 | - $callable[0] = $this->parseCallableController($callable[0]); |
|
| 214 | - $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
| 215 | - } else $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - return $callable; |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Get the controller object. |
|
| 223 | - * |
|
| 224 | - * @param string $controller |
|
| 225 | - * @return Object |
|
| 226 | - */ |
|
| 227 | - |
|
| 228 | - private function parseCallableController($controller) |
|
| 229 | - { |
|
| 230 | - $controller = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($controller); |
|
| 231 | - |
|
| 232 | - if ($this->controllerCreationFunction === null) { |
|
| 233 | - return new $controller; |
|
| 234 | - } else return call_user_func($this->controllerCreationFunction, $controller); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Parse and replace dynamic content on route action. |
|
| 239 | - * |
|
| 240 | - * @param string $fragment Part of callable |
|
| 241 | - * @return string |
|
| 242 | - */ |
|
| 243 | - |
|
| 244 | - private function parseCallablePlaceholders($fragment) |
|
| 245 | - { |
|
| 246 | - if (strpos($fragment, "{") !== false) { |
|
| 247 | - foreach ($this->params as $placeholder => $value) { |
|
| 248 | - if (strpos($fragment, "{" . $placeholder . "}") !== false) { |
|
| 249 | - $fragment = str_replace("{" . $placeholder . "}", ucwords(str_replace("-", " ", $value)), $fragment); |
|
| 250 | - } |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return $fragment; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * Execute the route action with the given strategy. |
|
| 259 | - * |
|
| 260 | - * @throws BadRouteException |
|
| 261 | - * @return mixed |
|
| 262 | - */ |
|
| 263 | - |
|
| 264 | - private function callWithStrategy() |
|
| 265 | - { |
|
| 266 | - if ($this->strategy instanceof StrategyInterface) { |
|
| 267 | - if ($this->strategy instanceof MatcherAwareInterface) { |
|
| 268 | - $this->strategy->setMatcher($this->matcher); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - return $this->strategy->call($this); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - throw new BadRouteException("`$this->strategy` is not a valid route dispatch strategy, ". |
|
| 275 | - "it must implement the `Codeburner\Router\Strategies\StrategyInterface` interface."); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * @return Collector |
|
| 280 | - */ |
|
| 281 | - |
|
| 282 | - public function getCollector() |
|
| 283 | - { |
|
| 284 | - return $this->collector; |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - /** |
|
| 288 | - * @return string |
|
| 289 | - */ |
|
| 290 | - |
|
| 291 | - public function getMethod() |
|
| 292 | - { |
|
| 293 | - return $this->method; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * @return string |
|
| 298 | - */ |
|
| 299 | - |
|
| 300 | - public function getPattern() |
|
| 301 | - { |
|
| 302 | - return $this->pattern; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * @return string[] |
|
| 307 | - */ |
|
| 308 | - |
|
| 309 | - public function getSegments() |
|
| 310 | - { |
|
| 311 | - return explode("/", $this->pattern); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * @return string|array|\Closure |
|
| 316 | - */ |
|
| 317 | - |
|
| 318 | - public function getAction() |
|
| 319 | - { |
|
| 320 | - return $this->action; |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * @return string |
|
| 325 | - */ |
|
| 326 | - |
|
| 327 | - public function getNamespace() |
|
| 328 | - { |
|
| 329 | - return $this->namespace; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * @return string[] |
|
| 334 | - */ |
|
| 335 | - |
|
| 336 | - public function getParams() |
|
| 337 | - { |
|
| 338 | - return $this->params; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * @param string $key |
|
| 343 | - * @return string |
|
| 344 | - */ |
|
| 345 | - |
|
| 346 | - public function getParam($key) |
|
| 347 | - { |
|
| 348 | - return $this->params[$key]; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * @return array |
|
| 353 | - */ |
|
| 354 | - |
|
| 355 | - public function getDefaults() |
|
| 356 | - { |
|
| 357 | - return $this->defaults; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * @param string $key |
|
| 362 | - * @return mixed |
|
| 363 | - */ |
|
| 364 | - |
|
| 365 | - public function getDefault($key) |
|
| 366 | - { |
|
| 367 | - return $this->defaults[$key]; |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * @return array |
|
| 372 | - */ |
|
| 373 | - |
|
| 374 | - public function getMetadataArray() |
|
| 375 | - { |
|
| 376 | - return $this->metadata; |
|
| 377 | - } |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * @param string $key |
|
| 381 | - * @return mixed |
|
| 382 | - */ |
|
| 383 | - |
|
| 384 | - public function getMetadata($key) |
|
| 385 | - { |
|
| 386 | - return $this->metadata[$key]; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * @return string|null |
|
| 391 | - */ |
|
| 392 | - |
|
| 393 | - public function getStrategy() |
|
| 394 | - { |
|
| 395 | - if ($this->strategy instanceof StrategyInterface) { |
|
| 396 | - return get_class($this->strategy); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - return $this->strategy; |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * @inheritdoc |
|
| 404 | - */ |
|
| 405 | - |
|
| 406 | - public function getMatcher() |
|
| 407 | - { |
|
| 408 | - return $this->matcher; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * @param string $method |
|
| 413 | - * @return Route |
|
| 414 | - */ |
|
| 415 | - |
|
| 416 | - public function setMethod($method) |
|
| 417 | - { |
|
| 418 | - $this->forget(); |
|
| 419 | - $this->method = $method; |
|
| 420 | - return $this->reset(); |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * @param string $pattern |
|
| 425 | - * @return Route |
|
| 426 | - */ |
|
| 427 | - |
|
| 428 | - public function setPattern($pattern) |
|
| 429 | - { |
|
| 430 | - $this->forget(); |
|
| 431 | - $this->pattern = $pattern; |
|
| 432 | - return $this->reset(); |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - /** |
|
| 436 | - * @param string $pattern |
|
| 437 | - * @return self |
|
| 438 | - */ |
|
| 439 | - |
|
| 440 | - public function setPatternWithoutReset($pattern) |
|
| 441 | - { |
|
| 442 | - $this->pattern = $pattern; |
|
| 443 | - return $this; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * @param string $action |
|
| 448 | - * @return self |
|
| 449 | - */ |
|
| 450 | - |
|
| 451 | - public function setAction($action) |
|
| 452 | - { |
|
| 453 | - $this->action = $action; |
|
| 454 | - return $this; |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * @param string $namespace |
|
| 459 | - * @return self |
|
| 460 | - */ |
|
| 461 | - |
|
| 462 | - public function setNamespace($namespace) |
|
| 463 | - { |
|
| 464 | - $this->namespace = $namespace; |
|
| 465 | - return $this; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - /** |
|
| 469 | - * @param string[] $params |
|
| 470 | - * @return self |
|
| 471 | - */ |
|
| 472 | - |
|
| 473 | - public function setParams(array $params) |
|
| 474 | - { |
|
| 475 | - $this->params = $params; |
|
| 476 | - return $this; |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * @param string $key |
|
| 481 | - * @param string $value |
|
| 482 | - * |
|
| 483 | - * @return self |
|
| 484 | - */ |
|
| 485 | - |
|
| 486 | - public function setParam($key, $value) |
|
| 487 | - { |
|
| 488 | - $this->params[$key] = $value; |
|
| 489 | - return $this; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * @param mixed[] $defaults |
|
| 494 | - * @return self |
|
| 495 | - */ |
|
| 496 | - |
|
| 497 | - public function setDefaults(array $defaults) |
|
| 498 | - { |
|
| 499 | - $this->defaults = $defaults; |
|
| 500 | - return $this; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - /** |
|
| 504 | - * @param string $key |
|
| 505 | - * @param mixed $value |
|
| 506 | - * |
|
| 507 | - * @return self |
|
| 508 | - */ |
|
| 509 | - |
|
| 510 | - public function setDefault($key, $value) |
|
| 511 | - { |
|
| 512 | - $this->defaults[$key] = $value; |
|
| 513 | - return $this; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * @param mixed[] $metadata |
|
| 518 | - * @return self |
|
| 519 | - */ |
|
| 520 | - |
|
| 521 | - public function setMetadataArray(array $metadata) |
|
| 522 | - { |
|
| 523 | - $this->metadata = $metadata; |
|
| 524 | - return $this; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * @param string $key |
|
| 529 | - * @param mixed $value |
|
| 530 | - * |
|
| 531 | - * @return $this |
|
| 532 | - */ |
|
| 533 | - |
|
| 534 | - public function setMetadata($key, $value) |
|
| 535 | - { |
|
| 536 | - $this->metadata[$key] = $value; |
|
| 537 | - return $this; |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * @param string|StrategyInterface $strategy |
|
| 542 | - * @return self |
|
| 543 | - */ |
|
| 544 | - |
|
| 545 | - public function setStrategy($strategy) |
|
| 546 | - { |
|
| 547 | - $this->strategy = $strategy; |
|
| 548 | - return $this; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * @inheritdoc |
|
| 553 | - */ |
|
| 554 | - |
|
| 555 | - public function setMatcher(Matcher $matcher) |
|
| 556 | - { |
|
| 557 | - $this->matcher = $matcher; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * Set a function to create controllers. |
|
| 562 | - * |
|
| 563 | - * @param string|array|\Closure $callable |
|
| 564 | - * @throws BadRouteException |
|
| 565 | - * @return self |
|
| 566 | - */ |
|
| 567 | - |
|
| 568 | - public function setControllerCreationFunction($callable) |
|
| 569 | - { |
|
| 570 | - if (!is_callable($callable)) { |
|
| 571 | - throw new BadRouteException(BadRouteException::WRONG_CONTROLLER_CREATION_FUNC); |
|
| 572 | - } |
|
| 573 | - |
|
| 574 | - $this->controllerCreationFunction = $this->parseCallable($callable); |
|
| 575 | - return $this; |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * @param string $key |
|
| 580 | - * @return bool |
|
| 581 | - */ |
|
| 582 | - |
|
| 583 | - public function hasParam($key) |
|
| 584 | - { |
|
| 585 | - return isset($this->params[$key]); |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - /** |
|
| 589 | - * @param string $key |
|
| 590 | - * @return bool |
|
| 591 | - */ |
|
| 592 | - |
|
| 593 | - public function hasDefault($key) |
|
| 594 | - { |
|
| 595 | - return isset($this->defaults[$key]); |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * @param string $key |
|
| 600 | - * @return bool |
|
| 601 | - */ |
|
| 602 | - |
|
| 603 | - public function hasMetadata($key) |
|
| 604 | - { |
|
| 605 | - return isset($this->metadata[$key]); |
|
| 606 | - } |
|
| 50 | + /** |
|
| 51 | + * @var string |
|
| 52 | + */ |
|
| 53 | + |
|
| 54 | + protected $namespace = ""; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var string[] |
|
| 58 | + */ |
|
| 59 | + |
|
| 60 | + protected $params = []; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Defaults are parameters set by the user, and don't |
|
| 64 | + * appear on the pattern. |
|
| 65 | + * |
|
| 66 | + * @var array |
|
| 67 | + */ |
|
| 68 | + |
|
| 69 | + protected $defaults = []; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Metadata can be set to be used on filters, dispatch strategies |
|
| 73 | + * or anywhere the route object is used. |
|
| 74 | + * |
|
| 75 | + * @var array |
|
| 76 | + */ |
|
| 77 | + |
|
| 78 | + protected $metadata = []; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @var null|string|StrategyInterface |
|
| 82 | + */ |
|
| 83 | + |
|
| 84 | + protected $strategy; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Blocked routes are dynamic routes selected to pass by the matcher. |
|
| 88 | + * |
|
| 89 | + * @var boolean |
|
| 90 | + */ |
|
| 91 | + |
|
| 92 | + protected $blocked = false; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * The matcher that dispatched this route. |
|
| 96 | + * |
|
| 97 | + * @var Matcher $matcher |
|
| 98 | + */ |
|
| 99 | + |
|
| 100 | + protected $matcher; |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * The function used to create controllers from name. |
|
| 104 | + * |
|
| 105 | + * @var null|string|array|\Closure |
|
| 106 | + */ |
|
| 107 | + |
|
| 108 | + protected $controllerCreationFunction; |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * @param Collector $collector |
|
| 112 | + * @param string $method |
|
| 113 | + * @param string $pattern |
|
| 114 | + * @param string|array|\Closure $action |
|
| 115 | + */ |
|
| 116 | + |
|
| 117 | + public function __construct(Collector $collector, $method, $pattern, $action) |
|
| 118 | + { |
|
| 119 | + $this->collector = $collector; |
|
| 120 | + $this->method = $method; |
|
| 121 | + $this->pattern = $pattern; |
|
| 122 | + $this->action = $action; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Clone this route and set it into the collector. |
|
| 127 | + * |
|
| 128 | + * @return Route |
|
| 129 | + */ |
|
| 130 | + |
|
| 131 | + public function reset() |
|
| 132 | + { |
|
| 133 | + return $this->collector->set($this->method, $this->pattern, $this->action)->nth(0) |
|
| 134 | + ->setStrategy($this->strategy)->setParams($this->params) |
|
| 135 | + ->setDefaults($this->defaults)->setMetadataArray($this->metadata); |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Remove this route from the collector. |
|
| 140 | + * |
|
| 141 | + * @return self |
|
| 142 | + */ |
|
| 143 | + |
|
| 144 | + public function forget() |
|
| 145 | + { |
|
| 146 | + $this->collector->forget($this->method, $this->pattern); |
|
| 147 | + return $this; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Blocking a route indicate that that route have been selected and |
|
| 152 | + * parsed, now it will be given to the matcher. |
|
| 153 | + * |
|
| 154 | + * @return self |
|
| 155 | + */ |
|
| 156 | + |
|
| 157 | + public function block() |
|
| 158 | + { |
|
| 159 | + $this->blocked = true; |
|
| 160 | + return $this; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Verify if a Route have already been blocked. |
|
| 165 | + * |
|
| 166 | + * @return boolean |
|
| 167 | + */ |
|
| 168 | + |
|
| 169 | + public function blocked() |
|
| 170 | + { |
|
| 171 | + return $this->blocked; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Execute the route action, if no strategy was provided the action |
|
| 176 | + * will be executed by the call_user_func PHP function. |
|
| 177 | + * |
|
| 178 | + * @throws BadRouteException |
|
| 179 | + * @return mixed |
|
| 180 | + */ |
|
| 181 | + |
|
| 182 | + public function call() |
|
| 183 | + { |
|
| 184 | + $this->action = $this->parseCallable($this->action); |
|
| 185 | + |
|
| 186 | + if ($this->strategy === null) { |
|
| 187 | + return call_user_func_array($this->action, array_merge($this->defaults, $this->params)); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + if (!is_object($this->strategy)) { |
|
| 191 | + $this->strategy = new $this->strategy; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + return $this->callWithStrategy(); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * Seek for dynamic content on callables. eg. routes action controller#action |
|
| 199 | + * syntax allow to use the variables to build the string like: {controller}@{action} |
|
| 200 | + * |
|
| 201 | + * @param string|array|\Closure $callable |
|
| 202 | + * @return string|array|\Closure |
|
| 203 | + */ |
|
| 204 | + |
|
| 205 | + private function parseCallable($callable) |
|
| 206 | + { |
|
| 207 | + if (is_string($callable) && strpos($callable, "@")) { |
|
| 208 | + $callable = explode("@", $callable); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + if (is_array($callable)) { |
|
| 212 | + if (is_string($callable[0])) { |
|
| 213 | + $callable[0] = $this->parseCallableController($callable[0]); |
|
| 214 | + $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
| 215 | + } else $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + return $callable; |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Get the controller object. |
|
| 223 | + * |
|
| 224 | + * @param string $controller |
|
| 225 | + * @return Object |
|
| 226 | + */ |
|
| 227 | + |
|
| 228 | + private function parseCallableController($controller) |
|
| 229 | + { |
|
| 230 | + $controller = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($controller); |
|
| 231 | + |
|
| 232 | + if ($this->controllerCreationFunction === null) { |
|
| 233 | + return new $controller; |
|
| 234 | + } else return call_user_func($this->controllerCreationFunction, $controller); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Parse and replace dynamic content on route action. |
|
| 239 | + * |
|
| 240 | + * @param string $fragment Part of callable |
|
| 241 | + * @return string |
|
| 242 | + */ |
|
| 243 | + |
|
| 244 | + private function parseCallablePlaceholders($fragment) |
|
| 245 | + { |
|
| 246 | + if (strpos($fragment, "{") !== false) { |
|
| 247 | + foreach ($this->params as $placeholder => $value) { |
|
| 248 | + if (strpos($fragment, "{" . $placeholder . "}") !== false) { |
|
| 249 | + $fragment = str_replace("{" . $placeholder . "}", ucwords(str_replace("-", " ", $value)), $fragment); |
|
| 250 | + } |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return $fragment; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * Execute the route action with the given strategy. |
|
| 259 | + * |
|
| 260 | + * @throws BadRouteException |
|
| 261 | + * @return mixed |
|
| 262 | + */ |
|
| 263 | + |
|
| 264 | + private function callWithStrategy() |
|
| 265 | + { |
|
| 266 | + if ($this->strategy instanceof StrategyInterface) { |
|
| 267 | + if ($this->strategy instanceof MatcherAwareInterface) { |
|
| 268 | + $this->strategy->setMatcher($this->matcher); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + return $this->strategy->call($this); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + throw new BadRouteException("`$this->strategy` is not a valid route dispatch strategy, ". |
|
| 275 | + "it must implement the `Codeburner\Router\Strategies\StrategyInterface` interface."); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * @return Collector |
|
| 280 | + */ |
|
| 281 | + |
|
| 282 | + public function getCollector() |
|
| 283 | + { |
|
| 284 | + return $this->collector; |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + /** |
|
| 288 | + * @return string |
|
| 289 | + */ |
|
| 290 | + |
|
| 291 | + public function getMethod() |
|
| 292 | + { |
|
| 293 | + return $this->method; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * @return string |
|
| 298 | + */ |
|
| 299 | + |
|
| 300 | + public function getPattern() |
|
| 301 | + { |
|
| 302 | + return $this->pattern; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * @return string[] |
|
| 307 | + */ |
|
| 308 | + |
|
| 309 | + public function getSegments() |
|
| 310 | + { |
|
| 311 | + return explode("/", $this->pattern); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * @return string|array|\Closure |
|
| 316 | + */ |
|
| 317 | + |
|
| 318 | + public function getAction() |
|
| 319 | + { |
|
| 320 | + return $this->action; |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * @return string |
|
| 325 | + */ |
|
| 326 | + |
|
| 327 | + public function getNamespace() |
|
| 328 | + { |
|
| 329 | + return $this->namespace; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * @return string[] |
|
| 334 | + */ |
|
| 335 | + |
|
| 336 | + public function getParams() |
|
| 337 | + { |
|
| 338 | + return $this->params; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * @param string $key |
|
| 343 | + * @return string |
|
| 344 | + */ |
|
| 345 | + |
|
| 346 | + public function getParam($key) |
|
| 347 | + { |
|
| 348 | + return $this->params[$key]; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * @return array |
|
| 353 | + */ |
|
| 354 | + |
|
| 355 | + public function getDefaults() |
|
| 356 | + { |
|
| 357 | + return $this->defaults; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * @param string $key |
|
| 362 | + * @return mixed |
|
| 363 | + */ |
|
| 364 | + |
|
| 365 | + public function getDefault($key) |
|
| 366 | + { |
|
| 367 | + return $this->defaults[$key]; |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * @return array |
|
| 372 | + */ |
|
| 373 | + |
|
| 374 | + public function getMetadataArray() |
|
| 375 | + { |
|
| 376 | + return $this->metadata; |
|
| 377 | + } |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * @param string $key |
|
| 381 | + * @return mixed |
|
| 382 | + */ |
|
| 383 | + |
|
| 384 | + public function getMetadata($key) |
|
| 385 | + { |
|
| 386 | + return $this->metadata[$key]; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * @return string|null |
|
| 391 | + */ |
|
| 392 | + |
|
| 393 | + public function getStrategy() |
|
| 394 | + { |
|
| 395 | + if ($this->strategy instanceof StrategyInterface) { |
|
| 396 | + return get_class($this->strategy); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + return $this->strategy; |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * @inheritdoc |
|
| 404 | + */ |
|
| 405 | + |
|
| 406 | + public function getMatcher() |
|
| 407 | + { |
|
| 408 | + return $this->matcher; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * @param string $method |
|
| 413 | + * @return Route |
|
| 414 | + */ |
|
| 415 | + |
|
| 416 | + public function setMethod($method) |
|
| 417 | + { |
|
| 418 | + $this->forget(); |
|
| 419 | + $this->method = $method; |
|
| 420 | + return $this->reset(); |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * @param string $pattern |
|
| 425 | + * @return Route |
|
| 426 | + */ |
|
| 427 | + |
|
| 428 | + public function setPattern($pattern) |
|
| 429 | + { |
|
| 430 | + $this->forget(); |
|
| 431 | + $this->pattern = $pattern; |
|
| 432 | + return $this->reset(); |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + /** |
|
| 436 | + * @param string $pattern |
|
| 437 | + * @return self |
|
| 438 | + */ |
|
| 439 | + |
|
| 440 | + public function setPatternWithoutReset($pattern) |
|
| 441 | + { |
|
| 442 | + $this->pattern = $pattern; |
|
| 443 | + return $this; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * @param string $action |
|
| 448 | + * @return self |
|
| 449 | + */ |
|
| 450 | + |
|
| 451 | + public function setAction($action) |
|
| 452 | + { |
|
| 453 | + $this->action = $action; |
|
| 454 | + return $this; |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * @param string $namespace |
|
| 459 | + * @return self |
|
| 460 | + */ |
|
| 461 | + |
|
| 462 | + public function setNamespace($namespace) |
|
| 463 | + { |
|
| 464 | + $this->namespace = $namespace; |
|
| 465 | + return $this; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + /** |
|
| 469 | + * @param string[] $params |
|
| 470 | + * @return self |
|
| 471 | + */ |
|
| 472 | + |
|
| 473 | + public function setParams(array $params) |
|
| 474 | + { |
|
| 475 | + $this->params = $params; |
|
| 476 | + return $this; |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * @param string $key |
|
| 481 | + * @param string $value |
|
| 482 | + * |
|
| 483 | + * @return self |
|
| 484 | + */ |
|
| 485 | + |
|
| 486 | + public function setParam($key, $value) |
|
| 487 | + { |
|
| 488 | + $this->params[$key] = $value; |
|
| 489 | + return $this; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + /** |
|
| 493 | + * @param mixed[] $defaults |
|
| 494 | + * @return self |
|
| 495 | + */ |
|
| 496 | + |
|
| 497 | + public function setDefaults(array $defaults) |
|
| 498 | + { |
|
| 499 | + $this->defaults = $defaults; |
|
| 500 | + return $this; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + /** |
|
| 504 | + * @param string $key |
|
| 505 | + * @param mixed $value |
|
| 506 | + * |
|
| 507 | + * @return self |
|
| 508 | + */ |
|
| 509 | + |
|
| 510 | + public function setDefault($key, $value) |
|
| 511 | + { |
|
| 512 | + $this->defaults[$key] = $value; |
|
| 513 | + return $this; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * @param mixed[] $metadata |
|
| 518 | + * @return self |
|
| 519 | + */ |
|
| 520 | + |
|
| 521 | + public function setMetadataArray(array $metadata) |
|
| 522 | + { |
|
| 523 | + $this->metadata = $metadata; |
|
| 524 | + return $this; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * @param string $key |
|
| 529 | + * @param mixed $value |
|
| 530 | + * |
|
| 531 | + * @return $this |
|
| 532 | + */ |
|
| 533 | + |
|
| 534 | + public function setMetadata($key, $value) |
|
| 535 | + { |
|
| 536 | + $this->metadata[$key] = $value; |
|
| 537 | + return $this; |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * @param string|StrategyInterface $strategy |
|
| 542 | + * @return self |
|
| 543 | + */ |
|
| 544 | + |
|
| 545 | + public function setStrategy($strategy) |
|
| 546 | + { |
|
| 547 | + $this->strategy = $strategy; |
|
| 548 | + return $this; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * @inheritdoc |
|
| 553 | + */ |
|
| 554 | + |
|
| 555 | + public function setMatcher(Matcher $matcher) |
|
| 556 | + { |
|
| 557 | + $this->matcher = $matcher; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * Set a function to create controllers. |
|
| 562 | + * |
|
| 563 | + * @param string|array|\Closure $callable |
|
| 564 | + * @throws BadRouteException |
|
| 565 | + * @return self |
|
| 566 | + */ |
|
| 567 | + |
|
| 568 | + public function setControllerCreationFunction($callable) |
|
| 569 | + { |
|
| 570 | + if (!is_callable($callable)) { |
|
| 571 | + throw new BadRouteException(BadRouteException::WRONG_CONTROLLER_CREATION_FUNC); |
|
| 572 | + } |
|
| 573 | + |
|
| 574 | + $this->controllerCreationFunction = $this->parseCallable($callable); |
|
| 575 | + return $this; |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * @param string $key |
|
| 580 | + * @return bool |
|
| 581 | + */ |
|
| 582 | + |
|
| 583 | + public function hasParam($key) |
|
| 584 | + { |
|
| 585 | + return isset($this->params[$key]); |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + /** |
|
| 589 | + * @param string $key |
|
| 590 | + * @return bool |
|
| 591 | + */ |
|
| 592 | + |
|
| 593 | + public function hasDefault($key) |
|
| 594 | + { |
|
| 595 | + return isset($this->defaults[$key]); |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * @param string $key |
|
| 600 | + * @return bool |
|
| 601 | + */ |
|
| 602 | + |
|
| 603 | + public function hasMetadata($key) |
|
| 604 | + { |
|
| 605 | + return isset($this->metadata[$key]); |
|
| 606 | + } |
|
| 607 | 607 | |
| 608 | 608 | } |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | |
| 228 | 228 | private function parseCallableController($controller) |
| 229 | 229 | { |
| 230 | - $controller = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($controller); |
|
| 230 | + $controller = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($controller); |
|
| 231 | 231 | |
| 232 | 232 | if ($this->controllerCreationFunction === null) { |
| 233 | 233 | return new $controller; |
@@ -271,7 +271,7 @@ discard block |
||
| 271 | 271 | return $this->strategy->call($this); |
| 272 | 272 | } |
| 273 | 273 | |
| 274 | - throw new BadRouteException("`$this->strategy` is not a valid route dispatch strategy, ". |
|
| 274 | + throw new BadRouteException("`$this->strategy` is not a valid route dispatch strategy, " . |
|
| 275 | 275 | "it must implement the `Codeburner\Router\Strategies\StrategyInterface` interface."); |
| 276 | 276 | } |
| 277 | 277 | |
@@ -212,7 +212,9 @@ discard block |
||
| 212 | 212 | if (is_string($callable[0])) { |
| 213 | 213 | $callable[0] = $this->parseCallableController($callable[0]); |
| 214 | 214 | $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
| 215 | - } else $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
| 215 | + } else { |
|
| 216 | + $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
| 217 | + } |
|
| 216 | 218 | } |
| 217 | 219 | |
| 218 | 220 | return $callable; |
@@ -231,7 +233,9 @@ discard block |
||
| 231 | 233 | |
| 232 | 234 | if ($this->controllerCreationFunction === null) { |
| 233 | 235 | return new $controller; |
| 234 | - } else return call_user_func($this->controllerCreationFunction, $controller); |
|
| 236 | + } else { |
|
| 237 | + return call_user_func($this->controllerCreationFunction, $controller); |
|
| 238 | + } |
|
| 235 | 239 | } |
| 236 | 240 | |
| 237 | 241 | /** |
@@ -20,9 +20,9 @@ |
||
| 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."; |
|
| 26 | - const WRONG_CONTROLLER_CREATION_FUNC = "The controller creation function passed is not callable."; |
|
| 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 | + const WRONG_CONTROLLER_CREATION_FUNC = "The controller creation function passed is not callable."; |
|
| 27 | 27 | |
| 28 | 28 | } |