@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | * @var array |
62 | 62 | */ |
63 | 63 | |
64 | - protected $statics = []; |
|
64 | + protected $statics = []; |
|
65 | 65 | |
66 | 66 | /** |
67 | 67 | * The dynamic routes have parameters and are stored in a hashtable that every cell have |
@@ -123,10 +123,10 @@ discard block |
||
123 | 123 | return new Group($group); |
124 | 124 | } |
125 | 125 | |
126 | - public function get ($pattern, $action) { return $this->set("get" , $pattern, $action); } |
|
127 | - public function post ($pattern, $action) { return $this->set("post" , $pattern, $action); } |
|
128 | - public function put ($pattern, $action) { return $this->set("put" , $pattern, $action); } |
|
129 | - public function patch ($pattern, $action) { return $this->set("patch" , $pattern, $action); } |
|
126 | + public function get($pattern, $action) { return $this->set("get", $pattern, $action); } |
|
127 | + public function post($pattern, $action) { return $this->set("post", $pattern, $action); } |
|
128 | + public function put($pattern, $action) { return $this->set("put", $pattern, $action); } |
|
129 | + public function patch($pattern, $action) { return $this->set("patch", $pattern, $action); } |
|
130 | 130 | public function delete($pattern, $action) { return $this->set("delete", $pattern, $action); } |
131 | 131 | |
132 | 132 | /** |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | */ |
21 | 21 | |
22 | 22 | if (!class_exists(Parser::class, false)) { |
23 | - include __DIR__ . "/Parser.php"; |
|
23 | + include __DIR__ . "/Parser.php"; |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | include __DIR__ . "/Route.php"; |
@@ -37,294 +37,294 @@ discard block |
||
37 | 37 | class Collector |
38 | 38 | { |
39 | 39 | |
40 | - use Collectors\ControllerCollectorTrait; |
|
41 | - use Collectors\ResourceCollectorTrait; |
|
42 | - |
|
43 | - |
|
44 | - /** |
|
45 | - * All the supported http methods separated by spaces. |
|
46 | - * |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - |
|
50 | - const HTTP_METHODS = "get post put patch delete"; |
|
51 | - |
|
52 | - /** |
|
53 | - * The static routes are simple stored in a multidimensional array, the first |
|
54 | - * dimension is indexed by an http method and hold an array indexed with the patterns |
|
55 | - * and holding the route. ex. [METHOD => [PATTERN => ROUTE]] |
|
56 | - * |
|
57 | - * @var array |
|
58 | - */ |
|
59 | - |
|
60 | - protected $statics = []; |
|
61 | - |
|
62 | - /** |
|
63 | - * The dynamic routes have parameters and are stored in a hashtable that every cell have |
|
64 | - * an array with route patterns as indexes and routes as values. ex. [INDEX => [PATTERN => ROUTE]] |
|
65 | - * |
|
66 | - * @var array |
|
67 | - */ |
|
68 | - |
|
69 | - protected $dynamics = []; |
|
70 | - |
|
71 | - /** |
|
72 | - * The pattern parser instance. |
|
73 | - * |
|
74 | - * @var Parser |
|
75 | - */ |
|
76 | - |
|
77 | - protected $parser; |
|
78 | - |
|
79 | - /** |
|
80 | - * Collector constructor. |
|
81 | - * |
|
82 | - * @param Parser|null $parser |
|
83 | - */ |
|
84 | - |
|
85 | - public function __construct(Parser $parser = null) |
|
86 | - { |
|
87 | - $this->parser = $parser ?: new Parser; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * @param string $method |
|
92 | - * @param string $pattern |
|
93 | - * @param callable $action |
|
94 | - * |
|
95 | - * @throws BadRouteException |
|
96 | - * @throws MethodNotSupportedException |
|
97 | - * |
|
98 | - * @return Group |
|
99 | - */ |
|
100 | - |
|
101 | - public function set($method, $pattern, $action) |
|
102 | - { |
|
103 | - $method = $this->getValidMethod($method); |
|
104 | - $patterns = $this->parser->parsePattern($pattern); |
|
105 | - $group = new Group; |
|
106 | - |
|
107 | - foreach ($patterns as $pattern) |
|
108 | - { |
|
109 | - $route = new Route($this, $method, $pattern, $action); |
|
110 | - $group->setRoute($route); |
|
111 | - |
|
112 | - if (strpos($pattern, "{") !== false) { |
|
113 | - $index = $this->getDynamicIndex($method, $pattern); |
|
114 | - $this->dynamics[$index][$pattern] = $route; |
|
115 | - } else $this->statics[$method][$pattern] = $route; |
|
116 | - } |
|
117 | - |
|
118 | - return $group; |
|
119 | - } |
|
120 | - |
|
121 | - public function get ($pattern, $action) { return $this->set("get" , $pattern, $action); } |
|
122 | - public function post ($pattern, $action) { return $this->set("post" , $pattern, $action); } |
|
123 | - public function put ($pattern, $action) { return $this->set("put" , $pattern, $action); } |
|
124 | - public function patch ($pattern, $action) { return $this->set("patch" , $pattern, $action); } |
|
125 | - public function delete($pattern, $action) { return $this->set("delete", $pattern, $action); } |
|
126 | - |
|
127 | - /** |
|
128 | - * Insert a route into several http methods. |
|
129 | - * |
|
130 | - * @param string[] $methods |
|
131 | - * @param string $pattern |
|
132 | - * @param callable $action |
|
133 | - * |
|
134 | - * @return Group |
|
135 | - */ |
|
136 | - |
|
137 | - public function match(array $methods, $pattern, $action) |
|
138 | - { |
|
139 | - $group = new Group; |
|
140 | - foreach ($methods as $method) |
|
141 | - $group->set($this->set($method, $pattern, $action)); |
|
142 | - return $group; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Insert a route into every http method supported. |
|
147 | - * |
|
148 | - * @param string $pattern |
|
149 | - * @param callable $action |
|
150 | - * |
|
151 | - * @return Group |
|
152 | - */ |
|
153 | - |
|
154 | - public function any($pattern, $action) |
|
155 | - { |
|
156 | - return $this->match(explode(" ", self::HTTP_METHODS), $pattern, $action); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Insert a route into every http method supported but the given ones. |
|
161 | - * |
|
162 | - * @param string $methods |
|
163 | - * @param string $pattern |
|
164 | - * @param callable $action |
|
165 | - * |
|
166 | - * @return Group |
|
167 | - */ |
|
168 | - |
|
169 | - public function except($methods, $pattern, $action) |
|
170 | - { |
|
171 | - return $this->match(array_diff(explode(" ", self::HTTP_METHODS), (array) $methods), $pattern, $action); |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Group all given routes. |
|
176 | - * |
|
177 | - * @param Route[] $routes |
|
178 | - * @return Group |
|
179 | - */ |
|
180 | - |
|
181 | - public function group(array $routes) |
|
182 | - { |
|
183 | - $group = new Group; |
|
184 | - foreach ($routes as $route) |
|
185 | - $group->set($route); |
|
186 | - return $group; |
|
187 | - } |
|
188 | - |
|
189 | - /** |
|
190 | - * Remove a route from collector. |
|
191 | - * |
|
192 | - * @param string $method |
|
193 | - * @param string $pattern |
|
194 | - */ |
|
195 | - |
|
196 | - public function forget($method, $pattern) |
|
197 | - { |
|
198 | - if (strpos($pattern, "{") === false) { |
|
199 | - unset($this->statics[$method][$pattern]); |
|
200 | - } else unset($this->dynamics[$this->getDynamicIndex($method, $pattern)][$pattern]); |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @param string $method |
|
205 | - * @param string $pattern |
|
206 | - * |
|
207 | - * @return Route|false |
|
208 | - */ |
|
209 | - |
|
210 | - public function findStaticRoute($method, $pattern) |
|
211 | - { |
|
212 | - $method = strtolower($method); |
|
213 | - if (isset($this->statics[$method]) && isset($this->statics[$method][$pattern])) |
|
214 | - return $this->statics[$method][$pattern]; |
|
215 | - return false; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * @param string $method |
|
220 | - * @param string $pattern |
|
221 | - * |
|
222 | - * @return array|false |
|
223 | - */ |
|
224 | - |
|
225 | - public function findDynamicRoutes($method, $pattern) |
|
226 | - { |
|
227 | - $index = $this->getDynamicIndex($method, $pattern); |
|
228 | - return isset($this->dynamics[$index]) ? $this->dynamics[$index] : false; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * @param string $method |
|
233 | - * @param string $pattern |
|
234 | - * |
|
235 | - * @return int |
|
236 | - */ |
|
237 | - |
|
238 | - protected function getDynamicIndex($method, $pattern) |
|
239 | - { |
|
240 | - return crc32(strtolower($method)) + substr_count($pattern, "/"); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Determine if the http method is valid. |
|
245 | - * |
|
246 | - * @param string $method |
|
247 | - * |
|
248 | - * @throws MethodNotSupportedException |
|
249 | - * @return string |
|
250 | - */ |
|
251 | - |
|
252 | - protected function getValidMethod($method) |
|
253 | - { |
|
254 | - $method = strtolower($method); |
|
255 | - |
|
256 | - if (strpos(self::HTTP_METHODS, $method) === false) { |
|
257 | - throw new MethodNotSupportedException($method); |
|
258 | - } |
|
259 | - |
|
260 | - return $method; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * @return string[] |
|
265 | - */ |
|
266 | - |
|
267 | - public function getWildcards() |
|
268 | - { |
|
269 | - return $this->parser->getWildcards(); |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * @return string[] |
|
274 | - */ |
|
275 | - |
|
276 | - public function getWildcardTokens() |
|
277 | - { |
|
278 | - return $this->parser->getWildcardTokens(); |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * @param string $wildcard |
|
283 | - * @return string|null |
|
284 | - */ |
|
285 | - |
|
286 | - public function getWildcard($wildcard) |
|
287 | - { |
|
288 | - return $this->parser->getWildcard($wildcard); |
|
289 | - } |
|
290 | - |
|
291 | - /** |
|
292 | - * @param string $wildcard |
|
293 | - * @param string $pattern |
|
294 | - * |
|
295 | - * @return self |
|
296 | - */ |
|
297 | - |
|
298 | - public function setWildcard($wildcard, $pattern) |
|
299 | - { |
|
300 | - $this->parser->setWildcard($wildcard, $pattern); |
|
301 | - return $this; |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * @return Parser |
|
306 | - */ |
|
307 | - |
|
308 | - public function getParser() |
|
309 | - { |
|
310 | - return $this->parser; |
|
311 | - } |
|
312 | - |
|
313 | - /** |
|
314 | - * @param Parser $parser |
|
315 | - * |
|
316 | - * @throws \LogicException |
|
317 | - * @return self |
|
318 | - */ |
|
319 | - |
|
320 | - public function setParser(Parser $parser) |
|
321 | - { |
|
322 | - if (!empty($this->statics) || !empty($this->dynamics)) { |
|
323 | - throw new \LogicException("You can't define a route parser after registering a route."); |
|
324 | - } |
|
325 | - |
|
326 | - $this->parser = $parser; |
|
327 | - return $this; |
|
328 | - } |
|
40 | + use Collectors\ControllerCollectorTrait; |
|
41 | + use Collectors\ResourceCollectorTrait; |
|
42 | + |
|
43 | + |
|
44 | + /** |
|
45 | + * All the supported http methods separated by spaces. |
|
46 | + * |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + |
|
50 | + const HTTP_METHODS = "get post put patch delete"; |
|
51 | + |
|
52 | + /** |
|
53 | + * The static routes are simple stored in a multidimensional array, the first |
|
54 | + * dimension is indexed by an http method and hold an array indexed with the patterns |
|
55 | + * and holding the route. ex. [METHOD => [PATTERN => ROUTE]] |
|
56 | + * |
|
57 | + * @var array |
|
58 | + */ |
|
59 | + |
|
60 | + protected $statics = []; |
|
61 | + |
|
62 | + /** |
|
63 | + * The dynamic routes have parameters and are stored in a hashtable that every cell have |
|
64 | + * an array with route patterns as indexes and routes as values. ex. [INDEX => [PATTERN => ROUTE]] |
|
65 | + * |
|
66 | + * @var array |
|
67 | + */ |
|
68 | + |
|
69 | + protected $dynamics = []; |
|
70 | + |
|
71 | + /** |
|
72 | + * The pattern parser instance. |
|
73 | + * |
|
74 | + * @var Parser |
|
75 | + */ |
|
76 | + |
|
77 | + protected $parser; |
|
78 | + |
|
79 | + /** |
|
80 | + * Collector constructor. |
|
81 | + * |
|
82 | + * @param Parser|null $parser |
|
83 | + */ |
|
84 | + |
|
85 | + public function __construct(Parser $parser = null) |
|
86 | + { |
|
87 | + $this->parser = $parser ?: new Parser; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * @param string $method |
|
92 | + * @param string $pattern |
|
93 | + * @param callable $action |
|
94 | + * |
|
95 | + * @throws BadRouteException |
|
96 | + * @throws MethodNotSupportedException |
|
97 | + * |
|
98 | + * @return Group |
|
99 | + */ |
|
100 | + |
|
101 | + public function set($method, $pattern, $action) |
|
102 | + { |
|
103 | + $method = $this->getValidMethod($method); |
|
104 | + $patterns = $this->parser->parsePattern($pattern); |
|
105 | + $group = new Group; |
|
106 | + |
|
107 | + foreach ($patterns as $pattern) |
|
108 | + { |
|
109 | + $route = new Route($this, $method, $pattern, $action); |
|
110 | + $group->setRoute($route); |
|
111 | + |
|
112 | + if (strpos($pattern, "{") !== false) { |
|
113 | + $index = $this->getDynamicIndex($method, $pattern); |
|
114 | + $this->dynamics[$index][$pattern] = $route; |
|
115 | + } else $this->statics[$method][$pattern] = $route; |
|
116 | + } |
|
117 | + |
|
118 | + return $group; |
|
119 | + } |
|
120 | + |
|
121 | + public function get ($pattern, $action) { return $this->set("get" , $pattern, $action); } |
|
122 | + public function post ($pattern, $action) { return $this->set("post" , $pattern, $action); } |
|
123 | + public function put ($pattern, $action) { return $this->set("put" , $pattern, $action); } |
|
124 | + public function patch ($pattern, $action) { return $this->set("patch" , $pattern, $action); } |
|
125 | + public function delete($pattern, $action) { return $this->set("delete", $pattern, $action); } |
|
126 | + |
|
127 | + /** |
|
128 | + * Insert a route into several http methods. |
|
129 | + * |
|
130 | + * @param string[] $methods |
|
131 | + * @param string $pattern |
|
132 | + * @param callable $action |
|
133 | + * |
|
134 | + * @return Group |
|
135 | + */ |
|
136 | + |
|
137 | + public function match(array $methods, $pattern, $action) |
|
138 | + { |
|
139 | + $group = new Group; |
|
140 | + foreach ($methods as $method) |
|
141 | + $group->set($this->set($method, $pattern, $action)); |
|
142 | + return $group; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Insert a route into every http method supported. |
|
147 | + * |
|
148 | + * @param string $pattern |
|
149 | + * @param callable $action |
|
150 | + * |
|
151 | + * @return Group |
|
152 | + */ |
|
153 | + |
|
154 | + public function any($pattern, $action) |
|
155 | + { |
|
156 | + return $this->match(explode(" ", self::HTTP_METHODS), $pattern, $action); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Insert a route into every http method supported but the given ones. |
|
161 | + * |
|
162 | + * @param string $methods |
|
163 | + * @param string $pattern |
|
164 | + * @param callable $action |
|
165 | + * |
|
166 | + * @return Group |
|
167 | + */ |
|
168 | + |
|
169 | + public function except($methods, $pattern, $action) |
|
170 | + { |
|
171 | + return $this->match(array_diff(explode(" ", self::HTTP_METHODS), (array) $methods), $pattern, $action); |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Group all given routes. |
|
176 | + * |
|
177 | + * @param Route[] $routes |
|
178 | + * @return Group |
|
179 | + */ |
|
180 | + |
|
181 | + public function group(array $routes) |
|
182 | + { |
|
183 | + $group = new Group; |
|
184 | + foreach ($routes as $route) |
|
185 | + $group->set($route); |
|
186 | + return $group; |
|
187 | + } |
|
188 | + |
|
189 | + /** |
|
190 | + * Remove a route from collector. |
|
191 | + * |
|
192 | + * @param string $method |
|
193 | + * @param string $pattern |
|
194 | + */ |
|
195 | + |
|
196 | + public function forget($method, $pattern) |
|
197 | + { |
|
198 | + if (strpos($pattern, "{") === false) { |
|
199 | + unset($this->statics[$method][$pattern]); |
|
200 | + } else unset($this->dynamics[$this->getDynamicIndex($method, $pattern)][$pattern]); |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @param string $method |
|
205 | + * @param string $pattern |
|
206 | + * |
|
207 | + * @return Route|false |
|
208 | + */ |
|
209 | + |
|
210 | + public function findStaticRoute($method, $pattern) |
|
211 | + { |
|
212 | + $method = strtolower($method); |
|
213 | + if (isset($this->statics[$method]) && isset($this->statics[$method][$pattern])) |
|
214 | + return $this->statics[$method][$pattern]; |
|
215 | + return false; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * @param string $method |
|
220 | + * @param string $pattern |
|
221 | + * |
|
222 | + * @return array|false |
|
223 | + */ |
|
224 | + |
|
225 | + public function findDynamicRoutes($method, $pattern) |
|
226 | + { |
|
227 | + $index = $this->getDynamicIndex($method, $pattern); |
|
228 | + return isset($this->dynamics[$index]) ? $this->dynamics[$index] : false; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * @param string $method |
|
233 | + * @param string $pattern |
|
234 | + * |
|
235 | + * @return int |
|
236 | + */ |
|
237 | + |
|
238 | + protected function getDynamicIndex($method, $pattern) |
|
239 | + { |
|
240 | + return crc32(strtolower($method)) + substr_count($pattern, "/"); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Determine if the http method is valid. |
|
245 | + * |
|
246 | + * @param string $method |
|
247 | + * |
|
248 | + * @throws MethodNotSupportedException |
|
249 | + * @return string |
|
250 | + */ |
|
251 | + |
|
252 | + protected function getValidMethod($method) |
|
253 | + { |
|
254 | + $method = strtolower($method); |
|
255 | + |
|
256 | + if (strpos(self::HTTP_METHODS, $method) === false) { |
|
257 | + throw new MethodNotSupportedException($method); |
|
258 | + } |
|
259 | + |
|
260 | + return $method; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * @return string[] |
|
265 | + */ |
|
266 | + |
|
267 | + public function getWildcards() |
|
268 | + { |
|
269 | + return $this->parser->getWildcards(); |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * @return string[] |
|
274 | + */ |
|
275 | + |
|
276 | + public function getWildcardTokens() |
|
277 | + { |
|
278 | + return $this->parser->getWildcardTokens(); |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * @param string $wildcard |
|
283 | + * @return string|null |
|
284 | + */ |
|
285 | + |
|
286 | + public function getWildcard($wildcard) |
|
287 | + { |
|
288 | + return $this->parser->getWildcard($wildcard); |
|
289 | + } |
|
290 | + |
|
291 | + /** |
|
292 | + * @param string $wildcard |
|
293 | + * @param string $pattern |
|
294 | + * |
|
295 | + * @return self |
|
296 | + */ |
|
297 | + |
|
298 | + public function setWildcard($wildcard, $pattern) |
|
299 | + { |
|
300 | + $this->parser->setWildcard($wildcard, $pattern); |
|
301 | + return $this; |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * @return Parser |
|
306 | + */ |
|
307 | + |
|
308 | + public function getParser() |
|
309 | + { |
|
310 | + return $this->parser; |
|
311 | + } |
|
312 | + |
|
313 | + /** |
|
314 | + * @param Parser $parser |
|
315 | + * |
|
316 | + * @throws \LogicException |
|
317 | + * @return self |
|
318 | + */ |
|
319 | + |
|
320 | + public function setParser(Parser $parser) |
|
321 | + { |
|
322 | + if (!empty($this->statics) || !empty($this->dynamics)) { |
|
323 | + throw new \LogicException("You can't define a route parser after registering a route."); |
|
324 | + } |
|
325 | + |
|
326 | + $this->parser = $parser; |
|
327 | + return $this; |
|
328 | + } |
|
329 | 329 | |
330 | 330 | } |
@@ -112,7 +112,9 @@ discard block |
||
112 | 112 | if (strpos($pattern, "{") !== false) { |
113 | 113 | $index = $this->getDynamicIndex($method, $pattern); |
114 | 114 | $this->dynamics[$index][$pattern] = $route; |
115 | - } else $this->statics[$method][$pattern] = $route; |
|
115 | + } else { |
|
116 | + $this->statics[$method][$pattern] = $route; |
|
117 | + } |
|
116 | 118 | } |
117 | 119 | |
118 | 120 | return $group; |
@@ -137,8 +139,9 @@ discard block |
||
137 | 139 | public function match(array $methods, $pattern, $action) |
138 | 140 | { |
139 | 141 | $group = new Group; |
140 | - foreach ($methods as $method) |
|
141 | - $group->set($this->set($method, $pattern, $action)); |
|
142 | + foreach ($methods as $method) { |
|
143 | + $group->set($this->set($method, $pattern, $action)); |
|
144 | + } |
|
142 | 145 | return $group; |
143 | 146 | } |
144 | 147 | |
@@ -181,8 +184,9 @@ discard block |
||
181 | 184 | public function group(array $routes) |
182 | 185 | { |
183 | 186 | $group = new Group; |
184 | - foreach ($routes as $route) |
|
185 | - $group->set($route); |
|
187 | + foreach ($routes as $route) { |
|
188 | + $group->set($route); |
|
189 | + } |
|
186 | 190 | return $group; |
187 | 191 | } |
188 | 192 | |
@@ -197,7 +201,9 @@ discard block |
||
197 | 201 | { |
198 | 202 | if (strpos($pattern, "{") === false) { |
199 | 203 | unset($this->statics[$method][$pattern]); |
200 | - } else unset($this->dynamics[$this->getDynamicIndex($method, $pattern)][$pattern]); |
|
204 | + } else { |
|
205 | + unset($this->dynamics[$this->getDynamicIndex($method, $pattern)][$pattern]); |
|
206 | + } |
|
201 | 207 | } |
202 | 208 | |
203 | 209 | /** |
@@ -210,8 +216,9 @@ discard block |
||
210 | 216 | public function findStaticRoute($method, $pattern) |
211 | 217 | { |
212 | 218 | $method = strtolower($method); |
213 | - if (isset($this->statics[$method]) && isset($this->statics[$method][$pattern])) |
|
214 | - return $this->statics[$method][$pattern]; |
|
219 | + if (isset($this->statics[$method]) && isset($this->statics[$method][$pattern])) { |
|
220 | + return $this->statics[$method][$pattern]; |
|
221 | + } |
|
215 | 222 | return false; |
216 | 223 | } |
217 | 224 |
@@ -31,12 +31,12 @@ discard block |
||
31 | 31 | */ |
32 | 32 | |
33 | 33 | protected $map = [ |
34 | - "index" => ["get", "/{name}"], |
|
35 | - "make" => ["get", "/{name}/make"], |
|
36 | - "create" => ["post", "/{name}"], |
|
37 | - "show" => ["get", "/{name}/{id:int+}"], |
|
38 | - "edit" => ["get", "/{name}/{id:int+}/edit"], |
|
39 | - "update" => ["put", "/{name}/{id:int+}"], |
|
34 | + "index" => ["get", "/{name}"], |
|
35 | + "make" => ["get", "/{name}/make"], |
|
36 | + "create" => ["post", "/{name}"], |
|
37 | + "show" => ["get", "/{name}/{id:int+}"], |
|
38 | + "edit" => ["get", "/{name}/{id:int+}/edit"], |
|
39 | + "update" => ["put", "/{name}/{id:int+}"], |
|
40 | 40 | "delete" => ["delete", "/{name}/{id:int+}"], |
41 | 41 | ]; |
42 | 42 | |
@@ -100,8 +100,7 @@ discard block |
||
100 | 100 | |
101 | 101 | protected function getResourceActions(array $options) |
102 | 102 | { |
103 | - return isset($options["only"]) ? array_intersect_key($this->map, array_flip((array) $options["only"])) : |
|
104 | - (isset($options["except"]) ? array_diff_key($this->map, array_flip((array) $options["except"])) : $this->map); |
|
103 | + return isset($options["only"]) ? array_intersect_key($this->map, array_flip((array) $options["only"])) : (isset($options["except"]) ? array_diff_key($this->map, array_flip((array) $options["except"])) : $this->map); |
|
105 | 104 | } |
106 | 105 | |
107 | 106 | /** |
@@ -116,8 +115,7 @@ discard block |
||
116 | 115 | protected function getResourcePath($action, $path, $name, array $options) |
117 | 116 | { |
118 | 117 | return str_replace("{name}", $name, |
119 | - $action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) : |
|
120 | - ($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path)); |
|
118 | + $action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) : ($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path)); |
|
121 | 119 | } |
122 | 120 | |
123 | 121 | } |
@@ -76,8 +76,9 @@ |
||
76 | 76 | public function resources(array $controllers) |
77 | 77 | { |
78 | 78 | $resource = new Resource; |
79 | - foreach ($controllers as $controller) |
|
80 | - $resource->set($this->resource($controller)); |
|
79 | + foreach ($controllers as $controller) { |
|
80 | + $resource->set($this->resource($controller)); |
|
81 | + } |
|
81 | 82 | return $resource; |
82 | 83 | } |
83 | 84 |
@@ -27,102 +27,102 @@ |
||
27 | 27 | trait ResourceCollectorTrait |
28 | 28 | { |
29 | 29 | |
30 | - abstract public function set($method, $pattern, $action); |
|
31 | - |
|
32 | - /** |
|
33 | - * A map of all routes of resources. |
|
34 | - * |
|
35 | - * @var array |
|
36 | - */ |
|
37 | - |
|
38 | - protected $map = [ |
|
39 | - "index" => ["get", "/{name}"], |
|
40 | - "make" => ["get", "/{name}/make"], |
|
41 | - "create" => ["post", "/{name}"], |
|
42 | - "show" => ["get", "/{name}/{id:int+}"], |
|
43 | - "edit" => ["get", "/{name}/{id:int+}/edit"], |
|
44 | - "update" => ["put", "/{name}/{id:int+}"], |
|
45 | - "delete" => ["delete", "/{name}/{id:int+}"], |
|
46 | - ]; |
|
47 | - |
|
48 | - /** |
|
49 | - * Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. |
|
50 | - * Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, |
|
51 | - * a resourceful route declares them in a single line of code. |
|
52 | - * |
|
53 | - * @param string $controller The controller name. |
|
54 | - * @param array $options Some options like, "as" to name the route pattern, "only" to |
|
55 | - * explicit say that only this routes will be registered, and |
|
56 | - * "except" that register all the routes except the indicates. |
|
57 | - * @return RouteResource |
|
58 | - */ |
|
59 | - |
|
60 | - public function resource($controller, array $options = array()) |
|
61 | - { |
|
62 | - $name = isset($options["prefix"]) ? $options["prefix"] : ""; |
|
63 | - $name .= $this->getResourceName($controller, $options); |
|
64 | - $actions = $this->getResourceActions($options); |
|
65 | - $resource = new RouteResource; |
|
66 | - |
|
67 | - foreach ($actions as $action => $map) { |
|
68 | - $resource->set($this->set($map[0], $this->getResourcePath($action, $map[1], $name, $options), [$controller, $action])); |
|
69 | - } |
|
70 | - |
|
71 | - return $resource; |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Collect several resources at same time. |
|
76 | - * |
|
77 | - * @param array $controllers Several controller names as parameters or an array with all controller names. |
|
78 | - * @return RouteResource |
|
79 | - */ |
|
80 | - |
|
81 | - public function resources(array $controllers) |
|
82 | - { |
|
83 | - $resource = new RouteResource; |
|
84 | - foreach ($controllers as $controller) |
|
85 | - $resource->set($this->resource($controller)); |
|
86 | - return $resource; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @param string $controller |
|
91 | - * @param array $options |
|
92 | - * |
|
93 | - * @return mixed |
|
94 | - */ |
|
95 | - |
|
96 | - protected function getResourceName($controller, array $options) |
|
97 | - { |
|
98 | - return isset($options["as"]) ? $options["as"] : str_replace("controller", "", strtolower($controller)); |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @param array $options |
|
103 | - * @return array |
|
104 | - */ |
|
105 | - |
|
106 | - protected function getResourceActions(array $options) |
|
107 | - { |
|
108 | - return isset($options["only"]) ? array_intersect_key($this->map, array_flip((array) $options["only"])) : |
|
109 | - (isset($options["except"]) ? array_diff_key($this->map, array_flip((array) $options["except"])) : $this->map); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param string $action |
|
114 | - * @param string $path |
|
115 | - * @param string $name |
|
116 | - * @param string[] $options |
|
117 | - * |
|
118 | - * @return string |
|
119 | - */ |
|
120 | - |
|
121 | - protected function getResourcePath($action, $path, $name, array $options) |
|
122 | - { |
|
123 | - return str_replace("{name}", $name, |
|
124 | - $action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) : |
|
125 | - ($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path)); |
|
126 | - } |
|
30 | + abstract public function set($method, $pattern, $action); |
|
31 | + |
|
32 | + /** |
|
33 | + * A map of all routes of resources. |
|
34 | + * |
|
35 | + * @var array |
|
36 | + */ |
|
37 | + |
|
38 | + protected $map = [ |
|
39 | + "index" => ["get", "/{name}"], |
|
40 | + "make" => ["get", "/{name}/make"], |
|
41 | + "create" => ["post", "/{name}"], |
|
42 | + "show" => ["get", "/{name}/{id:int+}"], |
|
43 | + "edit" => ["get", "/{name}/{id:int+}/edit"], |
|
44 | + "update" => ["put", "/{name}/{id:int+}"], |
|
45 | + "delete" => ["delete", "/{name}/{id:int+}"], |
|
46 | + ]; |
|
47 | + |
|
48 | + /** |
|
49 | + * Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. |
|
50 | + * Instead of declaring separate routes for your index, show, new, edit, create, update and destroy actions, |
|
51 | + * a resourceful route declares them in a single line of code. |
|
52 | + * |
|
53 | + * @param string $controller The controller name. |
|
54 | + * @param array $options Some options like, "as" to name the route pattern, "only" to |
|
55 | + * explicit say that only this routes will be registered, and |
|
56 | + * "except" that register all the routes except the indicates. |
|
57 | + * @return RouteResource |
|
58 | + */ |
|
59 | + |
|
60 | + public function resource($controller, array $options = array()) |
|
61 | + { |
|
62 | + $name = isset($options["prefix"]) ? $options["prefix"] : ""; |
|
63 | + $name .= $this->getResourceName($controller, $options); |
|
64 | + $actions = $this->getResourceActions($options); |
|
65 | + $resource = new RouteResource; |
|
66 | + |
|
67 | + foreach ($actions as $action => $map) { |
|
68 | + $resource->set($this->set($map[0], $this->getResourcePath($action, $map[1], $name, $options), [$controller, $action])); |
|
69 | + } |
|
70 | + |
|
71 | + return $resource; |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Collect several resources at same time. |
|
76 | + * |
|
77 | + * @param array $controllers Several controller names as parameters or an array with all controller names. |
|
78 | + * @return RouteResource |
|
79 | + */ |
|
80 | + |
|
81 | + public function resources(array $controllers) |
|
82 | + { |
|
83 | + $resource = new RouteResource; |
|
84 | + foreach ($controllers as $controller) |
|
85 | + $resource->set($this->resource($controller)); |
|
86 | + return $resource; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @param string $controller |
|
91 | + * @param array $options |
|
92 | + * |
|
93 | + * @return mixed |
|
94 | + */ |
|
95 | + |
|
96 | + protected function getResourceName($controller, array $options) |
|
97 | + { |
|
98 | + return isset($options["as"]) ? $options["as"] : str_replace("controller", "", strtolower($controller)); |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @param array $options |
|
103 | + * @return array |
|
104 | + */ |
|
105 | + |
|
106 | + protected function getResourceActions(array $options) |
|
107 | + { |
|
108 | + return isset($options["only"]) ? array_intersect_key($this->map, array_flip((array) $options["only"])) : |
|
109 | + (isset($options["except"]) ? array_diff_key($this->map, array_flip((array) $options["except"])) : $this->map); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param string $action |
|
114 | + * @param string $path |
|
115 | + * @param string $name |
|
116 | + * @param string[] $options |
|
117 | + * |
|
118 | + * @return string |
|
119 | + */ |
|
120 | + |
|
121 | + protected function getResourcePath($action, $path, $name, array $options) |
|
122 | + { |
|
123 | + return str_replace("{name}", $name, |
|
124 | + $action === "make" && isset($options["translate"]["make"]) ? str_replace("make", $options["translate"]["make"], $path) : |
|
125 | + ($action === "edit" && isset($options["translate"]["edit"]) ? str_replace("edit", $options["translate"]["edit"], $path) : $path)); |
|
126 | + } |
|
127 | 127 | |
128 | 128 | } |
@@ -235,7 +235,7 @@ discard block |
||
235 | 235 | |
236 | 236 | protected function checkStaticRouteInOtherMethods($targetHttpMethod, $path) |
237 | 237 | { |
238 | - return array_filter($this->getHttpMethodsBut($targetHttpMethod), function ($httpMethod) use ($path) { |
|
238 | + return array_filter($this->getHttpMethodsBut($targetHttpMethod), function($httpMethod) use ($path) { |
|
239 | 239 | return (bool) $this->collector->findStaticRoute($httpMethod, $path); |
240 | 240 | }); |
241 | 241 | } |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | |
252 | 252 | protected function checkDynamicRouteInOtherMethods($targetHttpMethod, $path) |
253 | 253 | { |
254 | - return array_filter($this->getHttpMethodsBut($targetHttpMethod), function ($httpMethod) use ($path) { |
|
254 | + return array_filter($this->getHttpMethodsBut($targetHttpMethod), function($httpMethod) use ($path) { |
|
255 | 255 | return (bool) $this->matchDynamicRoute($httpMethod, $path); |
256 | 256 | }); |
257 | 257 | } |
@@ -231,7 +231,7 @@ discard block |
||
231 | 231 | * @param string $targetHttpMethod The HTTP method that must not be checked |
232 | 232 | * @param string $path The Path that must be matched. |
233 | 233 | * |
234 | - * @return array |
|
234 | + * @return string |
|
235 | 235 | */ |
236 | 236 | |
237 | 237 | protected function checkStaticRouteInOtherMethods($targetHttpMethod, $path) |
@@ -247,7 +247,7 @@ discard block |
||
247 | 247 | * @param string $targetHttpMethod The HTTP method that must not be checked |
248 | 248 | * @param string $path The Path that must be matched. |
249 | 249 | * |
250 | - * @return array |
|
250 | + * @return string |
|
251 | 251 | */ |
252 | 252 | |
253 | 253 | protected function checkDynamicRouteInOtherMethods($targetHttpMethod, $path) |
@@ -261,6 +261,7 @@ discard block |
||
261 | 261 | * Strip the given http methods and return all the others. |
262 | 262 | * |
263 | 263 | * @param string|string[] |
264 | + * @param string $targetHttpMethod |
|
264 | 265 | * @return array |
265 | 266 | */ |
266 | 267 |
@@ -23,279 +23,279 @@ |
||
23 | 23 | class Matcher |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * @var Collector |
|
28 | - */ |
|
29 | - |
|
30 | - protected $collector; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var Parser $parser |
|
34 | - */ |
|
35 | - |
|
36 | - protected $parser; |
|
37 | - |
|
38 | - /** |
|
39 | - * Define a basepath to all routes. |
|
40 | - * |
|
41 | - * @var string |
|
42 | - */ |
|
43 | - |
|
44 | - protected $basepath = ""; |
|
45 | - |
|
46 | - /** |
|
47 | - * Construct the route dispatcher. |
|
48 | - * |
|
49 | - * @param Collector $collector |
|
50 | - * @param string $basepath Define a Path prefix that must be excluded on matches. |
|
51 | - */ |
|
52 | - |
|
53 | - public function __construct(Collector $collector, $basepath = "") |
|
54 | - { |
|
55 | - $this->collector = $collector; |
|
56 | - $this->basepath = $basepath; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Find a route that matches the given arguments. |
|
61 | - * |
|
62 | - * @param string $httpMethod |
|
63 | - * @param string $path |
|
64 | - * |
|
65 | - * @throws NotFoundException |
|
66 | - * @throws MethodNotAllowedException |
|
67 | - * |
|
68 | - * @return Route |
|
69 | - */ |
|
70 | - |
|
71 | - public function match($httpMethod, $path) |
|
72 | - { |
|
73 | - $path = $this->parsePath($path); |
|
74 | - |
|
75 | - if (($route = $this->collector->findStaticRoute($httpMethod, $path)) || |
|
76 | - ($route = $this->matchDynamicRoute($httpMethod, $path))) { |
|
77 | - $route->setMatcher($this); |
|
78 | - |
|
79 | - return $route; |
|
80 | - } |
|
81 | - |
|
82 | - $this->matchSimilarRoute($httpMethod, $path); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Find and return the request dynamic route based on the compiled data and Path. |
|
87 | - * |
|
88 | - * @param string $httpMethod |
|
89 | - * @param string $path |
|
90 | - * |
|
91 | - * @return Route|false If the request match an array with the action and parameters will |
|
92 | - * be returned otherwise a false will. |
|
93 | - */ |
|
94 | - |
|
95 | - protected function matchDynamicRoute($httpMethod, $path) |
|
96 | - { |
|
97 | - if ($routes = $this->collector->findDynamicRoutes($httpMethod, $path)) { |
|
98 | - // cache the parser reference |
|
99 | - $this->parser = $this->collector->getParser(); |
|
100 | - // chunk routes for smaller regex groups using the Sturges' Formula |
|
101 | - foreach (array_chunk($routes, round(1 + 3.3 * log(count($routes))), true) as $chunk) { |
|
102 | - array_map([$this, "buildRoute"], $chunk); |
|
103 | - list($pattern, $map) = $this->buildGroup($chunk); |
|
104 | - |
|
105 | - if (!preg_match($pattern, $path, $matches)) { |
|
106 | - continue; |
|
107 | - } |
|
108 | - |
|
109 | - /** @var Route $route */ |
|
110 | - $route = $map[count($matches)]; |
|
111 | - unset($matches[0]); |
|
112 | - |
|
113 | - $route->setParams(array_combine($route->getParams(), array_filter($matches))); |
|
114 | - |
|
115 | - return $route; |
|
116 | - } |
|
117 | - } |
|
118 | - |
|
119 | - return false; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Parse the dynamic segments of the pattern and replace then for |
|
124 | - * corresponding regex. |
|
125 | - * |
|
126 | - * @param Route $route |
|
127 | - * @return Route |
|
128 | - */ |
|
129 | - |
|
130 | - protected function buildRoute(Route $route) |
|
131 | - { |
|
132 | - if ($route->getBlock()) { |
|
133 | - return $route; |
|
134 | - } |
|
135 | - |
|
136 | - list($pattern, $params) = $this->parsePlaceholders($route->getPattern()); |
|
137 | - return $route->setPatternWithoutReset($pattern)->setParams($params)->setBlock(true); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Group several dynamic routes patterns into one big regex and maps |
|
142 | - * the routes to the pattern positions in the big regex. |
|
143 | - * |
|
144 | - * @param Route[] $routes |
|
145 | - * @return array |
|
146 | - */ |
|
147 | - |
|
148 | - protected function buildGroup(array $routes) |
|
149 | - { |
|
150 | - $groupCount = (int) $map = $regex = []; |
|
151 | - |
|
152 | - foreach ($routes as $route) { |
|
153 | - $params = $route->getParams(); |
|
154 | - $paramsCount = count($params); |
|
155 | - $groupCount = max($groupCount, $paramsCount) + 1; |
|
156 | - $regex[] = $route->getPattern() . str_repeat("()", $groupCount - $paramsCount - 1); |
|
157 | - $map[$groupCount] = $route; |
|
158 | - } |
|
159 | - |
|
160 | - return ["~^(?|" . implode("|", $regex) . ")$~", $map]; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Parse an route pattern seeking for parameters and build the route regex. |
|
165 | - * |
|
166 | - * @param string $pattern |
|
167 | - * @return array 0 => new route regex, 1 => map of parameter names |
|
168 | - */ |
|
169 | - |
|
170 | - protected function parsePlaceholders($pattern) |
|
171 | - { |
|
172 | - $params = []; |
|
173 | - $parser = $this->parser; |
|
174 | - |
|
175 | - preg_match_all("~" . $parser::DYNAMIC_REGEX . "~x", $pattern, $matches, PREG_SET_ORDER); |
|
176 | - |
|
177 | - foreach ((array) $matches as $key => $match) { |
|
178 | - $pattern = str_replace($match[0], isset($match[2]) ? "({$match[2]})" : "([^/]+)", $pattern); |
|
179 | - $params[$key] = $match[1]; |
|
180 | - } |
|
181 | - |
|
182 | - return [$pattern, $params]; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * Get only the path of a given url. |
|
187 | - * |
|
188 | - * @param string $path The given URL |
|
189 | - * |
|
190 | - * @throws Exception |
|
191 | - * @return string |
|
192 | - */ |
|
193 | - |
|
194 | - protected function parsePath($path) |
|
195 | - { |
|
196 | - $path = parse_url(substr(strstr(";" . $path, ";" . $this->basepath), strlen(";" . $this->basepath)), PHP_URL_PATH); |
|
197 | - |
|
198 | - if ($path === false) { |
|
199 | - throw new Exception("Seriously malformed URL passed to route matcher."); |
|
200 | - } |
|
201 | - |
|
202 | - return $path; |
|
203 | - } |
|
204 | - |
|
205 | - /** |
|
206 | - * Generate an HTTP error request with method not allowed or not found. |
|
207 | - * |
|
208 | - * @param string $httpMethod |
|
209 | - * @param string $path |
|
210 | - * |
|
211 | - * @throws NotFoundException |
|
212 | - * @throws MethodNotAllowedException |
|
213 | - */ |
|
214 | - |
|
215 | - protected function matchSimilarRoute($httpMethod, $path) |
|
216 | - { |
|
217 | - $dm = []; |
|
218 | - |
|
219 | - if (($sm = $this->checkStaticRouteInOtherMethods($httpMethod, $path)) |
|
220 | - || ($dm = $this->checkDynamicRouteInOtherMethods($httpMethod, $path))) { |
|
221 | - throw new MethodNotAllowedException($httpMethod, $path, array_merge((array) $sm, (array) $dm)); |
|
222 | - } |
|
223 | - |
|
224 | - throw new NotFoundException; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Verify if a static route match in another method than the requested. |
|
229 | - * |
|
230 | - * @param string $targetHttpMethod The HTTP method that must not be checked |
|
231 | - * @param string $path The Path that must be matched. |
|
232 | - * |
|
233 | - * @return array |
|
234 | - */ |
|
235 | - |
|
236 | - protected function checkStaticRouteInOtherMethods($targetHttpMethod, $path) |
|
237 | - { |
|
238 | - return array_filter($this->getHttpMethodsBut($targetHttpMethod), function ($httpMethod) use ($path) { |
|
239 | - return (bool) $this->collector->findStaticRoute($httpMethod, $path); |
|
240 | - }); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Verify if a dynamic route match in another method than the requested. |
|
245 | - * |
|
246 | - * @param string $targetHttpMethod The HTTP method that must not be checked |
|
247 | - * @param string $path The Path that must be matched. |
|
248 | - * |
|
249 | - * @return array |
|
250 | - */ |
|
251 | - |
|
252 | - protected function checkDynamicRouteInOtherMethods($targetHttpMethod, $path) |
|
253 | - { |
|
254 | - return array_filter($this->getHttpMethodsBut($targetHttpMethod), function ($httpMethod) use ($path) { |
|
255 | - return (bool) $this->matchDynamicRoute($httpMethod, $path); |
|
256 | - }); |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * Strip the given http methods and return all the others. |
|
261 | - * |
|
262 | - * @param string|string[] |
|
263 | - * @return array |
|
264 | - */ |
|
265 | - |
|
266 | - protected function getHttpMethodsBut($targetHttpMethod) |
|
267 | - { |
|
268 | - return array_diff(explode(" ", Collector::HTTP_METHODS), (array) $targetHttpMethod); |
|
269 | - } |
|
270 | - |
|
271 | - /** |
|
272 | - * @return Collector |
|
273 | - */ |
|
274 | - |
|
275 | - public function getCollector() |
|
276 | - { |
|
277 | - return $this->collector; |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * @return string |
|
282 | - */ |
|
283 | - |
|
284 | - public function getBasePath() |
|
285 | - { |
|
286 | - return $this->basepath; |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Set a new basepath, this will be a prefix that must be excluded in |
|
291 | - * every requested Path. |
|
292 | - * |
|
293 | - * @param string $basepath The new basepath |
|
294 | - */ |
|
26 | + /** |
|
27 | + * @var Collector |
|
28 | + */ |
|
29 | + |
|
30 | + protected $collector; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var Parser $parser |
|
34 | + */ |
|
35 | + |
|
36 | + protected $parser; |
|
37 | + |
|
38 | + /** |
|
39 | + * Define a basepath to all routes. |
|
40 | + * |
|
41 | + * @var string |
|
42 | + */ |
|
43 | + |
|
44 | + protected $basepath = ""; |
|
45 | + |
|
46 | + /** |
|
47 | + * Construct the route dispatcher. |
|
48 | + * |
|
49 | + * @param Collector $collector |
|
50 | + * @param string $basepath Define a Path prefix that must be excluded on matches. |
|
51 | + */ |
|
52 | + |
|
53 | + public function __construct(Collector $collector, $basepath = "") |
|
54 | + { |
|
55 | + $this->collector = $collector; |
|
56 | + $this->basepath = $basepath; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Find a route that matches the given arguments. |
|
61 | + * |
|
62 | + * @param string $httpMethod |
|
63 | + * @param string $path |
|
64 | + * |
|
65 | + * @throws NotFoundException |
|
66 | + * @throws MethodNotAllowedException |
|
67 | + * |
|
68 | + * @return Route |
|
69 | + */ |
|
70 | + |
|
71 | + public function match($httpMethod, $path) |
|
72 | + { |
|
73 | + $path = $this->parsePath($path); |
|
74 | + |
|
75 | + if (($route = $this->collector->findStaticRoute($httpMethod, $path)) || |
|
76 | + ($route = $this->matchDynamicRoute($httpMethod, $path))) { |
|
77 | + $route->setMatcher($this); |
|
78 | + |
|
79 | + return $route; |
|
80 | + } |
|
81 | + |
|
82 | + $this->matchSimilarRoute($httpMethod, $path); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Find and return the request dynamic route based on the compiled data and Path. |
|
87 | + * |
|
88 | + * @param string $httpMethod |
|
89 | + * @param string $path |
|
90 | + * |
|
91 | + * @return Route|false If the request match an array with the action and parameters will |
|
92 | + * be returned otherwise a false will. |
|
93 | + */ |
|
94 | + |
|
95 | + protected function matchDynamicRoute($httpMethod, $path) |
|
96 | + { |
|
97 | + if ($routes = $this->collector->findDynamicRoutes($httpMethod, $path)) { |
|
98 | + // cache the parser reference |
|
99 | + $this->parser = $this->collector->getParser(); |
|
100 | + // chunk routes for smaller regex groups using the Sturges' Formula |
|
101 | + foreach (array_chunk($routes, round(1 + 3.3 * log(count($routes))), true) as $chunk) { |
|
102 | + array_map([$this, "buildRoute"], $chunk); |
|
103 | + list($pattern, $map) = $this->buildGroup($chunk); |
|
104 | + |
|
105 | + if (!preg_match($pattern, $path, $matches)) { |
|
106 | + continue; |
|
107 | + } |
|
108 | + |
|
109 | + /** @var Route $route */ |
|
110 | + $route = $map[count($matches)]; |
|
111 | + unset($matches[0]); |
|
112 | + |
|
113 | + $route->setParams(array_combine($route->getParams(), array_filter($matches))); |
|
114 | + |
|
115 | + return $route; |
|
116 | + } |
|
117 | + } |
|
118 | + |
|
119 | + return false; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Parse the dynamic segments of the pattern and replace then for |
|
124 | + * corresponding regex. |
|
125 | + * |
|
126 | + * @param Route $route |
|
127 | + * @return Route |
|
128 | + */ |
|
129 | + |
|
130 | + protected function buildRoute(Route $route) |
|
131 | + { |
|
132 | + if ($route->getBlock()) { |
|
133 | + return $route; |
|
134 | + } |
|
135 | + |
|
136 | + list($pattern, $params) = $this->parsePlaceholders($route->getPattern()); |
|
137 | + return $route->setPatternWithoutReset($pattern)->setParams($params)->setBlock(true); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Group several dynamic routes patterns into one big regex and maps |
|
142 | + * the routes to the pattern positions in the big regex. |
|
143 | + * |
|
144 | + * @param Route[] $routes |
|
145 | + * @return array |
|
146 | + */ |
|
147 | + |
|
148 | + protected function buildGroup(array $routes) |
|
149 | + { |
|
150 | + $groupCount = (int) $map = $regex = []; |
|
151 | + |
|
152 | + foreach ($routes as $route) { |
|
153 | + $params = $route->getParams(); |
|
154 | + $paramsCount = count($params); |
|
155 | + $groupCount = max($groupCount, $paramsCount) + 1; |
|
156 | + $regex[] = $route->getPattern() . str_repeat("()", $groupCount - $paramsCount - 1); |
|
157 | + $map[$groupCount] = $route; |
|
158 | + } |
|
159 | + |
|
160 | + return ["~^(?|" . implode("|", $regex) . ")$~", $map]; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Parse an route pattern seeking for parameters and build the route regex. |
|
165 | + * |
|
166 | + * @param string $pattern |
|
167 | + * @return array 0 => new route regex, 1 => map of parameter names |
|
168 | + */ |
|
169 | + |
|
170 | + protected function parsePlaceholders($pattern) |
|
171 | + { |
|
172 | + $params = []; |
|
173 | + $parser = $this->parser; |
|
174 | + |
|
175 | + preg_match_all("~" . $parser::DYNAMIC_REGEX . "~x", $pattern, $matches, PREG_SET_ORDER); |
|
176 | + |
|
177 | + foreach ((array) $matches as $key => $match) { |
|
178 | + $pattern = str_replace($match[0], isset($match[2]) ? "({$match[2]})" : "([^/]+)", $pattern); |
|
179 | + $params[$key] = $match[1]; |
|
180 | + } |
|
181 | + |
|
182 | + return [$pattern, $params]; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * Get only the path of a given url. |
|
187 | + * |
|
188 | + * @param string $path The given URL |
|
189 | + * |
|
190 | + * @throws Exception |
|
191 | + * @return string |
|
192 | + */ |
|
193 | + |
|
194 | + protected function parsePath($path) |
|
195 | + { |
|
196 | + $path = parse_url(substr(strstr(";" . $path, ";" . $this->basepath), strlen(";" . $this->basepath)), PHP_URL_PATH); |
|
197 | + |
|
198 | + if ($path === false) { |
|
199 | + throw new Exception("Seriously malformed URL passed to route matcher."); |
|
200 | + } |
|
201 | + |
|
202 | + return $path; |
|
203 | + } |
|
204 | + |
|
205 | + /** |
|
206 | + * Generate an HTTP error request with method not allowed or not found. |
|
207 | + * |
|
208 | + * @param string $httpMethod |
|
209 | + * @param string $path |
|
210 | + * |
|
211 | + * @throws NotFoundException |
|
212 | + * @throws MethodNotAllowedException |
|
213 | + */ |
|
214 | + |
|
215 | + protected function matchSimilarRoute($httpMethod, $path) |
|
216 | + { |
|
217 | + $dm = []; |
|
218 | + |
|
219 | + if (($sm = $this->checkStaticRouteInOtherMethods($httpMethod, $path)) |
|
220 | + || ($dm = $this->checkDynamicRouteInOtherMethods($httpMethod, $path))) { |
|
221 | + throw new MethodNotAllowedException($httpMethod, $path, array_merge((array) $sm, (array) $dm)); |
|
222 | + } |
|
223 | + |
|
224 | + throw new NotFoundException; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Verify if a static route match in another method than the requested. |
|
229 | + * |
|
230 | + * @param string $targetHttpMethod The HTTP method that must not be checked |
|
231 | + * @param string $path The Path that must be matched. |
|
232 | + * |
|
233 | + * @return array |
|
234 | + */ |
|
235 | + |
|
236 | + protected function checkStaticRouteInOtherMethods($targetHttpMethod, $path) |
|
237 | + { |
|
238 | + return array_filter($this->getHttpMethodsBut($targetHttpMethod), function ($httpMethod) use ($path) { |
|
239 | + return (bool) $this->collector->findStaticRoute($httpMethod, $path); |
|
240 | + }); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Verify if a dynamic route match in another method than the requested. |
|
245 | + * |
|
246 | + * @param string $targetHttpMethod The HTTP method that must not be checked |
|
247 | + * @param string $path The Path that must be matched. |
|
248 | + * |
|
249 | + * @return array |
|
250 | + */ |
|
251 | + |
|
252 | + protected function checkDynamicRouteInOtherMethods($targetHttpMethod, $path) |
|
253 | + { |
|
254 | + return array_filter($this->getHttpMethodsBut($targetHttpMethod), function ($httpMethod) use ($path) { |
|
255 | + return (bool) $this->matchDynamicRoute($httpMethod, $path); |
|
256 | + }); |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * Strip the given http methods and return all the others. |
|
261 | + * |
|
262 | + * @param string|string[] |
|
263 | + * @return array |
|
264 | + */ |
|
265 | + |
|
266 | + protected function getHttpMethodsBut($targetHttpMethod) |
|
267 | + { |
|
268 | + return array_diff(explode(" ", Collector::HTTP_METHODS), (array) $targetHttpMethod); |
|
269 | + } |
|
270 | + |
|
271 | + /** |
|
272 | + * @return Collector |
|
273 | + */ |
|
274 | + |
|
275 | + public function getCollector() |
|
276 | + { |
|
277 | + return $this->collector; |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * @return string |
|
282 | + */ |
|
283 | + |
|
284 | + public function getBasePath() |
|
285 | + { |
|
286 | + return $this->basepath; |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Set a new basepath, this will be a prefix that must be excluded in |
|
291 | + * every requested Path. |
|
292 | + * |
|
293 | + * @param string $basepath The new basepath |
|
294 | + */ |
|
295 | 295 | |
296 | - public function setBasePath($basepath) |
|
297 | - { |
|
298 | - $this->basepath = $basepath; |
|
299 | - } |
|
296 | + public function setBasePath($basepath) |
|
297 | + { |
|
298 | + $this->basepath = $basepath; |
|
299 | + } |
|
300 | 300 | |
301 | 301 | } |
@@ -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; |
@@ -20,158 +20,158 @@ |
||
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|string[] $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|string[] $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 string[] $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 | - foreach ($this->routes as $route) { |
|
87 | - $action = $route->getAction()[1]; |
|
88 | - |
|
89 | - if ($action === "make" && isset($translations["make"])) { |
|
90 | - $route->setPatternWithoutReset(str_replace("make", $translations["make"], $route->getPattern())); |
|
91 | - } elseif ($action === "edit" && isset($translations["edit"])) { |
|
92 | - $route->setPatternWithoutReset(str_replace("edit", $translations["edit"], $route->getPattern())); |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - return $this; |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Add a route or a group of routes to the resource, it means that |
|
101 | - * every added route will now receive the parameters of the resource, like id. |
|
102 | - * |
|
103 | - * @param Route|Group $route |
|
104 | - * @return self |
|
105 | - */ |
|
106 | - |
|
107 | - public function member($route) |
|
108 | - { |
|
109 | - $resource = new self; |
|
110 | - $resource->set($route); |
|
111 | - $this->nest($resource); |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * Nested routes capture the relation between a resource and another resource. |
|
116 | - * |
|
117 | - * @param self $resource |
|
118 | - * @return self |
|
119 | - */ |
|
120 | - |
|
121 | - public function nest(self $resource) |
|
122 | - { |
|
123 | - foreach ($this->routes as $route) { |
|
124 | - if ($route->getAction()[1] === "show") { |
|
125 | - $this->set($resource->forget()->setPrefix($this->getNestedPrefix($route->getPattern()))); break; |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - return $this; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Nest resources but with only build routes with the minimal amount of information |
|
134 | - * to uniquely identify the resource. |
|
135 | - * |
|
136 | - * @param self $resource |
|
137 | - * @return self |
|
138 | - */ |
|
139 | - |
|
140 | - public function shallow(self $resource) |
|
141 | - { |
|
142 | - $newResource = new self; |
|
143 | - $resource->forget(); |
|
144 | - $routes = $resource->all(); |
|
145 | - |
|
146 | - foreach ($routes as $route) { |
|
147 | - if (strpos("index make create", $route->getAction()[1]) !== false) { |
|
148 | - $newResource->set($route); |
|
149 | - } |
|
150 | - } |
|
151 | - |
|
152 | - return $this->nest($newResource); |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Resolve the nesting pattern, setting the prefixes based on |
|
157 | - * parent resources patterns. |
|
158 | - * |
|
159 | - * @param string $pattern |
|
160 | - * @return string |
|
161 | - */ |
|
162 | - |
|
163 | - protected function getNestedPrefix($pattern) |
|
164 | - { |
|
165 | - $segments = explode("/", $pattern); |
|
166 | - $pattern = ""; |
|
167 | - |
|
168 | - foreach ($segments as $index => $segment) { |
|
169 | - if (strpos($segment, "{") === 0) { |
|
170 | - $pattern .= "/{" . $segments[$index - 1] . "_" . ltrim($segment, "{"); |
|
171 | - } else $pattern .= $segment; |
|
172 | - } |
|
173 | - |
|
174 | - return $pattern; |
|
175 | - } |
|
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|string[] $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|string[] $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 string[] $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 | + foreach ($this->routes as $route) { |
|
87 | + $action = $route->getAction()[1]; |
|
88 | + |
|
89 | + if ($action === "make" && isset($translations["make"])) { |
|
90 | + $route->setPatternWithoutReset(str_replace("make", $translations["make"], $route->getPattern())); |
|
91 | + } elseif ($action === "edit" && isset($translations["edit"])) { |
|
92 | + $route->setPatternWithoutReset(str_replace("edit", $translations["edit"], $route->getPattern())); |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + return $this; |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Add a route or a group of routes to the resource, it means that |
|
101 | + * every added route will now receive the parameters of the resource, like id. |
|
102 | + * |
|
103 | + * @param Route|Group $route |
|
104 | + * @return self |
|
105 | + */ |
|
106 | + |
|
107 | + public function member($route) |
|
108 | + { |
|
109 | + $resource = new self; |
|
110 | + $resource->set($route); |
|
111 | + $this->nest($resource); |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * Nested routes capture the relation between a resource and another resource. |
|
116 | + * |
|
117 | + * @param self $resource |
|
118 | + * @return self |
|
119 | + */ |
|
120 | + |
|
121 | + public function nest(self $resource) |
|
122 | + { |
|
123 | + foreach ($this->routes as $route) { |
|
124 | + if ($route->getAction()[1] === "show") { |
|
125 | + $this->set($resource->forget()->setPrefix($this->getNestedPrefix($route->getPattern()))); break; |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + return $this; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Nest resources but with only build routes with the minimal amount of information |
|
134 | + * to uniquely identify the resource. |
|
135 | + * |
|
136 | + * @param self $resource |
|
137 | + * @return self |
|
138 | + */ |
|
139 | + |
|
140 | + public function shallow(self $resource) |
|
141 | + { |
|
142 | + $newResource = new self; |
|
143 | + $resource->forget(); |
|
144 | + $routes = $resource->all(); |
|
145 | + |
|
146 | + foreach ($routes as $route) { |
|
147 | + if (strpos("index make create", $route->getAction()[1]) !== false) { |
|
148 | + $newResource->set($route); |
|
149 | + } |
|
150 | + } |
|
151 | + |
|
152 | + return $this->nest($newResource); |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Resolve the nesting pattern, setting the prefixes based on |
|
157 | + * parent resources patterns. |
|
158 | + * |
|
159 | + * @param string $pattern |
|
160 | + * @return string |
|
161 | + */ |
|
162 | + |
|
163 | + protected function getNestedPrefix($pattern) |
|
164 | + { |
|
165 | + $segments = explode("/", $pattern); |
|
166 | + $pattern = ""; |
|
167 | + |
|
168 | + foreach ($segments as $index => $segment) { |
|
169 | + if (strpos($segment, "{") === 0) { |
|
170 | + $pattern .= "/{" . $segments[$index - 1] . "_" . ltrim($segment, "{"); |
|
171 | + } else $pattern .= $segment; |
|
172 | + } |
|
173 | + |
|
174 | + return $pattern; |
|
175 | + } |
|
176 | 176 | |
177 | 177 | } |
@@ -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 | } |
@@ -69,8 +69,9 @@ discard block |
||
69 | 69 | public function controllers(array $controllers) |
70 | 70 | { |
71 | 71 | $group = new Group; |
72 | - foreach ($controllers as $controller) |
|
73 | - $group->set($this->controller($controller)); |
|
72 | + foreach ($controllers as $controller) { |
|
73 | + $group->set($this->controller($controller)); |
|
74 | + } |
|
74 | 75 | return $group; |
75 | 76 | } |
76 | 77 | |
@@ -100,8 +101,9 @@ discard block |
||
100 | 101 | public function controllersWithoutPrefix(array $controllers) |
101 | 102 | { |
102 | 103 | $group = new Group; |
103 | - foreach ($controllers as $controller) |
|
104 | - $group->set($this->controllerWithoutPrefix($controller)); |
|
104 | + foreach ($controllers as $controller) { |
|
105 | + $group->set($this->controllerWithoutPrefix($controller)); |
|
106 | + } |
|
105 | 107 | return $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->set($route); |
140 | 144 | } |
@@ -26,235 +26,235 @@ |
||
26 | 26 | trait ControllerCollectorTrait |
27 | 27 | { |
28 | 28 | |
29 | - /** |
|
30 | - * @return Parser |
|
31 | - */ |
|
32 | - |
|
33 | - abstract public function getParser(); |
|
34 | - |
|
35 | - /** |
|
36 | - * @param string $method |
|
37 | - * @param string $pattern |
|
38 | - * @param callable $action |
|
39 | - * |
|
40 | - * @return Group |
|
41 | - */ |
|
42 | - |
|
43 | - abstract public function set($method, $pattern, $action); |
|
44 | - |
|
45 | - /** |
|
46 | - * Define how controller actions names will be joined to form the route pattern. |
|
47 | - * |
|
48 | - * @var string |
|
49 | - */ |
|
50 | - |
|
51 | - protected $controllerActionJoin = "/"; |
|
52 | - |
|
53 | - /** |
|
54 | - * Maps all the controller methods that begins with a HTTP method, and maps the rest of |
|
55 | - * name as a path. The path will be the method name with slashes before every camelcased |
|
56 | - * word and without the HTTP method prefix, and the controller name will be used to prefix |
|
57 | - * the route pattern. e.g. ArticlesController::getCreate will generate a route to: GET articles/create |
|
58 | - * |
|
59 | - * @param string $controller The controller name |
|
60 | - * @param string $prefix |
|
61 | - * |
|
62 | - * @throws \ReflectionException |
|
63 | - * @return Group |
|
64 | - */ |
|
65 | - |
|
66 | - public function controller($controller, $prefix = null) |
|
67 | - { |
|
68 | - $controller = new ReflectionClass($controller); |
|
69 | - $prefix = $prefix === null ? $this->getControllerPrefix($controller) : $prefix; |
|
70 | - $methods = $controller->getMethods(ReflectionMethod::IS_PUBLIC); |
|
71 | - return $this->collectControllerRoutes($controller, $methods, "/$prefix/"); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Maps several controllers at same time. |
|
76 | - * |
|
77 | - * @param string[] $controllers Controllers name. |
|
78 | - * @throws \ReflectionException |
|
79 | - * @return Group |
|
80 | - */ |
|
81 | - |
|
82 | - public function controllers(array $controllers) |
|
83 | - { |
|
84 | - $group = new Group; |
|
85 | - foreach ($controllers as $controller) |
|
86 | - $group->set($this->controller($controller)); |
|
87 | - return $group; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Alias for Collector::controller but maps a controller without using the controller name as prefix. |
|
92 | - * |
|
93 | - * @param string $controller The controller name |
|
94 | - * @throws \ReflectionException |
|
95 | - * @return Group |
|
96 | - */ |
|
97 | - |
|
98 | - public function controllerWithoutPrefix($controller) |
|
99 | - { |
|
100 | - $controller = new ReflectionClass($controller); |
|
101 | - $methods = $controller->getMethods(ReflectionMethod::IS_PUBLIC); |
|
102 | - return $this->collectControllerRoutes($controller, $methods, "/"); |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Alias for Collector::controllers but maps a controller without using the controller name as prefix. |
|
107 | - * |
|
108 | - * @param string[] $controllers |
|
109 | - * @throws \ReflectionException |
|
110 | - * @return Group |
|
111 | - */ |
|
112 | - |
|
113 | - public function controllersWithoutPrefix(array $controllers) |
|
114 | - { |
|
115 | - $group = new Group; |
|
116 | - foreach ($controllers as $controller) |
|
117 | - $group->set($this->controllerWithoutPrefix($controller)); |
|
118 | - return $group; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @param ReflectionClass $controller |
|
123 | - * @param ReflectionMethod[] $methods |
|
124 | - * @param string $prefix |
|
125 | - * |
|
126 | - * @return Group |
|
127 | - */ |
|
128 | - |
|
129 | - protected function collectControllerRoutes(ReflectionClass $controller, array $methods, $prefix) |
|
130 | - { |
|
131 | - $group = new Group; |
|
132 | - $controllerDefaultStrategy = $this->getAnnotatedStrategy($controller); |
|
133 | - |
|
134 | - foreach ($methods as $method) { |
|
135 | - $name = preg_split("~(?=[A-Z])~", $method->name); |
|
136 | - $http = $name[0]; |
|
137 | - unset($name[0]); |
|
29 | + /** |
|
30 | + * @return Parser |
|
31 | + */ |
|
32 | + |
|
33 | + abstract public function getParser(); |
|
34 | + |
|
35 | + /** |
|
36 | + * @param string $method |
|
37 | + * @param string $pattern |
|
38 | + * @param callable $action |
|
39 | + * |
|
40 | + * @return Group |
|
41 | + */ |
|
42 | + |
|
43 | + abstract public function set($method, $pattern, $action); |
|
44 | + |
|
45 | + /** |
|
46 | + * Define how controller actions names will be joined to form the route pattern. |
|
47 | + * |
|
48 | + * @var string |
|
49 | + */ |
|
50 | + |
|
51 | + protected $controllerActionJoin = "/"; |
|
52 | + |
|
53 | + /** |
|
54 | + * Maps all the controller methods that begins with a HTTP method, and maps the rest of |
|
55 | + * name as a path. The path will be the method name with slashes before every camelcased |
|
56 | + * word and without the HTTP method prefix, and the controller name will be used to prefix |
|
57 | + * the route pattern. e.g. ArticlesController::getCreate will generate a route to: GET articles/create |
|
58 | + * |
|
59 | + * @param string $controller The controller name |
|
60 | + * @param string $prefix |
|
61 | + * |
|
62 | + * @throws \ReflectionException |
|
63 | + * @return Group |
|
64 | + */ |
|
65 | + |
|
66 | + public function controller($controller, $prefix = null) |
|
67 | + { |
|
68 | + $controller = new ReflectionClass($controller); |
|
69 | + $prefix = $prefix === null ? $this->getControllerPrefix($controller) : $prefix; |
|
70 | + $methods = $controller->getMethods(ReflectionMethod::IS_PUBLIC); |
|
71 | + return $this->collectControllerRoutes($controller, $methods, "/$prefix/"); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Maps several controllers at same time. |
|
76 | + * |
|
77 | + * @param string[] $controllers Controllers name. |
|
78 | + * @throws \ReflectionException |
|
79 | + * @return Group |
|
80 | + */ |
|
81 | + |
|
82 | + public function controllers(array $controllers) |
|
83 | + { |
|
84 | + $group = new Group; |
|
85 | + foreach ($controllers as $controller) |
|
86 | + $group->set($this->controller($controller)); |
|
87 | + return $group; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Alias for Collector::controller but maps a controller without using the controller name as prefix. |
|
92 | + * |
|
93 | + * @param string $controller The controller name |
|
94 | + * @throws \ReflectionException |
|
95 | + * @return Group |
|
96 | + */ |
|
97 | + |
|
98 | + public function controllerWithoutPrefix($controller) |
|
99 | + { |
|
100 | + $controller = new ReflectionClass($controller); |
|
101 | + $methods = $controller->getMethods(ReflectionMethod::IS_PUBLIC); |
|
102 | + return $this->collectControllerRoutes($controller, $methods, "/"); |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Alias for Collector::controllers but maps a controller without using the controller name as prefix. |
|
107 | + * |
|
108 | + * @param string[] $controllers |
|
109 | + * @throws \ReflectionException |
|
110 | + * @return Group |
|
111 | + */ |
|
112 | + |
|
113 | + public function controllersWithoutPrefix(array $controllers) |
|
114 | + { |
|
115 | + $group = new Group; |
|
116 | + foreach ($controllers as $controller) |
|
117 | + $group->set($this->controllerWithoutPrefix($controller)); |
|
118 | + return $group; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @param ReflectionClass $controller |
|
123 | + * @param ReflectionMethod[] $methods |
|
124 | + * @param string $prefix |
|
125 | + * |
|
126 | + * @return Group |
|
127 | + */ |
|
128 | + |
|
129 | + protected function collectControllerRoutes(ReflectionClass $controller, array $methods, $prefix) |
|
130 | + { |
|
131 | + $group = new Group; |
|
132 | + $controllerDefaultStrategy = $this->getAnnotatedStrategy($controller); |
|
133 | + |
|
134 | + foreach ($methods as $method) { |
|
135 | + $name = preg_split("~(?=[A-Z])~", $method->name); |
|
136 | + $http = $name[0]; |
|
137 | + unset($name[0]); |
|
138 | 138 | |
139 | - if (strpos(Collector::HTTP_METHODS, $http) !== false) { |
|
140 | - $action = $prefix . strtolower(implode($this->controllerActionJoin, $name)); |
|
141 | - $dynamic = $this->getMethodConstraints($method); |
|
142 | - $strategy = $this->getAnnotatedStrategy($method); |
|
143 | - |
|
144 | - $route = $this->set($http, "$action$dynamic", [$controller->name, $method->name]); |
|
145 | - |
|
146 | - if ($strategy !== null) { |
|
147 | - $route->setStrategy($strategy); |
|
148 | - } else $route->setStrategy($controllerDefaultStrategy); |
|
149 | - |
|
150 | - $group->set($route); |
|
151 | - } |
|
152 | - } |
|
153 | - |
|
154 | - return $group; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @param ReflectionClass $controller |
|
159 | - * |
|
160 | - * @return string |
|
161 | - */ |
|
162 | - |
|
163 | - protected function getControllerPrefix(ReflectionClass $controller) |
|
164 | - { |
|
165 | - preg_match("~\@prefix\s([a-zA-Z\\\_]+)~i", (string) $controller->getDocComment(), $prefix); |
|
166 | - return isset($prefix[1]) ? $prefix[1] : str_replace("controller", "", strtolower($controller->getShortName())); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @param \ReflectionMethod |
|
171 | - * @return string |
|
172 | - */ |
|
173 | - |
|
174 | - protected function getMethodConstraints(ReflectionMethod $method) |
|
175 | - { |
|
176 | - $beginPath = ""; |
|
177 | - $endPath = ""; |
|
178 | - |
|
179 | - if ($parameters = $method->getParameters()) { |
|
180 | - $types = $this->getParamsConstraint($method); |
|
181 | - |
|
182 | - foreach ($parameters as $parameter) { |
|
183 | - if ($parameter->isOptional()) { |
|
184 | - $beginPath .= "["; |
|
185 | - $endPath .= "]"; |
|
186 | - } |
|
187 | - |
|
188 | - $beginPath .= $this->getPathConstraint($parameter, $types); |
|
189 | - } |
|
190 | - } |
|
191 | - |
|
192 | - return $beginPath . $endPath; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * @param ReflectionParameter $parameter |
|
197 | - * @param string[] $types |
|
198 | - * @return string |
|
199 | - */ |
|
200 | - |
|
201 | - protected function getPathConstraint(ReflectionParameter $parameter, $types) |
|
202 | - { |
|
203 | - $name = $parameter->name; |
|
204 | - $path = "/{" . $name; |
|
205 | - return isset($types[$name]) ? "$path:{$types[$name]}}" : "$path}"; |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * @param ReflectionMethod $method |
|
210 | - * @return string[] |
|
211 | - */ |
|
212 | - |
|
213 | - protected function getParamsConstraint(ReflectionMethod $method) |
|
214 | - { |
|
215 | - $params = []; |
|
216 | - preg_match_all("~\@param\s(" . implode("|", array_keys($this->getParser()->getWildcards())) . "|\(.+\))\s\\$([a-zA-Z0-1_]+)~i", |
|
217 | - $method->getDocComment(), $types, PREG_SET_ORDER); |
|
218 | - |
|
219 | - foreach ((array) $types as $type) { |
|
220 | - // if a pattern is defined on Match take it otherwise take the param type by PHPDoc. |
|
221 | - $params[$type[2]] = isset($type[4]) ? $type[4] : $type[1]; |
|
222 | - } |
|
223 | - |
|
224 | - return $params; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * @param ReflectionClass|ReflectionMethod $reflector |
|
229 | - * @return string|null |
|
230 | - */ |
|
231 | - |
|
232 | - protected function getAnnotatedStrategy($reflector) |
|
233 | - { |
|
234 | - preg_match("~\@strategy\s([a-zA-Z\\\_]+)~i", (string) $reflector->getDocComment(), $strategy); |
|
235 | - return isset($strategy[1]) ? $strategy[1] : null; |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Define how controller actions names will be joined to form the route pattern. |
|
240 | - * Defaults to "/" so actions like "getMyAction" will be "/my/action". If changed to |
|
241 | - * "-" the new pattern will be "/my-action". |
|
242 | - * |
|
243 | - * @param string $join |
|
244 | - */ |
|
245 | - |
|
246 | - public function setControllerActionJoin($join) |
|
247 | - { |
|
248 | - $this->controllerActionJoin = $join; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * @return string |
|
253 | - */ |
|
254 | - |
|
255 | - public function getControllerActionJoin() |
|
256 | - { |
|
257 | - return $this->controllerActionJoin; |
|
258 | - } |
|
139 | + if (strpos(Collector::HTTP_METHODS, $http) !== false) { |
|
140 | + $action = $prefix . strtolower(implode($this->controllerActionJoin, $name)); |
|
141 | + $dynamic = $this->getMethodConstraints($method); |
|
142 | + $strategy = $this->getAnnotatedStrategy($method); |
|
143 | + |
|
144 | + $route = $this->set($http, "$action$dynamic", [$controller->name, $method->name]); |
|
145 | + |
|
146 | + if ($strategy !== null) { |
|
147 | + $route->setStrategy($strategy); |
|
148 | + } else $route->setStrategy($controllerDefaultStrategy); |
|
149 | + |
|
150 | + $group->set($route); |
|
151 | + } |
|
152 | + } |
|
153 | + |
|
154 | + return $group; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @param ReflectionClass $controller |
|
159 | + * |
|
160 | + * @return string |
|
161 | + */ |
|
162 | + |
|
163 | + protected function getControllerPrefix(ReflectionClass $controller) |
|
164 | + { |
|
165 | + preg_match("~\@prefix\s([a-zA-Z\\\_]+)~i", (string) $controller->getDocComment(), $prefix); |
|
166 | + return isset($prefix[1]) ? $prefix[1] : str_replace("controller", "", strtolower($controller->getShortName())); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @param \ReflectionMethod |
|
171 | + * @return string |
|
172 | + */ |
|
173 | + |
|
174 | + protected function getMethodConstraints(ReflectionMethod $method) |
|
175 | + { |
|
176 | + $beginPath = ""; |
|
177 | + $endPath = ""; |
|
178 | + |
|
179 | + if ($parameters = $method->getParameters()) { |
|
180 | + $types = $this->getParamsConstraint($method); |
|
181 | + |
|
182 | + foreach ($parameters as $parameter) { |
|
183 | + if ($parameter->isOptional()) { |
|
184 | + $beginPath .= "["; |
|
185 | + $endPath .= "]"; |
|
186 | + } |
|
187 | + |
|
188 | + $beginPath .= $this->getPathConstraint($parameter, $types); |
|
189 | + } |
|
190 | + } |
|
191 | + |
|
192 | + return $beginPath . $endPath; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * @param ReflectionParameter $parameter |
|
197 | + * @param string[] $types |
|
198 | + * @return string |
|
199 | + */ |
|
200 | + |
|
201 | + protected function getPathConstraint(ReflectionParameter $parameter, $types) |
|
202 | + { |
|
203 | + $name = $parameter->name; |
|
204 | + $path = "/{" . $name; |
|
205 | + return isset($types[$name]) ? "$path:{$types[$name]}}" : "$path}"; |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * @param ReflectionMethod $method |
|
210 | + * @return string[] |
|
211 | + */ |
|
212 | + |
|
213 | + protected function getParamsConstraint(ReflectionMethod $method) |
|
214 | + { |
|
215 | + $params = []; |
|
216 | + preg_match_all("~\@param\s(" . implode("|", array_keys($this->getParser()->getWildcards())) . "|\(.+\))\s\\$([a-zA-Z0-1_]+)~i", |
|
217 | + $method->getDocComment(), $types, PREG_SET_ORDER); |
|
218 | + |
|
219 | + foreach ((array) $types as $type) { |
|
220 | + // if a pattern is defined on Match take it otherwise take the param type by PHPDoc. |
|
221 | + $params[$type[2]] = isset($type[4]) ? $type[4] : $type[1]; |
|
222 | + } |
|
223 | + |
|
224 | + return $params; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * @param ReflectionClass|ReflectionMethod $reflector |
|
229 | + * @return string|null |
|
230 | + */ |
|
231 | + |
|
232 | + protected function getAnnotatedStrategy($reflector) |
|
233 | + { |
|
234 | + preg_match("~\@strategy\s([a-zA-Z\\\_]+)~i", (string) $reflector->getDocComment(), $strategy); |
|
235 | + return isset($strategy[1]) ? $strategy[1] : null; |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Define how controller actions names will be joined to form the route pattern. |
|
240 | + * Defaults to "/" so actions like "getMyAction" will be "/my/action". If changed to |
|
241 | + * "-" the new pattern will be "/my-action". |
|
242 | + * |
|
243 | + * @param string $join |
|
244 | + */ |
|
245 | + |
|
246 | + public function setControllerActionJoin($join) |
|
247 | + { |
|
248 | + $this->controllerActionJoin = $join; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * @return string |
|
253 | + */ |
|
254 | + |
|
255 | + public function getControllerActionJoin() |
|
256 | + { |
|
257 | + return $this->controllerActionJoin; |
|
258 | + } |
|
259 | 259 | |
260 | 260 | } |
@@ -19,7 +19,7 @@ |
||
19 | 19 | class BadRequestException extends HttpExceptionAbstract |
20 | 20 | { |
21 | 21 | |
22 | - protected $code = 400; |
|
23 | - protected $message = "Bad Request"; |
|
22 | + protected $code = 400; |
|
23 | + protected $message = "Bad Request"; |
|
24 | 24 | |
25 | 25 | } |