@@ -20,163 +20,163 @@ |
||
| 20 | 20 | class Resource extends Group |
| 21 | 21 | { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * @inheritdoc |
|
| 25 | - * @throws \BadMethodCallException |
|
| 26 | - */ |
|
| 27 | - |
|
| 28 | - public function setMethod($method) |
|
| 29 | - { |
|
| 30 | - throw new \BadMethodCallException("Resources can't chance they http method."); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Remove the routes without the passed methods. |
|
| 35 | - * |
|
| 36 | - * @param string|array $methods |
|
| 37 | - * @return self |
|
| 38 | - */ |
|
| 39 | - |
|
| 40 | - public function only($methods) |
|
| 41 | - { |
|
| 42 | - $this->filterByMethod((array) $methods, false); |
|
| 43 | - return $this; |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * Remove the routes with the passed methods. |
|
| 48 | - * |
|
| 49 | - * @param string|array $methods |
|
| 50 | - * @return self |
|
| 51 | - */ |
|
| 52 | - |
|
| 53 | - public function except($methods) |
|
| 54 | - { |
|
| 55 | - $this->filterByMethod((array) $methods, true); |
|
| 56 | - return $this; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Forget the grouped routes filtering by http methods. |
|
| 61 | - * |
|
| 62 | - * @param array $methods |
|
| 63 | - * @param bool $alt Should remove? |
|
| 64 | - */ |
|
| 65 | - |
|
| 66 | - private function filterByMethod(array $methods, $alt) |
|
| 67 | - { |
|
| 68 | - $methods = array_flip(array_map('strtolower', $methods)); |
|
| 69 | - |
|
| 70 | - foreach ($this->routes as $route) { |
|
| 71 | - if (isset($methods[$route->getAction()[1]]) === $alt) { |
|
| 72 | - $route->forget(); |
|
| 73 | - } |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Translate the "make" or "edit" from resources path. |
|
| 79 | - * |
|
| 80 | - * @param string[] $translations |
|
| 81 | - * @return self |
|
| 82 | - */ |
|
| 83 | - |
|
| 84 | - public function translate(array $translations) |
|
| 85 | - { |
|
| 86 | - /** @var Route $route */ |
|
| 87 | - foreach ($this->route as $route) { |
|
| 88 | - $action = $route->getAction()[1]; |
|
| 89 | - |
|
| 90 | - if ($action === "make" && isset($translations["make"])) { |
|
| 91 | - $route->setPatternWithoutReset(str_replace("make", $translations["make"], $route->getPattern())); |
|
| 92 | - } else { |
|
| 93 | - if ($action === "edit" && isset($translations["edit"])) { |
|
| 94 | - $route->setPatternWithoutReset(str_replace("edit", $translations["edit"], $route->getPattern())); |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - return $this; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Add a route or a group of routes to the resource, it means that |
|
| 104 | - * every added route will now receive the parameters of the resource, like id. |
|
| 105 | - * |
|
| 106 | - * @param Route|Group $route |
|
| 107 | - * @return self |
|
| 108 | - */ |
|
| 109 | - |
|
| 110 | - public function member($route) |
|
| 111 | - { |
|
| 112 | - $resource = new Resource; |
|
| 113 | - $resource->set($route); |
|
| 114 | - $this->nest($resource); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Nested routes capture the relation between a resource and another resource. |
|
| 119 | - * |
|
| 120 | - * @param Resource $resource |
|
| 121 | - * @return self |
|
| 122 | - */ |
|
| 123 | - |
|
| 124 | - public function nest(Resource $resource) |
|
| 125 | - { |
|
| 126 | - /** @var self $resource */ |
|
| 127 | - foreach ($this->routes as $route) { |
|
| 128 | - if ($route->getAction()[1] === "show") { |
|
| 129 | - $this->set($resource->forget()->setPrefix($this->getNestedPrefix($route->getPattern()))); break; |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - return $this; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Nest resources but with only build routes with the minimal amount of information |
|
| 138 | - * to uniquely identify the resource. |
|
| 139 | - * |
|
| 140 | - * @param Resource $resource |
|
| 141 | - * @return self |
|
| 142 | - */ |
|
| 143 | - |
|
| 144 | - public function shallow(Resource $resource) |
|
| 145 | - { |
|
| 146 | - /** @var self $resource */ |
|
| 147 | - $newResource = new Resource; |
|
| 148 | - $resource->forget(); |
|
| 149 | - $routes = $resource->all(); |
|
| 150 | - |
|
| 151 | - foreach ($routes as $route) { |
|
| 152 | - if (strpos("index make create", $route->getAction()[1]) !== false) { |
|
| 153 | - $newResource->set($route); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return $this->nest($newResource); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Resolve the nesting pattern, setting the prefixes based on |
|
| 162 | - * parent resources patterns. |
|
| 163 | - * |
|
| 164 | - * @param string $pattern |
|
| 165 | - * @return string |
|
| 166 | - */ |
|
| 167 | - |
|
| 168 | - protected function getNestedPrefix($pattern) |
|
| 169 | - { |
|
| 170 | - $segments = explode("/", $pattern); |
|
| 171 | - $pattern = ""; |
|
| 172 | - |
|
| 173 | - foreach ($segments as $index => $segment) { |
|
| 174 | - if (strpos($segment, "{") === 0) { |
|
| 175 | - $pattern .= "/{" . $segments[$index - 1] . "_" . ltrim($segment, "{"); |
|
| 176 | - } else $pattern .= $segment; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - return $pattern; |
|
| 180 | - } |
|
| 23 | + /** |
|
| 24 | + * @inheritdoc |
|
| 25 | + * @throws \BadMethodCallException |
|
| 26 | + */ |
|
| 27 | + |
|
| 28 | + public function setMethod($method) |
|
| 29 | + { |
|
| 30 | + throw new \BadMethodCallException("Resources can't chance they http method."); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Remove the routes without the passed methods. |
|
| 35 | + * |
|
| 36 | + * @param string|array $methods |
|
| 37 | + * @return self |
|
| 38 | + */ |
|
| 39 | + |
|
| 40 | + public function only($methods) |
|
| 41 | + { |
|
| 42 | + $this->filterByMethod((array) $methods, false); |
|
| 43 | + return $this; |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * Remove the routes with the passed methods. |
|
| 48 | + * |
|
| 49 | + * @param string|array $methods |
|
| 50 | + * @return self |
|
| 51 | + */ |
|
| 52 | + |
|
| 53 | + public function except($methods) |
|
| 54 | + { |
|
| 55 | + $this->filterByMethod((array) $methods, true); |
|
| 56 | + return $this; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Forget the grouped routes filtering by http methods. |
|
| 61 | + * |
|
| 62 | + * @param array $methods |
|
| 63 | + * @param bool $alt Should remove? |
|
| 64 | + */ |
|
| 65 | + |
|
| 66 | + private function filterByMethod(array $methods, $alt) |
|
| 67 | + { |
|
| 68 | + $methods = array_flip(array_map('strtolower', $methods)); |
|
| 69 | + |
|
| 70 | + foreach ($this->routes as $route) { |
|
| 71 | + if (isset($methods[$route->getAction()[1]]) === $alt) { |
|
| 72 | + $route->forget(); |
|
| 73 | + } |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Translate the "make" or "edit" from resources path. |
|
| 79 | + * |
|
| 80 | + * @param string[] $translations |
|
| 81 | + * @return self |
|
| 82 | + */ |
|
| 83 | + |
|
| 84 | + public function translate(array $translations) |
|
| 85 | + { |
|
| 86 | + /** @var Route $route */ |
|
| 87 | + foreach ($this->route as $route) { |
|
| 88 | + $action = $route->getAction()[1]; |
|
| 89 | + |
|
| 90 | + if ($action === "make" && isset($translations["make"])) { |
|
| 91 | + $route->setPatternWithoutReset(str_replace("make", $translations["make"], $route->getPattern())); |
|
| 92 | + } else { |
|
| 93 | + if ($action === "edit" && isset($translations["edit"])) { |
|
| 94 | + $route->setPatternWithoutReset(str_replace("edit", $translations["edit"], $route->getPattern())); |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + return $this; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Add a route or a group of routes to the resource, it means that |
|
| 104 | + * every added route will now receive the parameters of the resource, like id. |
|
| 105 | + * |
|
| 106 | + * @param Route|Group $route |
|
| 107 | + * @return self |
|
| 108 | + */ |
|
| 109 | + |
|
| 110 | + public function member($route) |
|
| 111 | + { |
|
| 112 | + $resource = new Resource; |
|
| 113 | + $resource->set($route); |
|
| 114 | + $this->nest($resource); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Nested routes capture the relation between a resource and another resource. |
|
| 119 | + * |
|
| 120 | + * @param Resource $resource |
|
| 121 | + * @return self |
|
| 122 | + */ |
|
| 123 | + |
|
| 124 | + public function nest(Resource $resource) |
|
| 125 | + { |
|
| 126 | + /** @var self $resource */ |
|
| 127 | + foreach ($this->routes as $route) { |
|
| 128 | + if ($route->getAction()[1] === "show") { |
|
| 129 | + $this->set($resource->forget()->setPrefix($this->getNestedPrefix($route->getPattern()))); break; |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + return $this; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Nest resources but with only build routes with the minimal amount of information |
|
| 138 | + * to uniquely identify the resource. |
|
| 139 | + * |
|
| 140 | + * @param Resource $resource |
|
| 141 | + * @return self |
|
| 142 | + */ |
|
| 143 | + |
|
| 144 | + public function shallow(Resource $resource) |
|
| 145 | + { |
|
| 146 | + /** @var self $resource */ |
|
| 147 | + $newResource = new Resource; |
|
| 148 | + $resource->forget(); |
|
| 149 | + $routes = $resource->all(); |
|
| 150 | + |
|
| 151 | + foreach ($routes as $route) { |
|
| 152 | + if (strpos("index make create", $route->getAction()[1]) !== false) { |
|
| 153 | + $newResource->set($route); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return $this->nest($newResource); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Resolve the nesting pattern, setting the prefixes based on |
|
| 162 | + * parent resources patterns. |
|
| 163 | + * |
|
| 164 | + * @param string $pattern |
|
| 165 | + * @return string |
|
| 166 | + */ |
|
| 167 | + |
|
| 168 | + protected function getNestedPrefix($pattern) |
|
| 169 | + { |
|
| 170 | + $segments = explode("/", $pattern); |
|
| 171 | + $pattern = ""; |
|
| 172 | + |
|
| 173 | + foreach ($segments as $index => $segment) { |
|
| 174 | + if (strpos($segment, "{") === 0) { |
|
| 175 | + $pattern .= "/{" . $segments[$index - 1] . "_" . ltrim($segment, "{"); |
|
| 176 | + } else $pattern .= $segment; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + return $pattern; |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | 182 | } |
@@ -173,7 +173,9 @@ |
||
| 173 | 173 | foreach ($segments as $index => $segment) { |
| 174 | 174 | if (strpos($segment, "{") === 0) { |
| 175 | 175 | $pattern .= "/{" . $segments[$index - 1] . "_" . ltrim($segment, "{"); |
| 176 | - } else $pattern .= $segment; |
|
| 176 | + } else { |
|
| 177 | + $pattern .= $segment; |
|
| 178 | + } |
|
| 177 | 179 | } |
| 178 | 180 | |
| 179 | 181 | return $pattern; |
@@ -23,538 +23,538 @@ |
||
| 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 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; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * The matcher that dispatched this route. |
|
| 96 | - * |
|
| 97 | - * @var Matcher $matcher |
|
| 98 | - */ |
|
| 99 | - |
|
| 100 | - protected $matcher; |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param Collector $collector |
|
| 104 | - * @param string $method |
|
| 105 | - * @param string $pattern |
|
| 106 | - * @param string|array|\Closure $action |
|
| 107 | - */ |
|
| 108 | - |
|
| 109 | - public function __construct(Collector $collector, $method, $pattern, $action) |
|
| 110 | - { |
|
| 111 | - $this->collector = $collector; |
|
| 112 | - $this->method = $method; |
|
| 113 | - $this->pattern = $pattern; |
|
| 114 | - $this->action = $action; |
|
| 115 | - $this->namespace = ""; |
|
| 116 | - $this->params = []; |
|
| 117 | - $this->defaults = []; |
|
| 118 | - $this->metadata = []; |
|
| 119 | - $this->strategy = null; |
|
| 120 | - $this->blocked = false; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Clone this route and set it into the collector. |
|
| 125 | - * |
|
| 126 | - * @return Route |
|
| 127 | - */ |
|
| 128 | - |
|
| 129 | - public function reset() |
|
| 130 | - { |
|
| 131 | - return $this->collector->set($this->method, $this->pattern, $this->action)->nth(0) |
|
| 132 | - ->setStrategy($this->strategy)->setParams($this->params) |
|
| 133 | - ->setDefaults($this->defaults)->setMetadataArray($this->metadata); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Remove this route from the collector. |
|
| 138 | - * |
|
| 139 | - * @return self |
|
| 140 | - */ |
|
| 141 | - |
|
| 142 | - public function forget() |
|
| 143 | - { |
|
| 144 | - $this->collector->forget($this->method, $this->pattern); |
|
| 145 | - return $this; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * Blocking a route indicate that that route have been selected and |
|
| 150 | - * parsed, now it will be given to the matcher. |
|
| 151 | - * |
|
| 152 | - * @return self |
|
| 153 | - */ |
|
| 154 | - |
|
| 155 | - public function block() |
|
| 156 | - { |
|
| 157 | - $this->blocked = true; |
|
| 158 | - return $this; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Verify if a Route have already been blocked. |
|
| 163 | - * |
|
| 164 | - * @return boolean |
|
| 165 | - */ |
|
| 166 | - |
|
| 167 | - public function blocked() |
|
| 168 | - { |
|
| 169 | - return $this->blocked; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Execute the route action, if no strategy was provided the action |
|
| 174 | - * will be executed by the call_user_func PHP function. |
|
| 175 | - * |
|
| 176 | - * @throws BadRouteException |
|
| 177 | - * @return mixed |
|
| 178 | - */ |
|
| 179 | - |
|
| 180 | - public function call() |
|
| 181 | - { |
|
| 182 | - $this->action = $this->parseCallable($this->action); |
|
| 183 | - |
|
| 184 | - if ($this->strategy === null) { |
|
| 185 | - return call_user_func_array($this->action, array_merge($this->defaults, $this->params)); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - if (!is_object($this->strategy)) { |
|
| 189 | - $strategy = new $this->strategy; |
|
| 190 | - |
|
| 191 | - if ($strategy instanceof StrategyInterface) { |
|
| 192 | - if ($strategy instanceof MatcherAwareInterface) { |
|
| 193 | - $strategy->setMatcher($this->matcher); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - return $strategy->call($this); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - throw new BadRouteException("`$this->strategy` is not a valid route dispatch strategy, ". |
|
| 201 | - "it must implement the `Codeburner\Router\Strategies\StrategyInterface` interface."); |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - /** |
|
| 205 | - * Seek for dynamic content on callables. eg. routes action controller#action |
|
| 206 | - * syntax allow to use the variables to build the string like: {controller}@{action} |
|
| 207 | - * |
|
| 208 | - * @param string|array|\Closure $callable |
|
| 209 | - * @return string|array|\Closure |
|
| 210 | - */ |
|
| 211 | - |
|
| 212 | - private function parseCallable($callable) |
|
| 213 | - { |
|
| 214 | - if (is_string($callable) && strpos($callable, "@")) { |
|
| 215 | - $callable = explode("@", $callable); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - if (is_array($callable)) { |
|
| 219 | - if (is_string($callable[0])) { |
|
| 220 | - $callable[0] = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($callable[0]); |
|
| 221 | - $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
| 222 | - } else $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - return $callable; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Parse and replace dynamic content on route action. |
|
| 230 | - * |
|
| 231 | - * @param string $fragment Part of callable |
|
| 232 | - * @return string |
|
| 233 | - */ |
|
| 234 | - |
|
| 235 | - private function parseCallablePlaceholders($fragment) |
|
| 236 | - { |
|
| 237 | - if (strpos($fragment, "{") !== false) { |
|
| 238 | - foreach ($this->params as $placeholder => $value) { |
|
| 239 | - if (strpos($fragment, "{" . $placeholder . "}") !== false) { |
|
| 240 | - $fragment = str_replace("{" . $placeholder . "}", ucwords(str_replace("-", " ", $value)), $fragment); |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - return $fragment; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - /** |
|
| 249 | - * @return Collector |
|
| 250 | - */ |
|
| 251 | - |
|
| 252 | - public function getCollector() |
|
| 253 | - { |
|
| 254 | - return $this->collector; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * @return string |
|
| 259 | - */ |
|
| 260 | - |
|
| 261 | - public function getMethod() |
|
| 262 | - { |
|
| 263 | - return $this->method; |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * @return string |
|
| 268 | - */ |
|
| 269 | - |
|
| 270 | - public function getPattern() |
|
| 271 | - { |
|
| 272 | - return $this->pattern; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * @return string[] |
|
| 277 | - */ |
|
| 278 | - |
|
| 279 | - public function getSegments() |
|
| 280 | - { |
|
| 281 | - return explode("/", $this->pattern); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * @return string|array|\Closure |
|
| 286 | - */ |
|
| 287 | - |
|
| 288 | - public function getAction() |
|
| 289 | - { |
|
| 290 | - return $this->action; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @return string |
|
| 295 | - */ |
|
| 296 | - |
|
| 297 | - public function getNamespace() |
|
| 298 | - { |
|
| 299 | - return $this->namespace; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - /** |
|
| 303 | - * @return string[] |
|
| 304 | - */ |
|
| 305 | - |
|
| 306 | - public function getParams() |
|
| 307 | - { |
|
| 308 | - return $this->params; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * @param string $key |
|
| 313 | - * @return string |
|
| 314 | - */ |
|
| 315 | - |
|
| 316 | - public function getParam($key) |
|
| 317 | - { |
|
| 318 | - return $this->params[$key]; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * @return array |
|
| 323 | - */ |
|
| 324 | - |
|
| 325 | - public function getDefaults() |
|
| 326 | - { |
|
| 327 | - return $this->defaults; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * @param string $key |
|
| 332 | - * @return mixed |
|
| 333 | - */ |
|
| 334 | - |
|
| 335 | - public function getDefault($key) |
|
| 336 | - { |
|
| 337 | - return $this->defaults[$key]; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /** |
|
| 341 | - * @return array |
|
| 342 | - */ |
|
| 343 | - |
|
| 344 | - public function getMetadataArray() |
|
| 345 | - { |
|
| 346 | - return $this->metadata; |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * @param string $key |
|
| 351 | - * @return mixed |
|
| 352 | - */ |
|
| 353 | - |
|
| 354 | - public function getMetadata($key) |
|
| 355 | - { |
|
| 356 | - return $this->metadata[$key]; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * @return string|null |
|
| 361 | - */ |
|
| 362 | - |
|
| 363 | - public function getStrategy() |
|
| 364 | - { |
|
| 365 | - if ($this->strategy instanceof StrategyInterface) { |
|
| 366 | - return get_class($this->strategy); |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - return $this->strategy; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * @inheritdoc |
|
| 374 | - */ |
|
| 375 | - |
|
| 376 | - public function getMatcher() |
|
| 377 | - { |
|
| 378 | - return $this->matcher; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * @param string $method |
|
| 383 | - * @return Route |
|
| 384 | - */ |
|
| 385 | - |
|
| 386 | - public function setMethod($method) |
|
| 387 | - { |
|
| 388 | - $this->forget(); |
|
| 389 | - $this->method = $method; |
|
| 390 | - return $this->reset(); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * @param string $pattern |
|
| 395 | - * @return Route |
|
| 396 | - */ |
|
| 397 | - |
|
| 398 | - public function setPattern($pattern) |
|
| 399 | - { |
|
| 400 | - $this->forget(); |
|
| 401 | - $this->pattern = $pattern; |
|
| 402 | - return $this->reset(); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * @param string $pattern |
|
| 407 | - * @return self |
|
| 408 | - */ |
|
| 409 | - |
|
| 410 | - public function setPatternWithoutReset($pattern) |
|
| 411 | - { |
|
| 412 | - $this->pattern = $pattern; |
|
| 413 | - return $this; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * @param string $action |
|
| 418 | - * @return self |
|
| 419 | - */ |
|
| 420 | - |
|
| 421 | - public function setAction($action) |
|
| 422 | - { |
|
| 423 | - $this->action = $action; |
|
| 424 | - return $this; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * @param string $namespace |
|
| 429 | - * @return self |
|
| 430 | - */ |
|
| 431 | - |
|
| 432 | - public function setNamespace($namespace) |
|
| 433 | - { |
|
| 434 | - $this->namespace = $namespace; |
|
| 435 | - return $this; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * @param string[] $params |
|
| 440 | - * @return self |
|
| 441 | - */ |
|
| 442 | - |
|
| 443 | - public function setParams(array $params) |
|
| 444 | - { |
|
| 445 | - $this->params = $params; |
|
| 446 | - return $this; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * @param string $key |
|
| 451 | - * @param string $value |
|
| 452 | - * |
|
| 453 | - * @return self |
|
| 454 | - */ |
|
| 455 | - |
|
| 456 | - public function setParam($key, $value) |
|
| 457 | - { |
|
| 458 | - $this->params[$key] = $value; |
|
| 459 | - return $this; |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * @param mixed[] $defaults |
|
| 464 | - * @return self |
|
| 465 | - */ |
|
| 466 | - |
|
| 467 | - public function setDefaults(array $defaults) |
|
| 468 | - { |
|
| 469 | - $this->defaults = $defaults; |
|
| 470 | - return $this; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - /** |
|
| 474 | - * @param string $key |
|
| 475 | - * @param mixed $value |
|
| 476 | - * |
|
| 477 | - * @return self |
|
| 478 | - */ |
|
| 479 | - |
|
| 480 | - public function setDefault($key, $value) |
|
| 481 | - { |
|
| 482 | - $this->defaults[$key] = $value; |
|
| 483 | - return $this; |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * @param mixed[] $metadata |
|
| 488 | - * @return self |
|
| 489 | - */ |
|
| 490 | - |
|
| 491 | - public function setMetadataArray(array $metadata) |
|
| 492 | - { |
|
| 493 | - $this->metadata = $metadata; |
|
| 494 | - return $this; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * @param string $key |
|
| 499 | - * @param mixed $value |
|
| 500 | - * |
|
| 501 | - * @return $this |
|
| 502 | - */ |
|
| 503 | - |
|
| 504 | - public function setMetadata($key, $value) |
|
| 505 | - { |
|
| 506 | - $this->metadata[$key] = $value; |
|
| 507 | - return $this; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * @param string|StrategyInterface $strategy |
|
| 512 | - * @return self |
|
| 513 | - */ |
|
| 514 | - |
|
| 515 | - public function setStrategy($strategy) |
|
| 516 | - { |
|
| 517 | - $this->strategy = $strategy; |
|
| 518 | - return $this; |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - /** |
|
| 522 | - * @inheritdoc |
|
| 523 | - */ |
|
| 524 | - |
|
| 525 | - public function setMatcher(Matcher $matcher) |
|
| 526 | - { |
|
| 527 | - $this->matcher = $matcher; |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - /** |
|
| 531 | - * @param string $key |
|
| 532 | - * @return bool |
|
| 533 | - */ |
|
| 534 | - |
|
| 535 | - public function hasParam($key) |
|
| 536 | - { |
|
| 537 | - return isset($this->params[$key]); |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * @param string $key |
|
| 542 | - * @return bool |
|
| 543 | - */ |
|
| 544 | - |
|
| 545 | - public function hasDefault($key) |
|
| 546 | - { |
|
| 547 | - return isset($this->defaults[$key]); |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * @param string $key |
|
| 552 | - * @return bool |
|
| 553 | - */ |
|
| 554 | - |
|
| 555 | - public function hasMetadata($key) |
|
| 556 | - { |
|
| 557 | - return isset($this->metadata[$key]); |
|
| 558 | - } |
|
| 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 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; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * The matcher that dispatched this route. |
|
| 96 | + * |
|
| 97 | + * @var Matcher $matcher |
|
| 98 | + */ |
|
| 99 | + |
|
| 100 | + protected $matcher; |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param Collector $collector |
|
| 104 | + * @param string $method |
|
| 105 | + * @param string $pattern |
|
| 106 | + * @param string|array|\Closure $action |
|
| 107 | + */ |
|
| 108 | + |
|
| 109 | + public function __construct(Collector $collector, $method, $pattern, $action) |
|
| 110 | + { |
|
| 111 | + $this->collector = $collector; |
|
| 112 | + $this->method = $method; |
|
| 113 | + $this->pattern = $pattern; |
|
| 114 | + $this->action = $action; |
|
| 115 | + $this->namespace = ""; |
|
| 116 | + $this->params = []; |
|
| 117 | + $this->defaults = []; |
|
| 118 | + $this->metadata = []; |
|
| 119 | + $this->strategy = null; |
|
| 120 | + $this->blocked = false; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Clone this route and set it into the collector. |
|
| 125 | + * |
|
| 126 | + * @return Route |
|
| 127 | + */ |
|
| 128 | + |
|
| 129 | + public function reset() |
|
| 130 | + { |
|
| 131 | + return $this->collector->set($this->method, $this->pattern, $this->action)->nth(0) |
|
| 132 | + ->setStrategy($this->strategy)->setParams($this->params) |
|
| 133 | + ->setDefaults($this->defaults)->setMetadataArray($this->metadata); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Remove this route from the collector. |
|
| 138 | + * |
|
| 139 | + * @return self |
|
| 140 | + */ |
|
| 141 | + |
|
| 142 | + public function forget() |
|
| 143 | + { |
|
| 144 | + $this->collector->forget($this->method, $this->pattern); |
|
| 145 | + return $this; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * Blocking a route indicate that that route have been selected and |
|
| 150 | + * parsed, now it will be given to the matcher. |
|
| 151 | + * |
|
| 152 | + * @return self |
|
| 153 | + */ |
|
| 154 | + |
|
| 155 | + public function block() |
|
| 156 | + { |
|
| 157 | + $this->blocked = true; |
|
| 158 | + return $this; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Verify if a Route have already been blocked. |
|
| 163 | + * |
|
| 164 | + * @return boolean |
|
| 165 | + */ |
|
| 166 | + |
|
| 167 | + public function blocked() |
|
| 168 | + { |
|
| 169 | + return $this->blocked; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Execute the route action, if no strategy was provided the action |
|
| 174 | + * will be executed by the call_user_func PHP function. |
|
| 175 | + * |
|
| 176 | + * @throws BadRouteException |
|
| 177 | + * @return mixed |
|
| 178 | + */ |
|
| 179 | + |
|
| 180 | + public function call() |
|
| 181 | + { |
|
| 182 | + $this->action = $this->parseCallable($this->action); |
|
| 183 | + |
|
| 184 | + if ($this->strategy === null) { |
|
| 185 | + return call_user_func_array($this->action, array_merge($this->defaults, $this->params)); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + if (!is_object($this->strategy)) { |
|
| 189 | + $strategy = new $this->strategy; |
|
| 190 | + |
|
| 191 | + if ($strategy instanceof StrategyInterface) { |
|
| 192 | + if ($strategy instanceof MatcherAwareInterface) { |
|
| 193 | + $strategy->setMatcher($this->matcher); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + return $strategy->call($this); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + throw new BadRouteException("`$this->strategy` is not a valid route dispatch strategy, ". |
|
| 201 | + "it must implement the `Codeburner\Router\Strategies\StrategyInterface` interface."); |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + /** |
|
| 205 | + * Seek for dynamic content on callables. eg. routes action controller#action |
|
| 206 | + * syntax allow to use the variables to build the string like: {controller}@{action} |
|
| 207 | + * |
|
| 208 | + * @param string|array|\Closure $callable |
|
| 209 | + * @return string|array|\Closure |
|
| 210 | + */ |
|
| 211 | + |
|
| 212 | + private function parseCallable($callable) |
|
| 213 | + { |
|
| 214 | + if (is_string($callable) && strpos($callable, "@")) { |
|
| 215 | + $callable = explode("@", $callable); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + if (is_array($callable)) { |
|
| 219 | + if (is_string($callable[0])) { |
|
| 220 | + $callable[0] = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($callable[0]); |
|
| 221 | + $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
| 222 | + } else $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + return $callable; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Parse and replace dynamic content on route action. |
|
| 230 | + * |
|
| 231 | + * @param string $fragment Part of callable |
|
| 232 | + * @return string |
|
| 233 | + */ |
|
| 234 | + |
|
| 235 | + private function parseCallablePlaceholders($fragment) |
|
| 236 | + { |
|
| 237 | + if (strpos($fragment, "{") !== false) { |
|
| 238 | + foreach ($this->params as $placeholder => $value) { |
|
| 239 | + if (strpos($fragment, "{" . $placeholder . "}") !== false) { |
|
| 240 | + $fragment = str_replace("{" . $placeholder . "}", ucwords(str_replace("-", " ", $value)), $fragment); |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + return $fragment; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + /** |
|
| 249 | + * @return Collector |
|
| 250 | + */ |
|
| 251 | + |
|
| 252 | + public function getCollector() |
|
| 253 | + { |
|
| 254 | + return $this->collector; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * @return string |
|
| 259 | + */ |
|
| 260 | + |
|
| 261 | + public function getMethod() |
|
| 262 | + { |
|
| 263 | + return $this->method; |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * @return string |
|
| 268 | + */ |
|
| 269 | + |
|
| 270 | + public function getPattern() |
|
| 271 | + { |
|
| 272 | + return $this->pattern; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * @return string[] |
|
| 277 | + */ |
|
| 278 | + |
|
| 279 | + public function getSegments() |
|
| 280 | + { |
|
| 281 | + return explode("/", $this->pattern); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * @return string|array|\Closure |
|
| 286 | + */ |
|
| 287 | + |
|
| 288 | + public function getAction() |
|
| 289 | + { |
|
| 290 | + return $this->action; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @return string |
|
| 295 | + */ |
|
| 296 | + |
|
| 297 | + public function getNamespace() |
|
| 298 | + { |
|
| 299 | + return $this->namespace; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + /** |
|
| 303 | + * @return string[] |
|
| 304 | + */ |
|
| 305 | + |
|
| 306 | + public function getParams() |
|
| 307 | + { |
|
| 308 | + return $this->params; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * @param string $key |
|
| 313 | + * @return string |
|
| 314 | + */ |
|
| 315 | + |
|
| 316 | + public function getParam($key) |
|
| 317 | + { |
|
| 318 | + return $this->params[$key]; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * @return array |
|
| 323 | + */ |
|
| 324 | + |
|
| 325 | + public function getDefaults() |
|
| 326 | + { |
|
| 327 | + return $this->defaults; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * @param string $key |
|
| 332 | + * @return mixed |
|
| 333 | + */ |
|
| 334 | + |
|
| 335 | + public function getDefault($key) |
|
| 336 | + { |
|
| 337 | + return $this->defaults[$key]; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /** |
|
| 341 | + * @return array |
|
| 342 | + */ |
|
| 343 | + |
|
| 344 | + public function getMetadataArray() |
|
| 345 | + { |
|
| 346 | + return $this->metadata; |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * @param string $key |
|
| 351 | + * @return mixed |
|
| 352 | + */ |
|
| 353 | + |
|
| 354 | + public function getMetadata($key) |
|
| 355 | + { |
|
| 356 | + return $this->metadata[$key]; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * @return string|null |
|
| 361 | + */ |
|
| 362 | + |
|
| 363 | + public function getStrategy() |
|
| 364 | + { |
|
| 365 | + if ($this->strategy instanceof StrategyInterface) { |
|
| 366 | + return get_class($this->strategy); |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + return $this->strategy; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * @inheritdoc |
|
| 374 | + */ |
|
| 375 | + |
|
| 376 | + public function getMatcher() |
|
| 377 | + { |
|
| 378 | + return $this->matcher; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * @param string $method |
|
| 383 | + * @return Route |
|
| 384 | + */ |
|
| 385 | + |
|
| 386 | + public function setMethod($method) |
|
| 387 | + { |
|
| 388 | + $this->forget(); |
|
| 389 | + $this->method = $method; |
|
| 390 | + return $this->reset(); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * @param string $pattern |
|
| 395 | + * @return Route |
|
| 396 | + */ |
|
| 397 | + |
|
| 398 | + public function setPattern($pattern) |
|
| 399 | + { |
|
| 400 | + $this->forget(); |
|
| 401 | + $this->pattern = $pattern; |
|
| 402 | + return $this->reset(); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * @param string $pattern |
|
| 407 | + * @return self |
|
| 408 | + */ |
|
| 409 | + |
|
| 410 | + public function setPatternWithoutReset($pattern) |
|
| 411 | + { |
|
| 412 | + $this->pattern = $pattern; |
|
| 413 | + return $this; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * @param string $action |
|
| 418 | + * @return self |
|
| 419 | + */ |
|
| 420 | + |
|
| 421 | + public function setAction($action) |
|
| 422 | + { |
|
| 423 | + $this->action = $action; |
|
| 424 | + return $this; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * @param string $namespace |
|
| 429 | + * @return self |
|
| 430 | + */ |
|
| 431 | + |
|
| 432 | + public function setNamespace($namespace) |
|
| 433 | + { |
|
| 434 | + $this->namespace = $namespace; |
|
| 435 | + return $this; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * @param string[] $params |
|
| 440 | + * @return self |
|
| 441 | + */ |
|
| 442 | + |
|
| 443 | + public function setParams(array $params) |
|
| 444 | + { |
|
| 445 | + $this->params = $params; |
|
| 446 | + return $this; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * @param string $key |
|
| 451 | + * @param string $value |
|
| 452 | + * |
|
| 453 | + * @return self |
|
| 454 | + */ |
|
| 455 | + |
|
| 456 | + public function setParam($key, $value) |
|
| 457 | + { |
|
| 458 | + $this->params[$key] = $value; |
|
| 459 | + return $this; |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * @param mixed[] $defaults |
|
| 464 | + * @return self |
|
| 465 | + */ |
|
| 466 | + |
|
| 467 | + public function setDefaults(array $defaults) |
|
| 468 | + { |
|
| 469 | + $this->defaults = $defaults; |
|
| 470 | + return $this; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + /** |
|
| 474 | + * @param string $key |
|
| 475 | + * @param mixed $value |
|
| 476 | + * |
|
| 477 | + * @return self |
|
| 478 | + */ |
|
| 479 | + |
|
| 480 | + public function setDefault($key, $value) |
|
| 481 | + { |
|
| 482 | + $this->defaults[$key] = $value; |
|
| 483 | + return $this; |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * @param mixed[] $metadata |
|
| 488 | + * @return self |
|
| 489 | + */ |
|
| 490 | + |
|
| 491 | + public function setMetadataArray(array $metadata) |
|
| 492 | + { |
|
| 493 | + $this->metadata = $metadata; |
|
| 494 | + return $this; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * @param string $key |
|
| 499 | + * @param mixed $value |
|
| 500 | + * |
|
| 501 | + * @return $this |
|
| 502 | + */ |
|
| 503 | + |
|
| 504 | + public function setMetadata($key, $value) |
|
| 505 | + { |
|
| 506 | + $this->metadata[$key] = $value; |
|
| 507 | + return $this; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * @param string|StrategyInterface $strategy |
|
| 512 | + * @return self |
|
| 513 | + */ |
|
| 514 | + |
|
| 515 | + public function setStrategy($strategy) |
|
| 516 | + { |
|
| 517 | + $this->strategy = $strategy; |
|
| 518 | + return $this; |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + /** |
|
| 522 | + * @inheritdoc |
|
| 523 | + */ |
|
| 524 | + |
|
| 525 | + public function setMatcher(Matcher $matcher) |
|
| 526 | + { |
|
| 527 | + $this->matcher = $matcher; |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * @param string $key |
|
| 532 | + * @return bool |
|
| 533 | + */ |
|
| 534 | + |
|
| 535 | + public function hasParam($key) |
|
| 536 | + { |
|
| 537 | + return isset($this->params[$key]); |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * @param string $key |
|
| 542 | + * @return bool |
|
| 543 | + */ |
|
| 544 | + |
|
| 545 | + public function hasDefault($key) |
|
| 546 | + { |
|
| 547 | + return isset($this->defaults[$key]); |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * @param string $key |
|
| 552 | + * @return bool |
|
| 553 | + */ |
|
| 554 | + |
|
| 555 | + public function hasMetadata($key) |
|
| 556 | + { |
|
| 557 | + return isset($this->metadata[$key]); |
|
| 558 | + } |
|
| 559 | 559 | |
| 560 | 560 | } |
@@ -197,7 +197,7 @@ |
||
| 197 | 197 | } |
| 198 | 198 | } |
| 199 | 199 | |
| 200 | - throw new BadRouteException("`$this->strategy` is not a valid route dispatch strategy, ". |
|
| 200 | + throw new BadRouteException("`$this->strategy` is not a valid route dispatch strategy, " . |
|
| 201 | 201 | "it must implement the `Codeburner\Router\Strategies\StrategyInterface` interface."); |
| 202 | 202 | } |
| 203 | 203 | |
@@ -219,7 +219,9 @@ |
||
| 219 | 219 | if (is_string($callable[0])) { |
| 220 | 220 | $callable[0] = rtrim($this->namespace, "\\") . "\\" . $this->parseCallablePlaceholders($callable[0]); |
| 221 | 221 | $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
| 222 | - } else $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
| 222 | + } else { |
|
| 223 | + $callable[1] = $this->parseCallablePlaceholders($callable[1]); |
|
| 224 | + } |
|
| 223 | 225 | } |
| 224 | 226 | |
| 225 | 227 | return $callable; |
@@ -24,37 +24,37 @@ |
||
| 24 | 24 | abstract class EnhancerAbstractStrategy implements StrategyInterface |
| 25 | 25 | { |
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Key used to store the real route strategy on metadata. |
|
| 29 | - * |
|
| 30 | - * @var string |
|
| 31 | - */ |
|
| 32 | - |
|
| 33 | - protected $metadataStrategyKey = "strategy"; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @inheritdoc |
|
| 37 | - * @throws BadRouteException |
|
| 38 | - */ |
|
| 39 | - |
|
| 40 | - public function call(Route $route) |
|
| 41 | - { |
|
| 42 | - if ($route->hasMetadata($this->metadataStrategyKey)) { |
|
| 43 | - $route->setStrategy($route->getMetadata($this->metadataStrategyKey)); |
|
| 44 | - } else $route->setStrategy(null); |
|
| 45 | - |
|
| 46 | - $this->enhance($route); |
|
| 47 | - |
|
| 48 | - return $route->call(); |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Manipulate route object before the dispatch. |
|
| 53 | - * |
|
| 54 | - * @param Route $route |
|
| 55 | - * @return mixed |
|
| 56 | - */ |
|
| 57 | - |
|
| 58 | - abstract public function enhance(Route $route); |
|
| 27 | + /** |
|
| 28 | + * Key used to store the real route strategy on metadata. |
|
| 29 | + * |
|
| 30 | + * @var string |
|
| 31 | + */ |
|
| 32 | + |
|
| 33 | + protected $metadataStrategyKey = "strategy"; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @inheritdoc |
|
| 37 | + * @throws BadRouteException |
|
| 38 | + */ |
|
| 39 | + |
|
| 40 | + public function call(Route $route) |
|
| 41 | + { |
|
| 42 | + if ($route->hasMetadata($this->metadataStrategyKey)) { |
|
| 43 | + $route->setStrategy($route->getMetadata($this->metadataStrategyKey)); |
|
| 44 | + } else $route->setStrategy(null); |
|
| 45 | + |
|
| 46 | + $this->enhance($route); |
|
| 47 | + |
|
| 48 | + return $route->call(); |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Manipulate route object before the dispatch. |
|
| 53 | + * |
|
| 54 | + * @param Route $route |
|
| 55 | + * @return mixed |
|
| 56 | + */ |
|
| 57 | + |
|
| 58 | + abstract public function enhance(Route $route); |
|
| 59 | 59 | |
| 60 | 60 | } |
@@ -41,7 +41,9 @@ |
||
| 41 | 41 | { |
| 42 | 42 | if ($route->hasMetadata($this->metadataStrategyKey)) { |
| 43 | 43 | $route->setStrategy($route->getMetadata($this->metadataStrategyKey)); |
| 44 | - } else $route->setStrategy(null); |
|
| 44 | + } else { |
|
| 45 | + $route->setStrategy(null); |
|
| 46 | + } |
|
| 45 | 47 | |
| 46 | 48 | $this->enhance($route); |
| 47 | 49 | |
@@ -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 | } |