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