1 | <?php |
||
9 | class Router |
||
10 | { |
||
11 | private $trailing_slash_check = true; |
||
12 | private $routes = []; |
||
13 | private $route_names = []; |
||
14 | private $regexp_map = [ |
||
15 | '/\{([A-Za-z]\w*)\}/' => '(?<$1>[^/]+)', |
||
16 | '/\{([A-Za-z]\w*):word\}/' => '(?<$1>\w+)', |
||
17 | '/\{([A-Za-z]\w*):number\}/' => '(?<$1>\d+)', |
||
18 | '/\{([A-Za-z]\w*):slug\}/' => '(?<$1>[A-Za-z0-9_-]+)', |
||
19 | '/\{([A-Za-z]\w*):([^}]+)\}/' => '(?<$1>$2)', |
||
20 | '/\//' => '\/' |
||
21 | ]; |
||
22 | private static $parsed_regexp = []; // cache |
||
23 | |||
24 | /** |
||
25 | * Creates a new Router. |
||
26 | * |
||
27 | * @param Route[] $routes |
||
28 | * (Optional) Array of Route value objects to create. |
||
29 | * |
||
30 | * @throws \RunTimeException |
||
31 | * Thrown if array not contains Router instances. |
||
32 | */ |
||
33 | 20 | public function __construct(array $routes = []) |
|
43 | |||
44 | /** |
||
45 | * Registers a route. |
||
46 | * |
||
47 | * @param Route $route |
||
48 | * Route Value Object. |
||
49 | */ |
||
50 | 9 | public function add(Route $route) |
|
54 | |||
55 | /** |
||
56 | * Registers a route. |
||
57 | * |
||
58 | * @param string|array $methods |
||
59 | * Either a single HTTP verb, or an array of verbs. |
||
60 | * @param string $uri |
||
61 | * URI pattern to match for this route. |
||
62 | * @param string $handler |
||
63 | * ClassName::methodName to invoke for this route. If methodName |
||
64 | * is not present, a method of 'index' is assumed. |
||
65 | * @param string $name |
||
66 | * (Optional) An unique name for this route. |
||
67 | */ |
||
68 | 19 | public function addRoute($methods, $uri, $handler, $name = null) |
|
81 | |||
82 | /** |
||
83 | * Syntactic sugar: registers a HEAD route. |
||
84 | * |
||
85 | * @param string $uri |
||
86 | * @param string $handler |
||
87 | * @param string $name |
||
88 | * (Optional) An unique name for this route. |
||
89 | */ |
||
90 | 1 | public function addHead($uri, $handler, $name = null) |
|
94 | |||
95 | /** |
||
96 | * Syntactic sugar: registers a GET route. |
||
97 | * |
||
98 | * @param string $uri |
||
99 | * @param string $handler |
||
100 | * @param string $name |
||
101 | * (Optional) An unique name for this route. |
||
102 | */ |
||
103 | 1 | public function addGet($uri, $handler, $name = null) |
|
107 | |||
108 | /** |
||
109 | * Syntactic sugar: registers a DELETE route. |
||
110 | * |
||
111 | * @param string $uri |
||
112 | * @param string $handler |
||
113 | * @param string $name |
||
114 | * (Optional) An unique name for this route. |
||
115 | */ |
||
116 | 1 | public function addDelete($uri, $handler, $name = null) |
|
120 | |||
121 | /** |
||
122 | * Syntactic sugar: registers an OPTIONS route. |
||
123 | * |
||
124 | * @param string $uri |
||
125 | * @param string $handler |
||
126 | * @param string $name |
||
127 | * (Optional) An unique name for this route. |
||
128 | */ |
||
129 | 1 | public function addOptions($uri, $handler, $name = null) |
|
133 | |||
134 | /** |
||
135 | * Syntactic sugar: registers a PATCH route. |
||
136 | * |
||
137 | * @param string $uri |
||
138 | * @param string $handler |
||
139 | * @param string $name |
||
140 | * (Optional) An unique name for this route. |
||
141 | */ |
||
142 | 1 | public function addPatch($uri, $handler, $name = null) |
|
146 | |||
147 | /** |
||
148 | * Syntactic sugar: registers a POST route. |
||
149 | * |
||
150 | * @param string $uri |
||
151 | * @param string $handler |
||
152 | * @param string $name |
||
153 | * (Optional) An unique name for this route. |
||
154 | */ |
||
155 | 1 | public function addPost($uri, $handler, $name = null) |
|
159 | |||
160 | /** |
||
161 | * Syntactic sugar: registers a PUT route. |
||
162 | * |
||
163 | * @param string $uri |
||
164 | * @param string $handler |
||
165 | * @param string $name |
||
166 | * (Optional) An unique name for this route. |
||
167 | */ |
||
168 | 1 | public function addPut($uri, $handler, $name = null) |
|
172 | |||
173 | /** |
||
174 | * Disables distinguishing an extra slash on the end of incoming URIs as a |
||
175 | * different URL. |
||
176 | */ |
||
177 | 1 | public function disableTrailingSlashCheck() |
|
181 | |||
182 | /** |
||
183 | * Registers a matching pattern for URI parameters. |
||
184 | * |
||
185 | * @param string $name |
||
186 | * Name which will appear within curly-braces in URI patterns. |
||
187 | * @param string $regexp |
||
188 | * Regexp substitution pattern. |
||
189 | */ |
||
190 | 1 | public function addPattern($name, $regexp) |
|
194 | |||
195 | /** |
||
196 | * Finds the uri associated with a given name. |
||
197 | * |
||
198 | * @param string $name |
||
199 | * Name of the route to find. |
||
200 | * @param array $parameters |
||
201 | * (Optional) Parameters to complete the URI. |
||
202 | * |
||
203 | * @return string|null |
||
204 | */ |
||
205 | 2 | public function findURI($name, $parameters = []) |
|
216 | |||
217 | /** |
||
218 | * Parses an incoming method and URI, and returns a matching route. |
||
219 | * |
||
220 | * @param string $method |
||
221 | * HTTP verb to find a match on. |
||
222 | * @param string $uri |
||
223 | * URI pattern to find a match on. |
||
224 | * @param string $prefix |
||
225 | * (Optional) Prefix to prepend to URI path. |
||
226 | * |
||
227 | * @return ParsedRoute |
||
228 | * |
||
229 | * @throws MethodNotAllowedException |
||
230 | * Thrown if a handler is registered for this route, but it is not |
||
231 | * configured to handle this verb. |
||
232 | * @throws RouteNotFoundException |
||
233 | * Thrown if there is no handler registered for this route. |
||
234 | */ |
||
235 | 17 | public function parse($method, $uri, $prefix = "") |
|
260 | |||
261 | /** |
||
262 | * Finds the first matching route for a given method and URI. |
||
263 | * |
||
264 | * @param string $method |
||
265 | * HTTP verb to find a match on. |
||
266 | * @param string $uri |
||
267 | * URI pattern to find a match on. |
||
268 | * @param string $prefix |
||
269 | * (Optional) Prefix to prepend to URI path. |
||
270 | * |
||
271 | * @return ParsedRoute |
||
272 | * |
||
273 | * @throws RouteNotFoundException |
||
274 | */ |
||
275 | 17 | private function findMatches($method, $uri, $prefix = "") |
|
290 | |||
291 | /** |
||
292 | * Applies standard regexp patterns to an incoming URI route. |
||
293 | * |
||
294 | * @param string $route |
||
295 | * |
||
296 | * @return string |
||
297 | */ |
||
298 | 17 | private function prepareRouteRegexp($route) |
|
305 | } |
||
306 |