1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ZfDebugModule. Console commands and other utilities for debugging ZF2 apps. |
4
|
|
|
* |
5
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
6
|
|
|
* @copyright 2016 Vítor Brandão <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Noiselabs\ZfDebugModule\Util\Routing; |
10
|
|
|
|
11
|
|
|
use Zend\Mvc\Router\RouteInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* RouteCollectionBuilder is only able to build FlatRouteCollection(s). |
15
|
|
|
*/ |
16
|
|
|
class RouteCollectionBuilder |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var RouteInterface |
20
|
|
|
*/ |
21
|
|
|
private $routes; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* RoutesInspector constructor. |
25
|
|
|
* |
26
|
|
|
* @param array $routes |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $routes = []) |
29
|
|
|
{ |
30
|
|
|
$this->routes = $routes; |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return FlatRouteCollection |
35
|
|
|
*/ |
36
|
|
|
public function build() |
37
|
|
|
{ |
38
|
|
|
return $this->processRoutes(new FlatRouteCollection(), $this->routes); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param FlatRouteCollection $routeCollection |
43
|
|
|
* @param array $routesConfig |
44
|
|
|
* @param Route $parentRoute |
45
|
|
|
* |
46
|
|
|
* @return FlatRouteCollection |
47
|
|
|
*/ |
48
|
|
|
private function processRoutes(FlatRouteCollection $routeCollection, array $routesConfig, Route $parentRoute = null) |
49
|
|
|
{ |
50
|
|
|
foreach (array_keys($routesConfig) as $k) { |
51
|
|
|
$childRoutes = null; |
52
|
|
|
if (isset($routesConfig[$k]['child_routes']) && !empty($routesConfig[$k]['child_routes'])) { |
53
|
|
|
$childRoutes = $routesConfig[$k]['child_routes']; |
54
|
|
|
unset($routesConfig[$k]['child_routes']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$routesConfig[$k]['name'] = $k; |
58
|
|
|
$route = $this->processRoute($routesConfig[$k], $parentRoute); |
59
|
|
|
if (!isset($routesConfig[$k]['may_terminate']) || false !== $routesConfig[$k]['may_terminate']) { |
60
|
|
|
$routeCollection->addRoute($route); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (null !== $childRoutes) { |
64
|
|
|
$this->processRoutes($routeCollection, $childRoutes, $route); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $routeCollection; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $routeConfig |
73
|
|
|
* @param Route|null $parentRoute |
74
|
|
|
* |
75
|
|
|
* @return Route |
76
|
|
|
*/ |
77
|
|
|
private function processRoute(array $routeConfig, Route $parentRoute = null) |
78
|
|
|
{ |
79
|
|
|
$name = $routeConfig['name']; |
80
|
|
|
$url = isset($routeConfig['options']['route']) ? $routeConfig['options']['route'] : ''; |
81
|
|
|
$controller = isset($routeConfig['options']['defaults']['controller']) |
82
|
|
|
? $routeConfig['options']['defaults']['controller'] : null; |
83
|
|
|
$action = isset($routeConfig['options']['defaults']['action']) |
84
|
|
|
? $routeConfig['options']['defaults']['action'] : null; |
85
|
|
|
|
86
|
|
|
if (null !== $parentRoute) { |
87
|
|
|
$name = $parentRoute->getName() . '/' . $name; |
88
|
|
|
$url = $parentRoute->getUrl() . $url; |
89
|
|
|
if (null === $controller) { |
90
|
|
|
$controller = $parentRoute->getController(); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return new Route($name, $url, $controller, $action); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..