| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace SideDevOrg\MiniPhpFw; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Framework bootstrap class. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Bootstrap | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |      * Default options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private $config = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         'lang' => 'en', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         'paths' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             'database' => 'app/config/database.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             'middlewares' => 'app/config/middlewares.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             'routes' => 'app/config/routes.php', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             'env' => 'app', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             'view' => 'app/views', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             'i18n' => 'app/langs', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             'routesCache' => 'app/storage', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             'assets_manifest' => 'static/build/mix-manifest.json', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         'config' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             'not_found_controller' => '\SideDevOrg\MiniPhpFw\Controller::not_found', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Response. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @var \Psr\Http\Message\ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     private $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Load framework. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param array $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return \SideDevOrg\MiniPhpFw\Bootstrap | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 3 |  |     public function load(array $config = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 3 |  |         $this->config = array_replace_recursive($this->config, $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 3 |  |         $this->enviroment(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |         $this->errors(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |         $this->templates(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |         $this->orm(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |         $this->router(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 3 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Load errors strategy. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |     private function errors() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  |         if (env('DEV_MODE')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 3 |  |             $whoops = new \Whoops\Run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 3 |  |             $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |             $whoops->register(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * Load enviroment strategy. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 3 |  |     private function enviroment() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 3 |  |         $dotenv = new \Dotenv\Dotenv($this->config['paths']['env']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 3 |  |         $dotenv->load(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * Load templates strategy. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |     private function templates() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 3 |  |         \Mustache_Autoloader::register(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * Load orm strategy. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  |     private function orm() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 3 |  |         if (env('LOAD_ORM')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 3 |  |             $capsule = new \Illuminate\Database\Capsule\Manager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 3 |  |             $connections = require $this->config['paths']['database']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 3 |  |             foreach ($connections as $connection) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 3 |  |                 $name = $connection['name']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 3 |  |                 unset($connection['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 3 |  |                 $capsule->addConnection($connection, $name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 3 |  |             $capsule->setEventDispatcher( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 3 |  |                 new \Illuminate\Events\Dispatcher( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 3 |  |                     new \Illuminate\Container\Container() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 3 |  |             $capsule->setAsGlobal(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 3 |  |             $capsule->bootEloquent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * Load router strategy. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 114 | 3 |  |     private function router() | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 116 | 3 |  |         $routes = require $this->config['paths']['routes']; | 
            
                                                                        
                            
            
                                    
            
            
                | 117 | 3 |  |         $request = \Zend\Diactoros\ServerRequestFactory::fromGlobals(); | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 | 3 |  |         $cacheFileName = 'routes.'.filemtime($this->config['paths']['routes']).'.cache'; | 
            
                                                                        
                            
            
                                    
            
            
                | 120 | 3 |  |         $cacheFile = $this->config['paths']['routesCache'].'/'.$cacheFileName; | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 122 | 3 |  |         if (!file_exists($cacheFile)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 3 |  |             array_map('unlink', glob( $this->config['paths']['routesCache'].'/*.cache')); | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 | 3 |  |         $dispatcher = \FastRoute\cachedDispatcher( | 
            
                                                                        
                            
            
                                    
            
            
                | 127 | 3 |  |             function(\FastRoute\RouteCollector $r) use ($routes) { | 
            
                                                                        
                            
            
                                    
            
            
                | 128 | 3 |  |                 foreach ($routes as $route) { | 
            
                                                                        
                            
            
                                    
            
            
                | 129 | 2 |  |                     $r->addRoute($route['methods'], $route['endpoint'], $route['call']); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 131 | 3 |  |             }, | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 133 | 3 |  |                 'cacheFile' => $cacheFile, | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 137 | 3 |  |         $request = $request->withHeader('lang', $this->config['lang']); | 
            
                                                                        
                            
            
                                    
            
            
                | 138 | 3 |  |         $request = $request->withHeader('config', json_encode($this->config)); | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 140 | 3 |  |         $routeInfo = $dispatcher->dispatch( | 
            
                                                                        
                            
            
                                    
            
            
                | 141 | 3 |  |             $request->getMethod(), | 
            
                                                                        
                            
            
                                    
            
            
                | 142 | 3 |  |             $request->getUri()->getPath() | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 145 | 3 |  |         $response = new \Zend\Diactoros\Response(); | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 147 | 3 |  |         switch ($routeInfo[0]) { | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 149 | 3 |  |             case \FastRoute\Dispatcher::METHOD_NOT_ALLOWED: | 
            
                                                                        
                            
            
                                    
            
            
                | 150 | 1 |  |                 $response->getBody()->write('METHOD NOT ALLOWED'); | 
            
                                                                        
                            
            
                                    
            
            
                | 151 | 1 |  |                 $response = $response->withStatus(405); | 
            
                                                                        
                            
            
                                    
            
            
                | 152 | 1 |  |                 $this->response = $response; | 
            
                                                                        
                            
            
                                    
            
            
                | 153 | 1 |  |                 break; | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 155 | 2 |  |             case \FastRoute\Dispatcher::NOT_FOUND: | 
            
                                                                        
                            
            
                                    
            
            
                | 156 | 1 |  |             case \FastRoute\Dispatcher::FOUND: | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 158 | 2 |  |                 $handler = isset($routeInfo[1]) ? $routeInfo[1] : $this->config['config']['not_found_controller']; | 
            
                                                                        
                            
            
                                    
            
            
                | 159 | 2 |  |                 $vars = isset($routeInfo[2]) ? $routeInfo[2] : []; | 
            
                                                                        
                            
            
                                    
            
            
                | 160 | 2 |  |                 $handler = explode('::', $handler); | 
            
                                                                        
                            
            
                                    
            
            
                | 161 | 2 |  |                 $class = $handler[0]; | 
            
                                                                        
                            
            
                                    
            
            
                | 162 | 2 |  |                 $method = $handler[1]; | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 164 | 2 |  |                 $response = (new \SideDevOrg\MiniPhpFw\Resolver())->dispatch( | 
            
                                                                        
                            
            
                                    
            
            
                | 165 | 2 |  |                     $class, | 
            
                                                                        
                            
            
                                    
            
            
                | 166 | 2 |  |                     $method, | 
            
                                                                        
                            
            
                                    
            
            
                | 167 | 2 |  |                     $vars, | 
            
                                                                        
                            
            
                                    
            
            
                | 168 | 2 |  |                     $request, | 
            
                                                                        
                            
            
                                    
            
            
                | 169 | 2 |  |                     $response, | 
            
                                                                        
                            
            
                                    
            
            
                | 170 | 2 |  |                     $middlewares = require $this->config['paths']['middlewares'] | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |                 ); | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 173 | 2 |  |                 if (!isset($routeInfo[1])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 174 | 1 |  |                     $response = $response->withStatus(404); | 
            
                                                                        
                            
            
                                    
            
            
                | 175 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 176 | 2 |  |                 $this->response = $response; | 
            
                                                                        
                            
            
                                    
            
            
                | 177 | 2 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |      * Get response. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |      * @return \Psr\Http\Message\ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 3 |  |     public function getResponse() : \Psr\Http\Message\ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 3 |  |         return $this->response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 190 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 191 |  |  |  |