1 | <?php |
||
37 | class Collector |
||
38 | { |
||
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 | 57 | public function __construct(Parser $parser = null) |
|
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 | 57 | public function set($method, $pattern, $action) |
|
120 | |||
121 | public function get ($pattern, $action) { return $this->set("get" , $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 | 3 | public function match(array $methods, $pattern, $action) |
|
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 | 1 | public function any($pattern, $action) |
|
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 | 1 | public function except($methods, $pattern, $action) |
|
173 | |||
174 | /** |
||
175 | * Group all given routes. |
||
176 | * |
||
177 | * @param Route[] $routes |
||
178 | * @return Group |
||
179 | */ |
||
180 | |||
181 | 9 | public function group(array $routes) |
|
188 | |||
189 | /** |
||
190 | * Remove a route from collector. |
||
191 | * |
||
192 | * @param string $method |
||
193 | * @param string $pattern |
||
194 | */ |
||
195 | |||
196 | 6 | public function forget($method, $pattern) |
|
202 | |||
203 | /** |
||
204 | * @param string $method |
||
205 | * @param string $pattern |
||
206 | * |
||
207 | * @return Route|false |
||
208 | */ |
||
209 | |||
210 | 51 | public function findStaticRoute($method, $pattern) |
|
217 | |||
218 | /** |
||
219 | * @param string $method |
||
220 | * @param string $pattern |
||
221 | * |
||
222 | * @return array|false |
||
223 | */ |
||
224 | |||
225 | 38 | public function findDynamicRoutes($method, $pattern) |
|
230 | |||
231 | /** |
||
232 | * @param string $method |
||
233 | * @param string $pattern |
||
234 | * |
||
235 | * @return int |
||
236 | */ |
||
237 | |||
238 | 38 | protected function getDynamicIndex($method, $pattern) |
|
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 | 57 | protected function getValidMethod($method) |
|
262 | |||
263 | /** |
||
264 | * @return string[] |
||
265 | */ |
||
266 | |||
267 | public function getWildcards() |
||
271 | |||
272 | /** |
||
273 | * @return string[] |
||
274 | */ |
||
275 | |||
276 | public function getWildcardTokens() |
||
280 | |||
281 | /** |
||
282 | * @param string $wildcard |
||
283 | * @return string|null |
||
284 | */ |
||
285 | |||
286 | 1 | public function getWildcard($wildcard) |
|
287 | { |
||
288 | 1 | return $this->parser->getWildcard($wildcard); |
|
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param string $wildcard |
||
293 | * @param string $pattern |
||
294 | * |
||
295 | * @return self |
||
296 | */ |
||
297 | |||
298 | 1 | public function setWildcard($wildcard, $pattern) |
|
299 | { |
||
300 | 1 | $this->parser->setWildcard($wildcard, $pattern); |
|
301 | 1 | return $this; |
|
302 | } |
||
303 | |||
304 | /** |
||
305 | * @return Parser |
||
306 | */ |
||
307 | |||
308 | 33 | public function getParser() |
|
312 | |||
313 | /** |
||
314 | * @param Parser $parser |
||
315 | * |
||
316 | * @throws \LogicException |
||
317 | * @return self |
||
318 | */ |
||
319 | |||
320 | 1 | public function setParser(Parser $parser) |
|
329 | |||
330 | } |
||
331 |
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.