| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace brendt\stitcher\controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use brendt\stitcher\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use brendt\stitcher\exception\StitcherException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use brendt\stitcher\factory\AdapterFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use brendt\stitcher\Stitcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Routing\Exception\ResourceNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Routing\Matcher\UrlMatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Routing\RequestContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Routing\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Routing\RouteCollection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * The developer controller is used to render pages on the fly (on an HTTP request). This controller enables a | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * developer make code changes and see those changes real-time without re-compiling the whole site. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 18 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  | class DevController { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      * @var Stitcher | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     protected $stitcher; | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * Create a new developer controller with optional configuration path and -file. | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @param string $path | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param string $name | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function __construct($path = './', $name = 'config.dev.yml') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         Config::load($path, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->stitcher = new Stitcher(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * Run the developer controller. This function will read the request URL and dispatch the according route. | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @param string $url | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function run($url = null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if ($url === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $request = explode('?', $_SERVER['REQUEST_URI']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $url = reset($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             return $this->getPage($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } catch (StitcherException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             return $e->getMessage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } catch (ResourceNotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             return "404"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * Create the route container | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * @return \Symfony\Component\Routing\RouteCollection | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected function createRouteCollection() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $routeCollection = new RouteCollection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $site = $this->stitcher->loadSite(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         foreach ($site as $page) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $route = $page->getId(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $routeCollection->add($route, new Route($route)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             if ($page->getAdapterConfig(AdapterFactory::PAGINATION_ADAPTER)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $paginationRoute = $route . '/page-{page}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $routeCollection->add($paginationRoute, new Route($paginationRoute)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return $routeCollection; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * @param $url | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * @return mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     protected function getPage($url) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $routeCollection = $this->createRouteCollection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $matcher = new UrlMatcher($routeCollection, new RequestContext()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $routeResult = $matcher->match($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $route = $routeResult['_route']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $id = isset($routeResult['id']) ? $routeResult['id'] : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if (isset($routeResult['page'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             $route = str_replace('/page-{page}', '', $route); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             $id = $routeResult['page']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $blanket = $this->stitcher->stitch($route, $id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         if (isset($blanket[$route])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             return $blanket[$route]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         if (isset($blanket[$url])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             return $blanket[$url]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         throw new ResourceNotFoundException(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                        
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: