1 | <?php |
||
21 | class Router implements \IteratorAggregate |
||
22 | { |
||
23 | /** @var string The base path. */ |
||
24 | private $basePath; |
||
25 | |||
26 | /** @var array The routes. */ |
||
27 | private $routes; |
||
28 | |||
29 | /** |
||
30 | * Construct a Router object with the given routers. |
||
31 | * |
||
32 | * @param string $basePath = '' |
||
33 | */ |
||
34 | 19 | public function __construct($basePath = '') |
|
39 | |||
40 | /** |
||
41 | * Returns the base path. |
||
42 | * |
||
43 | * @return string the base path. |
||
44 | */ |
||
45 | 2 | public function getBasePath() |
|
49 | |||
50 | /** |
||
51 | * Set the base path. |
||
52 | * |
||
53 | * @param string $basePath |
||
54 | * @return $this |
||
55 | */ |
||
56 | 2 | public function setBasePath($basePath) |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | 1 | public function getIterator() |
|
70 | |||
71 | /** |
||
72 | * Returns the number of key-value mappings in the router map. |
||
73 | * |
||
74 | * @return int the number of key-value mappings in the router map. |
||
75 | */ |
||
76 | 1 | public function count() |
|
86 | |||
87 | /** |
||
88 | * Returns true if the router map contains no key-value mappings. |
||
89 | * |
||
90 | * @return bool true if the router map contains no key-value mappings. |
||
91 | */ |
||
92 | 2 | public function isEmpty() |
|
96 | |||
97 | /** |
||
98 | * Returns true if the router map contains a mapping for the specified method & route. |
||
99 | * |
||
100 | * @param string $method |
||
101 | * @param string $route |
||
102 | * @return bool true if the static route map contains a mapping for the specified method & route. |
||
103 | */ |
||
104 | 2 | public function contains($method, $route) |
|
108 | |||
109 | /** |
||
110 | * Returns true if the router map maps one or more routes to the specified callable. |
||
111 | * |
||
112 | * @param callable $callable |
||
113 | * @return bool true if the router map maps one or more routes to the specified callable. |
||
114 | */ |
||
115 | 1 | public function containsCallable(callable $callable) |
|
128 | |||
129 | /** |
||
130 | * Returns the callable to which the specified route is mapped, or null if the router map contains no mapping for the route. |
||
131 | * |
||
132 | * @param string $method |
||
133 | * @param string $route |
||
134 | * @return callable the callable to which the specified route is mapped, or null if the router map contains no mapping for the route. |
||
135 | */ |
||
136 | 4 | public function get($method, $route) |
|
137 | { |
||
138 | 4 | if (!array_key_exists($method, $this->routes)) { |
|
139 | 2 | return null; |
|
140 | } |
||
141 | |||
142 | 3 | $pattern = $this->createPattern($route); |
|
143 | 3 | ||
144 | 3 | if (array_key_exists($pattern, $this->routes[$method])) { |
|
145 | return $this->routes[$method][$pattern]->callable; |
||
146 | } |
||
147 | |||
148 | 2 | return null; |
|
149 | } |
||
150 | |||
151 | /** |
||
152 | * Associates the specified callable with the specified method & route in the route map. |
||
153 | * |
||
154 | * @param string|array $methods |
||
155 | * @param string $route |
||
156 | * @param callable $callable |
||
157 | * @return $this |
||
158 | */ |
||
159 | 19 | public function set($methods, $route, callable $callable) |
|
171 | |||
172 | /** |
||
173 | * Adds a method & route to the to the route map. |
||
174 | * |
||
175 | * @param string $method |
||
176 | * @param string $route |
||
177 | * @param callable $callable |
||
178 | */ |
||
179 | 19 | private function add(string $method, string $route, callable $callable) |
|
195 | |||
196 | /** |
||
197 | * Creates a regex-enabled pattern from the route |
||
198 | * |
||
199 | * @param string $route |
||
200 | 19 | * @return string the pattern string. |
|
201 | */ |
||
202 | 19 | private function createPattern(string $route) |
|
206 | |||
207 | /** |
||
208 | * Removes the mapping for the specified route from the router map if present. |
||
209 | * |
||
210 | * @param string $method |
||
211 | * @param string $route |
||
212 | 3 | * @return null |
|
213 | */ |
||
214 | 3 | public function remove($method, $route) |
|
230 | |||
231 | /** |
||
232 | * Removes all of the mappings from the router map. |
||
233 | * |
||
234 | 1 | * @return null |
|
235 | */ |
||
236 | 1 | public function clear() |
|
240 | |||
241 | /** |
||
242 | * Returns the result of the given route's callable. |
||
243 | * |
||
244 | * @param string|null $method = new ServerRequest()->getMethod() |
||
245 | * @param string|null $route = new ServerRequest()->getUri()->getPath() |
||
246 | * @return mixed the result of the given route's callable. |
||
247 | 8 | * @throws ServerResponseException |
|
248 | */ |
||
249 | 8 | public function resolve($method = null, $route = null) |
|
267 | |||
268 | /** |
||
269 | * Returns the callable to which the specied method and route are mapped. |
||
270 | * |
||
271 | * @param string $method |
||
272 | * @param string $route |
||
273 | * @return array the callable to which the specied method and route are mapped and the route matches. |
||
274 | 8 | * @throws ServerResponseException |
|
275 | */ |
||
276 | 8 | private function getCallable($method, $route) |
|
291 | |||
292 | /** |
||
293 | * Returns the result of the callable. |
||
294 | * |
||
295 | * @param array the callable and the route matches. |
||
296 | 6 | * @return mixed the result the callable. |
|
297 | */ |
||
298 | 6 | private function callCallable($callable) |
|
302 | } |
||
303 |