1 | <?php |
||
33 | class Collector |
||
34 | { |
||
35 | |||
36 | use Collectors\ControllerCollectorTrait; |
||
37 | use Collectors\ResourceCollectorTrait; |
||
38 | |||
39 | /** |
||
40 | * These regex define the structure of a dynamic segment in a pattern. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | |||
45 | const DYNAMIC_REGEX = "{\s*(\w*)\s*(?::\s*([^{}]*(?:{(?-1)}*)*))?\s*}"; |
||
46 | |||
47 | |||
48 | /** |
||
49 | * All the supported http methods separated by spaces. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | |||
54 | const HTTP_METHODS = "get post put patch delete"; |
||
55 | |||
56 | /** |
||
57 | * The static routes are simple stored in a multidimensional array, the first |
||
58 | * dimension is indexed by an http method and hold an array indexed with the patterns |
||
59 | * and holding the route. ex. [METHOD => [PATTERN => ROUTE]] |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | |||
64 | protected $statics = []; |
||
65 | |||
66 | /** |
||
67 | * The dynamic routes have parameters and are stored in a hashtable that every cell have |
||
68 | * an array with route patterns as indexes and routes as values. ex. [INDEX => [PATTERN => ROUTE]] |
||
69 | * |
||
70 | * @var array |
||
71 | */ |
||
72 | |||
73 | protected $dynamics = []; |
||
74 | |||
75 | /** |
||
76 | * Some regex wildcards for easily definition of dynamic routes. |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | |||
81 | protected $wildcards = [ |
||
82 | "uid" => "uid-[a-zA-Z0-9]", |
||
83 | "slug" => "[a-z0-9-]", |
||
84 | "string" => "\w", |
||
85 | "int" => "\d", |
||
86 | "integer" => "\d", |
||
87 | "float" => "[-+]?\d*?[.]?\d", |
||
88 | "double" => "[-+]?\d*?[.]?\d", |
||
89 | "hex" => "0[xX][0-9a-fA-F]", |
||
90 | "octal" => "0[1-7][0-7]", |
||
91 | "bool" => "1|0|true|false|yes|no", |
||
92 | "boolean" => "1|0|true|false|yes|no", |
||
93 | ]; |
||
94 | |||
95 | /** |
||
96 | * @param string $method |
||
97 | * @param string $pattern |
||
98 | * @param string|array|\Closure $action |
||
99 | * |
||
100 | * @throws BadRouteException |
||
101 | * @throws MethodNotSupportedException |
||
102 | * |
||
103 | * @return Group |
||
104 | */ |
||
105 | |||
106 | 28 | public function set($method, $pattern, $action) |
|
125 | |||
126 | public function get ($pattern, $action) { return $this->set("get" , $pattern, $action); } |
||
131 | |||
132 | /** |
||
133 | * Insert a route into several http methods. |
||
134 | * |
||
135 | * @param string[] $methods |
||
136 | * @param string $pattern |
||
137 | * @param Callable $action |
||
138 | * |
||
139 | * @return Group |
||
140 | */ |
||
141 | |||
142 | 3 | public function match(array $methods, $pattern, $action) |
|
149 | |||
150 | /** |
||
151 | * Insert a route into every http method supported. |
||
152 | * |
||
153 | * @param string $pattern |
||
154 | * @param Callable $action |
||
155 | * |
||
156 | * @return Group |
||
157 | */ |
||
158 | |||
159 | 1 | public function any($pattern, $action) |
|
163 | |||
164 | /** |
||
165 | * Insert a route into every http method supported but the given ones. |
||
166 | * |
||
167 | * @param string[] $methods |
||
168 | * @param string $pattern |
||
169 | * @param Callable $action |
||
170 | * |
||
171 | * @return Group |
||
172 | */ |
||
173 | |||
174 | 1 | public function except(array $methods, $pattern, $action) |
|
178 | |||
179 | /** |
||
180 | * Group all given routes. |
||
181 | * |
||
182 | * @param Route[] $routes |
||
183 | * @return Group |
||
184 | */ |
||
185 | |||
186 | 1 | public function group(array $routes) |
|
193 | |||
194 | /** |
||
195 | * Remove a route from collector. |
||
196 | * |
||
197 | * @param string $method |
||
198 | * @param string $pattern |
||
199 | */ |
||
200 | |||
201 | 5 | public function forget($method, $pattern) |
|
207 | |||
208 | /** |
||
209 | * Determine if the http method is valid. |
||
210 | * |
||
211 | * @param string $method |
||
212 | * @throws MethodNotSupportedException |
||
213 | * @return string |
||
214 | */ |
||
215 | |||
216 | 28 | protected function parseMethod($method) |
|
226 | |||
227 | /** |
||
228 | * Separate routes pattern with optional parts into n new patterns. |
||
229 | * |
||
230 | * @param string $pattern |
||
231 | * @return array |
||
232 | */ |
||
233 | |||
234 | 28 | protected function parsePattern($pattern) |
|
244 | |||
245 | /** |
||
246 | * Parse all the possible patterns seeking for an incorrect or incompatible pattern. |
||
247 | * |
||
248 | * @param string[] $segments Segments are all the possible patterns made on top of a pattern with optional segments. |
||
249 | * @param int $closingNumber The count of optional segments. |
||
250 | * @param string $withoutClosing The pattern without the closing token of an optional segment. aka: ] |
||
251 | * |
||
252 | * @throws BadRouteException |
||
253 | */ |
||
254 | |||
255 | 28 | protected function parseSegments(array $segments, $closingNumber, $withoutClosing) |
|
263 | |||
264 | /** |
||
265 | * @param string[] $segments |
||
266 | * |
||
267 | * @throws BadRouteException |
||
268 | * @return array |
||
269 | */ |
||
270 | |||
271 | 28 | protected function buildSegments(array $segments) |
|
293 | |||
294 | /** |
||
295 | * @param string $method |
||
296 | * @param string $pattern |
||
297 | * |
||
298 | * @return Route|false |
||
299 | */ |
||
300 | |||
301 | 27 | public function findStaticRoute($method, $pattern) |
|
308 | |||
309 | /** |
||
310 | * @param string $method |
||
311 | * @param string $pattern |
||
312 | * |
||
313 | * @return array|false |
||
314 | */ |
||
315 | |||
316 | 22 | public function findDynamicRoutes($method, $pattern) |
|
321 | |||
322 | /** |
||
323 | * @param string $method |
||
324 | * @param string $pattern |
||
325 | * |
||
326 | * @return int |
||
327 | */ |
||
328 | |||
329 | 22 | protected function getDynamicIndex($method, $pattern) |
|
333 | |||
334 | /** |
||
335 | * @return string[] |
||
336 | */ |
||
337 | |||
338 | 7 | public function getWildcards() |
|
342 | |||
343 | /** |
||
344 | * @param string $pattern |
||
345 | * @param string $wildcard |
||
346 | */ |
||
347 | |||
348 | public function setWildcard($pattern, $wildcard) |
||
352 | |||
353 | } |
||
354 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.