1 | <?php |
||
29 | class RouteCollection implements RouteCollectionInterface |
||
30 | { |
||
31 | /** |
||
32 | * |
||
33 | * Controller::Method delimiter. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $delimiter = '::'; |
||
38 | |||
39 | /** |
||
40 | * RouteInterfaces |
||
41 | * |
||
42 | * @var array. |
||
43 | */ |
||
44 | protected $collection = []; |
||
45 | /** |
||
46 | * Route group context. |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $group = []; |
||
51 | /** |
||
52 | * Route group names. |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $groupNames = []; |
||
57 | /** |
||
58 | * Namespaces for Controller::Method handler. |
||
59 | * |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $namespace = []; |
||
63 | /** |
||
64 | * Global Collection route names. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $globalRouteNames = []; |
||
69 | /** |
||
70 | * Is grouped callback flag. |
||
71 | * |
||
72 | * @var bool |
||
73 | */ |
||
74 | private $_isGrouped = false; |
||
75 | |||
76 | /** |
||
77 | * Add Namespace to Route Collection. |
||
78 | * |
||
79 | * @param string $namespace Controller Namespace. |
||
80 | */ |
||
81 | 2 | public function addNamespace(string $namespace) |
|
82 | { |
||
83 | 2 | $formattedNamespace = '\\'.ltrim($namespace,'\\'); |
|
84 | 2 | $this->namespace[] = $formattedNamespace; |
|
85 | 2 | } |
|
86 | |||
87 | /** |
||
88 | * @param array $names |
||
89 | */ |
||
90 | public function addGlobalNames(array $names) |
||
96 | |||
97 | /** |
||
98 | * @inheritdoc |
||
99 | */ |
||
100 | 2 | public function group($path = '', callable $routeCallback, array $names = []) |
|
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | * |
||
114 | * @throws \Rs\Router\Exceptions\UniqueRouteException |
||
115 | */ |
||
116 | 19 | public function addRoute($method, $pattern, $handler, $name = []) |
|
136 | |||
137 | /** |
||
138 | * Add Namespace to handler. |
||
139 | * |
||
140 | * @param mixed $handler |
||
141 | * |
||
142 | * @return mixed |
||
143 | */ |
||
144 | 18 | private function _findHandlerNamespace($handler) |
|
160 | |||
161 | /** |
||
162 | * Check that the Route is unique before adding it (again). |
||
163 | * |
||
164 | * @param $method |
||
165 | * @param $pattern |
||
166 | * @return bool |
||
167 | * @throws UniqueRouteException |
||
168 | */ |
||
169 | 19 | private function _isUnique($method, $pattern) |
|
181 | |||
182 | /** |
||
183 | * Get Collection global route names. |
||
184 | * |
||
185 | * @param array $names Routes names to merge with globals. |
||
186 | * |
||
187 | * @return array |
||
188 | */ |
||
189 | 19 | private function _getRouteGlobals(array $names) |
|
197 | |||
198 | /** |
||
199 | * Merge Route Names with group. |
||
200 | * |
||
201 | * @param array $names Route names to be added to group. |
||
202 | * |
||
203 | * @return array |
||
204 | */ |
||
205 | 19 | private function _mergeGroupNames(array $names) |
|
215 | |||
216 | /** |
||
217 | * Format Route Pattern and return with grouped directory is necessary. |
||
218 | * |
||
219 | * @param $pattern |
||
220 | * @return string |
||
221 | */ |
||
222 | 21 | public function formatRoutePattern($pattern) |
|
239 | |||
240 | /** |
||
241 | * @inheritdoc |
||
242 | */ |
||
243 | 2 | public function mergeCollection(RouteCollectionInterface $collection, bool $throw = true) |
|
264 | |||
265 | /** |
||
266 | * @inheritdoc |
||
267 | */ |
||
268 | 1 | public function getRoutes() |
|
272 | |||
273 | /** |
||
274 | * Returns current route. |
||
275 | * |
||
276 | * @return RouteInterface |
||
277 | */ |
||
278 | 8 | public function current() |
|
282 | |||
283 | /** |
||
284 | * Returns next route. |
||
285 | * |
||
286 | * @return RouteInterface |
||
287 | */ |
||
288 | 5 | public function next() |
|
292 | |||
293 | /** |
||
294 | * Returns current route index. |
||
295 | * |
||
296 | * @return mixed |
||
297 | */ |
||
298 | 3 | public function key() |
|
302 | |||
303 | /** |
||
304 | * If last route not reached. |
||
305 | * |
||
306 | * @return bool |
||
307 | */ |
||
308 | 5 | public function valid() |
|
313 | |||
314 | /** |
||
315 | * Return to and return first route. |
||
316 | * |
||
317 | * @return RouteInterface |
||
318 | */ |
||
319 | 4 | public function rewind() |
|
323 | |||
324 | /** |
||
325 | * Get total routes in collection. |
||
326 | * |
||
327 | * @return int |
||
328 | */ |
||
329 | 2 | public function count() |
|
333 | } |
||
334 |