1 | <?php |
||
32 | class Regex implements Matcher |
||
33 | { |
||
34 | /** |
||
35 | * Regex for getting URL variables |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | const VARIABLE_REGEX = |
||
40 | "~\{ |
||
41 | \s* ([a-zA-Z0-9_]*) \s* |
||
42 | (?: |
||
43 | : \s* ([^{]+(?:\{.*?\})?) |
||
44 | )? |
||
45 | \}\??~x"; |
||
46 | |||
47 | /** |
||
48 | * Shortcuts for regex. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $regexShortcuts = [ |
||
53 | ':i}' => ':[0-9]+}', |
||
54 | ':a}' => ':[0-9A-Za-z]+}', |
||
55 | ':h}' => ':[0-9A-Fa-f]+}', |
||
56 | ':c}' => ':[a-zA-Z0-9+_\-\.]+}' |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * Flag for enabling support of mode rewrite. |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | private $urlFormat = RouteMode::PATH_FORMAT; |
||
65 | |||
66 | /** |
||
67 | * Name of GET parameter from which the route will be get |
||
68 | * if url format is set to GET_FORMAT. |
||
69 | * |
||
70 | * @var string |
||
71 | */ |
||
72 | private $modeRewriteParameter = 'r'; |
||
73 | |||
74 | /** |
||
75 | * Instance of route collection |
||
76 | * |
||
77 | * @var \Kambo\Router\Route\Collection |
||
78 | */ |
||
79 | private $routeCollection; |
||
80 | |||
81 | /** |
||
82 | * Constructor |
||
83 | * |
||
84 | * @param \Kambo\Router\Route\Collection $routeCollection |
||
85 | * |
||
86 | */ |
||
87 | public function __construct( |
||
92 | |||
93 | /** |
||
94 | * Match request with provided routes. |
||
95 | * Get method and url from provided request and start matching. |
||
96 | * |
||
97 | * @param ServerRequest $request instance of PSR 7 compatible request object |
||
98 | * |
||
99 | * @return mixed |
||
100 | */ |
||
101 | public function matchRequest($request) |
||
108 | |||
109 | /** |
||
110 | * Match url and method with provided routes. |
||
111 | * |
||
112 | * @param string $method http method |
||
113 | * @param string $url url |
||
114 | * |
||
115 | * @return mixed |
||
116 | */ |
||
117 | public function matchPathAndMethod($method, $url) |
||
121 | |||
122 | /** |
||
123 | * Set format for URL resolving. |
||
124 | * If the path mode is set to a path a web server must be properly |
||
125 | * configurated, defualt value is PATH_FORMAT. |
||
126 | * |
||
127 | * @param string $urlFormat value from RouteMode enum |
||
128 | * |
||
129 | * @return self for fluent interface |
||
130 | */ |
||
131 | public function setUrlFormat($urlFormat) |
||
143 | |||
144 | /** |
||
145 | * Get format for URL resolving. |
||
146 | * |
||
147 | * @return string value from RouteMode enum |
||
148 | */ |
||
149 | public function getUrlFormat() |
||
153 | |||
154 | // ------------ PRIVATE METHODS |
||
155 | |||
156 | /** |
||
157 | * Match method and route with provided routes. |
||
158 | * If route and method match a route is dispatch using provided dispatcher. |
||
159 | * |
||
160 | * @param string $method http method |
||
161 | * @param string $url url |
||
162 | * |
||
163 | * @return mixed |
||
164 | */ |
||
165 | private function getMatchRoute($method, $url) |
||
183 | |||
184 | /** |
||
185 | * Match route by provided regex. |
||
186 | * |
||
187 | * @param string $routeRegex |
||
188 | * @param string $route |
||
189 | * |
||
190 | * @return mixed |
||
191 | */ |
||
192 | private function routeMatch($routeRegex, $route) |
||
203 | |||
204 | /** |
||
205 | * Prepare regex and parameters for each of routes. |
||
206 | * |
||
207 | * @param array $routes array with instances of route object |
||
208 | * |
||
209 | * @return array transformed routes |
||
210 | */ |
||
211 | private function parseRoutes(Collection $routes) |
||
227 | |||
228 | /** |
||
229 | * Get route from request object. |
||
230 | * Method expect an instance of PSR 7 compatible request object. |
||
231 | * |
||
232 | * @param object $request |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | private function getUrl($request) |
||
252 | |||
253 | /** |
||
254 | * Prepare regex for resolving route a extract variables from route. |
||
255 | * |
||
256 | * @param string $route |
||
257 | * |
||
258 | * @return array regex and parameters |
||
259 | */ |
||
260 | private function transformRoute($route) |
||
286 | |||
287 | /** |
||
288 | * Extract variables from route |
||
289 | * |
||
290 | * @param string $route |
||
291 | * |
||
292 | * @return null|array |
||
293 | */ |
||
294 | private function extractVariableRouteParts($route) |
||
306 | } |
||
307 |