1 | <?php |
||
23 | class Matcher |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var Collector |
||
28 | */ |
||
29 | |||
30 | protected $collector; |
||
31 | |||
32 | /** |
||
33 | * Define a basepath to all routes. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | |||
38 | protected $basepath = ""; |
||
39 | |||
40 | /** |
||
41 | * Construct the route dispatcher. |
||
42 | * |
||
43 | * @param Collector $collector |
||
44 | * @param string $basepath Define a Path prefix that must be excluded on matches. |
||
45 | */ |
||
46 | |||
47 | 51 | public function __construct(Collector $collector, $basepath = "") |
|
52 | |||
53 | /** |
||
54 | * Find a route that matches the given arguments. |
||
55 | * |
||
56 | * @param string $httpMethod |
||
57 | * @param string $path |
||
58 | * |
||
59 | * @throws NotFoundException |
||
60 | * @throws MethodNotAllowedException |
||
61 | * |
||
62 | * @return Route |
||
63 | */ |
||
64 | |||
65 | 46 | public function match($httpMethod, $path) |
|
79 | |||
80 | /** |
||
81 | * Find and return the request dynamic route based on the compiled data and Path. |
||
82 | * |
||
83 | * @param string $httpMethod |
||
84 | * @param string $path |
||
85 | * |
||
86 | * @return Route|false If the request match an array with the action and parameters will |
||
87 | * be returned otherwise a false will. |
||
88 | */ |
||
89 | |||
90 | 34 | protected function matchDynamicRoute($httpMethod, $path) |
|
115 | |||
116 | /** |
||
117 | * Parse the dynamic segments of the pattern and replace then for |
||
118 | * corresponding regex. |
||
119 | * |
||
120 | * @param Route $route |
||
121 | * @return Route |
||
122 | */ |
||
123 | |||
124 | 28 | protected function buildRoute(Route $route) |
|
133 | |||
134 | /** |
||
135 | * Group several dynamic routes patterns into one big regex and maps |
||
136 | * the routes to the pattern positions in the big regex. |
||
137 | * |
||
138 | * @param Route[] $routes |
||
139 | * @return array |
||
140 | */ |
||
141 | |||
142 | 28 | protected function buildGroup(array $routes) |
|
156 | |||
157 | /** |
||
158 | * Parse an route pattern seeking for parameters and build the route regex. |
||
159 | * |
||
160 | * @param string $pattern |
||
161 | * @return array 0 => new route regex, 1 => map of parameter names |
||
162 | */ |
||
163 | |||
164 | 28 | protected function parsePlaceholders($pattern) |
|
176 | |||
177 | /** |
||
178 | * Get only the path of a given url. |
||
179 | * |
||
180 | * @param string $path The given URL |
||
181 | * |
||
182 | * @throws Exception |
||
183 | * @return string |
||
184 | */ |
||
185 | |||
186 | 46 | protected function parsePath($path) |
|
196 | |||
197 | /** |
||
198 | * Generate an HTTP error request with method not allowed or not found. |
||
199 | * |
||
200 | * @param string $httpMethod |
||
201 | * @param string $path |
||
202 | * |
||
203 | * @throws NotFoundException |
||
204 | * @throws MethodNotAllowedException |
||
205 | */ |
||
206 | |||
207 | 14 | protected function matchSimilarRoute($httpMethod, $path) |
|
218 | |||
219 | /** |
||
220 | * Verify if a static route match in another method than the requested. |
||
221 | * |
||
222 | * @param string $targetHttpMethod The HTTP method that must not be checked |
||
223 | * @param string $path The Path that must be matched. |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | |||
228 | 14 | protected function checkStaticRouteInOtherMethods($targetHttpMethod, $path) |
|
234 | |||
235 | /** |
||
236 | * Verify if a dynamic route match in another method than the requested. |
||
237 | * |
||
238 | * @param string $targetHttpMethod The HTTP method that must not be checked |
||
239 | * @param string $path The Path that must be matched. |
||
240 | * |
||
241 | * @return array |
||
242 | */ |
||
243 | |||
244 | protected function checkDynamicRouteInOtherMethods($targetHttpMethod, $path) |
||
250 | |||
251 | /** |
||
252 | * Strip the given http methods and return all the others. |
||
253 | * |
||
254 | * @param string|string[] |
||
255 | * @return array |
||
256 | */ |
||
257 | |||
258 | 14 | protected function getHttpMethodsBut($targetHttpMethod) |
|
262 | |||
263 | /** |
||
264 | * @return Collector |
||
265 | */ |
||
266 | |||
267 | public function getCollector() |
||
271 | |||
272 | /** |
||
273 | * @return string |
||
274 | */ |
||
275 | |||
276 | 1 | public function getBasePath() |
|
280 | |||
281 | /** |
||
282 | * Set a new basepath, this will be a prefix that must be excluded in |
||
283 | * every requested Path. |
||
284 | * |
||
285 | * @param string $basepath The new basepath |
||
286 | */ |
||
287 | |||
288 | 1 | public function setBasePath($basepath) |
|
292 | |||
293 | } |
||
294 |