1 | <?php |
||
26 | class Matcher |
||
27 | { |
||
28 | /** |
||
29 | * Regex for getting URL variables |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | const VARIABLE_REGEX = |
||
34 | "~\{ |
||
35 | \s* ([a-zA-Z0-9_]*) \s* |
||
36 | (?: |
||
37 | : \s* ([^{]+(?:\{.*?\})?) |
||
38 | )? |
||
39 | \}\??~x"; |
||
40 | |||
41 | /** |
||
42 | * Shortcuts for regex. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | private $regexShortcuts = [ |
||
47 | ':i}' => ':[0-9]+}', |
||
48 | ':a}' => ':[0-9A-Za-z]+}', |
||
49 | ':h}' => ':[0-9A-Fa-f]+}', |
||
50 | ':c}' => ':[a-zA-Z0-9+_\-\.]+}' |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * Flag for enabling support of mode rewrite. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | private $urlFormat = RouteMode::PATH_FORMAT; |
||
59 | |||
60 | /** |
||
61 | * Name of GET parameter from which the route will be get |
||
62 | * if url format is set to GET_FORMAT. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | private $modeRewriteParameter = 'r'; |
||
67 | |||
68 | /** |
||
69 | * Instance of route collection |
||
70 | * |
||
71 | * @var \Kambo\Router\Route\Collection |
||
72 | */ |
||
73 | private $routeCollection; |
||
74 | |||
75 | /** |
||
76 | * Instance of Dispatcher which will dispatch the request |
||
77 | * |
||
78 | * @var \Kambo\Router\Dispatchers\Interfaces\DispatcherInterface |
||
79 | */ |
||
80 | private $dispatcher; |
||
81 | |||
82 | /** |
||
83 | * Constructor |
||
84 | * |
||
85 | * @param \Kambo\Router\Route\Collection $routeCollection |
||
86 | * @param \Kambo\Router\Dispatchers\Interfaces\DispatcherInterface $dispatcher |
||
87 | * |
||
88 | */ |
||
89 | public function __construct(Collection $routeCollection, DispatcherInterface $dispatcher) |
||
94 | |||
95 | /** |
||
96 | * Match request with provided routes. |
||
97 | * Get method and url from provided request and start matching. |
||
98 | * |
||
99 | * @param object $request instance of PSR 7 compatible request object |
||
100 | * |
||
101 | * @return mixed |
||
102 | */ |
||
103 | public function match($request) |
||
110 | |||
111 | /** |
||
112 | * Match method and route with provided routes. |
||
113 | * If route and method match a route is dispatch using provided dispatcher. |
||
114 | * |
||
115 | * @param string $method http method |
||
116 | * @param string $url url |
||
117 | * |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public function matchRoute($method, $url) |
||
138 | |||
139 | /** |
||
140 | * Set format for URL resolving. |
||
141 | * If the path mode is set to a path a web server must be properly |
||
142 | * configurated, defualt value is PATH_FORMAT. |
||
143 | * |
||
144 | * @param string $urlFormat value from RouteMode enum |
||
145 | * |
||
146 | * @return self for fluent interface |
||
147 | */ |
||
148 | public function setUrlFormat($urlFormat) |
||
160 | |||
161 | /** |
||
162 | * Get format for URL resolving. |
||
163 | * |
||
164 | * @return string value from RouteMode enum |
||
165 | */ |
||
166 | public function getUrlFormat() |
||
170 | |||
171 | // ------------ PRIVATE METHODS |
||
172 | |||
173 | /** |
||
174 | * Match route with provideed regex. |
||
175 | * |
||
176 | * @param string $routeRegex |
||
177 | * @param string $route |
||
178 | * |
||
179 | * @return mixed |
||
180 | */ |
||
181 | private function routeMatch($routeRegex, $route) |
||
192 | |||
193 | /** |
||
194 | * Prepare regex and parameters for each of routes. |
||
195 | * |
||
196 | * @param array $routes array with instances of route object |
||
197 | * |
||
198 | * @return array transformed routes |
||
199 | */ |
||
200 | private function parseRoutes($routes) |
||
211 | |||
212 | /** |
||
213 | * Get route from request object. |
||
214 | * Method expect an instance of PSR 7 compatible request object. |
||
215 | * |
||
216 | * @param object $request |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | private function getUrl($request) |
||
236 | |||
237 | /** |
||
238 | * Prepare regex for resolving route a extract variables from route. |
||
239 | * |
||
240 | * @param string $route |
||
241 | * |
||
242 | * @return array regex and parameters |
||
243 | */ |
||
244 | private function transformRoute($route) |
||
266 | |||
267 | /** |
||
268 | * Extract variables from route |
||
269 | * |
||
270 | * @param string $route |
||
271 | * |
||
272 | * @return null|array |
||
273 | */ |
||
274 | private function extractVariableRouteParts($route) |
||
286 | } |
||
287 |