| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * ZfDebugModule. WebUI and Console commands 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 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * RouteCollectionBuilder is only able to build FlatRouteCollection(s). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class RouteCollectionBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $routes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * RoutesInspector constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @param array $routes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 10 |  |     public function __construct(array $routes) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 10 |  |         $this->routes = $routes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 10 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return FlatRouteCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 10 |  |     public function build() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 10 |  |         return $this->processRoutes(new FlatRouteCollection(), $this->routes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param FlatRouteCollection $routeCollection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param array               $routesConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param Route               $parentRoute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return FlatRouteCollection | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 46 | 10 |  |     private function processRoutes(FlatRouteCollection $routeCollection, array $routesConfig, Route $parentRoute = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 48 | 10 |  |         foreach (array_keys($routesConfig) as $k) { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 | 9 |  |             $childRoutes = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 50 | 9 |  |             if (isset($routesConfig[$k]['child_routes']) && !empty($routesConfig[$k]['child_routes'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 | 9 |  |                 $childRoutes = $routesConfig[$k]['child_routes']; | 
            
                                                                        
                            
            
                                    
            
            
                | 52 | 9 |  |                 unset($routesConfig[$k]['child_routes']); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 9 |  |             $routesConfig[$k]['name'] = $k; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 9 |  |             $route = $this->processRoute($routesConfig[$k], $parentRoute); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 | 9 |  |             if (!isset($routesConfig[$k]['may_terminate']) || false !== $routesConfig[$k]['may_terminate']) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 | 9 |  |                 $routeCollection->addRoute($route); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 | 9 |  |             if (null !== $childRoutes) { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 9 |  |                 $this->processRoutes($routeCollection, $childRoutes, $route); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 | 10 |  |         return $routeCollection; | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param array      $routeConfig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param Route|null $parentRoute | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @return Route | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 9 |  |     private function processRoute(array $routeConfig, Route $parentRoute = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 9 |  |         $name = $routeConfig['name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 9 |  |         $url = isset($routeConfig['options']['route']) ? $routeConfig['options']['route'] : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 9 |  |         $controller = isset($routeConfig['options']['defaults']['controller']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 9 |  |             ? $routeConfig['options']['defaults']['controller'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 9 |  |         $action = isset($routeConfig['options']['defaults']['action']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 9 |  |             ? $routeConfig['options']['defaults']['action'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 9 |  |         if (null !== $parentRoute) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 9 |  |             $name = $parentRoute->getName() . '/' . $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 9 |  |             $url = $parentRoute->getUrl() . $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 9 |  |             if (null === $controller) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |                 $controller = $parentRoute->getController(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 9 |  |         if (null === $action) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |             $action = 'index'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 9 |  |         $action .= 'Action'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 9 |  |         return new Route($name, $url, $controller, $action); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  |