1 | <?php |
||
10 | class Router |
||
11 | { |
||
12 | private $trailing_slash_check = true; |
||
13 | private $routes = []; |
||
14 | private $route_names = []; |
||
15 | private $regexp_map = [ |
||
16 | '/\{([A-Za-z]\w*)\}/' => '(?<$1>[^/]+)', |
||
17 | '/\{([A-Za-z]\w*):word\}/' => '(?<$1>\w+)', |
||
18 | '/\{([A-Za-z]\w*):number\}/' => '(?<$1>\d+)', |
||
19 | '/\{([A-Za-z]\w*):slug\}/' => '(?<$1>[A-Za-z0-9_-]+)', |
||
20 | '/\{([A-Za-z]\w*):([^}]+)\}/' => '(?<$1>$2)', |
||
21 | '/\//' => '\/' |
||
22 | ]; |
||
23 | private static $parsed_regexp = []; // cache |
||
24 | |||
25 | /** |
||
26 | * Creates a new Router. |
||
27 | * |
||
28 | * @param Route[] $routes |
||
29 | * (Optional) Array of Route value objects to create. |
||
30 | * |
||
31 | * @throws \RunTimeException |
||
32 | * Thrown if array not contains Router instances. |
||
33 | */ |
||
34 | 20 | public function __construct(array $routes = []) |
|
40 | |||
41 | /** |
||
42 | * Registers a route. |
||
43 | * |
||
44 | * @param Route $route |
||
45 | * Route Value Object. |
||
46 | */ |
||
47 | 10 | public function add(Route $route) |
|
51 | |||
52 | /** |
||
53 | * Registers a route. |
||
54 | * |
||
55 | * @param string|array $methods |
||
56 | * Either a single HTTP verb, or an array of verbs. |
||
57 | * @param string $uri |
||
58 | * URI pattern to match for this route. |
||
59 | * @param string $handler |
||
60 | * ClassName::methodName to invoke for this route. If methodName |
||
61 | * is not present, a method of 'index' is assumed. |
||
62 | * @param string $name |
||
63 | * (Optional) An unique name for this route. |
||
64 | */ |
||
65 | 20 | public function addRoute($methods, $uri, $handler, $name = null) |
|
78 | |||
79 | /** |
||
80 | * Registers a new route provider to feed new routes |
||
81 | * |
||
82 | * @param RouteProvider $provider |
||
83 | */ |
||
84 | 1 | public function addRouteProvider(RouteProvider $provider) |
|
85 | { |
||
86 | 1 | foreach ($provider->routes() as $route) { |
|
87 | 1 | $this->add($route); |
|
88 | } |
||
89 | 1 | } |
|
90 | |||
91 | /** |
||
92 | * Syntactic sugar: registers a HEAD route. |
||
93 | * |
||
94 | * @param string $uri |
||
95 | * @param string $handler |
||
96 | * @param string $name |
||
97 | * (Optional) An unique name for this route. |
||
98 | */ |
||
99 | 1 | public function addHead($uri, $handler, $name = null) |
|
103 | |||
104 | /** |
||
105 | * Syntactic sugar: registers a GET route. |
||
106 | * |
||
107 | * @param string $uri |
||
108 | * @param string $handler |
||
109 | * @param string $name |
||
110 | * (Optional) An unique name for this route. |
||
111 | */ |
||
112 | 1 | public function addGet($uri, $handler, $name = null) |
|
116 | |||
117 | /** |
||
118 | * Syntactic sugar: registers a DELETE route. |
||
119 | * |
||
120 | * @param string $uri |
||
121 | * @param string $handler |
||
122 | * @param string $name |
||
123 | * (Optional) An unique name for this route. |
||
124 | */ |
||
125 | 1 | public function addDelete($uri, $handler, $name = null) |
|
129 | |||
130 | /** |
||
131 | * Syntactic sugar: registers an OPTIONS route. |
||
132 | * |
||
133 | * @param string $uri |
||
134 | * @param string $handler |
||
135 | * @param string $name |
||
136 | * (Optional) An unique name for this route. |
||
137 | */ |
||
138 | 1 | public function addOptions($uri, $handler, $name = null) |
|
142 | |||
143 | /** |
||
144 | * Syntactic sugar: registers a PATCH route. |
||
145 | * |
||
146 | * @param string $uri |
||
147 | * @param string $handler |
||
148 | * @param string $name |
||
149 | * (Optional) An unique name for this route. |
||
150 | */ |
||
151 | 1 | public function addPatch($uri, $handler, $name = null) |
|
155 | |||
156 | /** |
||
157 | * Syntactic sugar: registers a POST route. |
||
158 | * |
||
159 | * @param string $uri |
||
160 | * @param string $handler |
||
161 | * @param string $name |
||
162 | * (Optional) An unique name for this route. |
||
163 | */ |
||
164 | 1 | public function addPost($uri, $handler, $name = null) |
|
168 | |||
169 | /** |
||
170 | * Syntactic sugar: registers a PUT route. |
||
171 | * |
||
172 | * @param string $uri |
||
173 | * @param string $handler |
||
174 | * @param string $name |
||
175 | * (Optional) An unique name for this route. |
||
176 | */ |
||
177 | 1 | public function addPut($uri, $handler, $name = null) |
|
181 | |||
182 | /** |
||
183 | * Disables distinguishing an extra slash on the end of incoming URIs as a |
||
184 | * different URL. |
||
185 | */ |
||
186 | 1 | public function disableTrailingSlashCheck() |
|
190 | |||
191 | /** |
||
192 | * Registers a matching pattern for URI parameters. |
||
193 | * |
||
194 | * @param string $name |
||
195 | * Name which will appear within curly-braces in URI patterns. |
||
196 | * @param string $regexp |
||
197 | * Regexp substitution pattern. |
||
198 | */ |
||
199 | 1 | public function addPattern($name, $regexp) |
|
203 | |||
204 | /** |
||
205 | * Finds the uri associated with a given name. |
||
206 | * |
||
207 | * @param string $name |
||
208 | * Name of the route to find. |
||
209 | * @param array $parameters |
||
210 | * (Optional) Parameters to complete the URI. |
||
211 | * |
||
212 | * @return string|null |
||
213 | */ |
||
214 | 2 | public function findURI($name, $parameters = []) |
|
227 | |||
228 | /** |
||
229 | * Parses an incoming method and URI, and returns a matching route. |
||
230 | * |
||
231 | * @param string $method |
||
232 | * HTTP verb to find a match on. |
||
233 | * @param string $uri |
||
234 | * URI pattern to find a match on. |
||
235 | * @param string $prefix |
||
236 | * (Optional) Prefix to prepend to URI path. |
||
237 | * |
||
238 | * @return ParsedRoute |
||
239 | * |
||
240 | * @throws MethodNotAllowedException |
||
241 | * Thrown if a handler is registered for this route, but it is not |
||
242 | * configured to handle this verb. |
||
243 | * @throws RouteNotFoundException |
||
244 | * Thrown if there is no handler registered for this route. |
||
245 | */ |
||
246 | 18 | public function parse($method, $uri, $prefix = "") |
|
271 | |||
272 | /** |
||
273 | * Finds the first matching route for a given method and URI. |
||
274 | * |
||
275 | * @param string $method |
||
276 | * HTTP verb to find a match on. |
||
277 | * @param string $uri |
||
278 | * URI pattern to find a match on. |
||
279 | * @param string $prefix |
||
280 | * (Optional) Prefix to prepend to URI path. |
||
281 | * |
||
282 | * @return ParsedRoute |
||
283 | * |
||
284 | * @throws RouteNotFoundException |
||
285 | */ |
||
286 | 18 | private function findMatches($method, $uri, $prefix = "") |
|
301 | |||
302 | /** |
||
303 | * Applies standard regexp patterns to an incoming URI route. |
||
304 | * |
||
305 | * @param string $route |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | 18 | private function prepareRouteRegexp($route) |
|
316 | } |
||
317 |