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 | 48 | 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 | 43 | 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 | 31 | 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 | 25 | 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 array $routes |
||
139 | * @return array |
||
140 | */ |
||
141 | |||
142 | 25 | protected function buildGroup(array $routes) |
|
157 | |||
158 | /** |
||
159 | * Parse an route pattern seeking for parameters and build the route regex. |
||
160 | * |
||
161 | * @param string $pattern |
||
162 | * @return array 0 => new route regex, 1 => map of parameter names |
||
163 | */ |
||
164 | |||
165 | 25 | protected function parsePlaceholders($pattern) |
|
177 | |||
178 | /** |
||
179 | * Get only the path of a given url. |
||
180 | * |
||
181 | * @param string $path The given URL |
||
182 | * |
||
183 | * @throws Exception |
||
184 | * @return string |
||
185 | */ |
||
186 | |||
187 | 43 | protected function parsePath($path) |
|
197 | |||
198 | /** |
||
199 | * Generate an HTTP error request with method not allowed or not found. |
||
200 | * |
||
201 | * @param string $httpMethod |
||
202 | * @param string $path |
||
203 | * |
||
204 | * @throws NotFoundException |
||
205 | * @throws MethodNotAllowedException |
||
206 | */ |
||
207 | |||
208 | 14 | protected function matchSimilarRoute($httpMethod, $path) |
|
219 | |||
220 | /** |
||
221 | * Verify if a static route match in another method than the requested. |
||
222 | * |
||
223 | * @param string $targetHttpMethod The HTTP method that must not be checked |
||
224 | * @param string $path The Path that must be matched. |
||
225 | * |
||
226 | * @return array |
||
227 | */ |
||
228 | |||
229 | 14 | protected function checkStaticRouteInOtherMethods($targetHttpMethod, $path) |
|
235 | |||
236 | /** |
||
237 | * Verify if a dynamic route match in another method than the requested. |
||
238 | * |
||
239 | * @param string $targetHttpMethod The HTTP method that must not be checked |
||
240 | * @param string $path The Path that must be matched. |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | |||
245 | protected function checkDynamicRouteInOtherMethods($targetHttpMethod, $path) |
||
251 | |||
252 | /** |
||
253 | * Strip the given http methods and return all the others. |
||
254 | * |
||
255 | * @param array|string |
||
256 | * @return array |
||
257 | */ |
||
258 | |||
259 | 14 | protected function getHttpMethodsBut($targetHttpMethod) |
|
263 | |||
264 | /** |
||
265 | * @return Collector |
||
266 | */ |
||
267 | |||
268 | public function getCollector() |
||
272 | |||
273 | /** |
||
274 | * @return string |
||
275 | */ |
||
276 | |||
277 | 1 | public function getBasePath() |
|
281 | |||
282 | /** |
||
283 | * Set a new basepath, this will be a prefix that must be excluded in |
||
284 | * every requested Path. |
||
285 | * |
||
286 | * @param string $basepath The new basepath |
||
287 | */ |
||
288 | |||
289 | 1 | public function setBasePath($basepath) |
|
293 | |||
294 | } |
||
295 |