| Total Complexity | 41 |
| Total Lines | 236 |
| Duplicated Lines | 0 % |
| Changes | 11 | ||
| Bugs | 3 | Features | 1 |
Complex classes like Laravel often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Laravel, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Laravel |
||
| 15 | { |
||
| 16 | /**@var Container */ |
||
| 17 | protected $currentApp; |
||
| 18 | |||
| 19 | /**@var Container */ |
||
| 20 | protected $snapshotApp; |
||
| 21 | |||
| 22 | /**@var ReflectionApp */ |
||
| 23 | protected $reflectionApp; |
||
| 24 | |||
| 25 | /**@var HttpKernel */ |
||
| 26 | protected $kernel; |
||
| 27 | |||
| 28 | /**@var array */ |
||
| 29 | protected $conf = []; |
||
| 30 | |||
| 31 | /**@var array */ |
||
| 32 | protected static $staticBlackList = [ |
||
| 33 | '/index.php' => true, |
||
| 34 | '/.htaccess' => true, |
||
| 35 | '/web.config' => true, |
||
| 36 | ]; |
||
| 37 | /**@var array */ |
||
| 38 | protected static $staticIndexList = [ |
||
| 39 | 'index.html', |
||
| 40 | ]; |
||
| 41 | |||
| 42 | /**@var array */ |
||
| 43 | private $rawGlobals = []; |
||
| 44 | |||
| 45 | /**@var CleanerManager */ |
||
| 46 | protected $cleanerManager; |
||
| 47 | |||
| 48 | public function __construct(array $conf = []) |
||
| 49 | { |
||
| 50 | $this->conf = $conf; |
||
| 51 | |||
| 52 | // Merge $_ENV $_SERVER |
||
| 53 | $this->rawGlobals['_SERVER'] = $_SERVER + $this->conf['_SERVER']; |
||
| 54 | $this->rawGlobals['_ENV'] = $_ENV + $this->conf['_ENV']; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function prepareLaravel() |
||
| 58 | { |
||
| 59 | list($this->currentApp, $this->kernel) = $this->createAppKernel(); |
||
| 60 | |||
| 61 | $this->reflectionApp = new ReflectionApp($this->currentApp); |
||
| 62 | |||
| 63 | $this->saveSnapshot(); |
||
| 64 | |||
| 65 | // Create cleaner manager |
||
| 66 | $this->cleanerManager = new CleanerManager($this->currentApp, $this->snapshotApp, $this->conf); |
||
| 67 | } |
||
| 68 | |||
| 69 | protected function saveSnapshot() |
||
| 70 | { |
||
| 71 | $this->snapshotApp = clone $this->currentApp; |
||
| 72 | |||
| 73 | $instances = $this->reflectionApp->instances(); |
||
| 74 | |||
| 75 | foreach ($instances as $key => $value) { |
||
| 76 | $this->snapshotApp->offsetSet($key, is_object($value) ? clone $value : $value); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | protected function createAppKernel() |
||
| 81 | { |
||
| 82 | // Register autoload |
||
| 83 | self::autoload($this->conf['root_path']); |
||
| 84 | |||
| 85 | // Make kernel for Laravel |
||
| 86 | $app = require $this->conf['root_path'] . '/bootstrap/app.php'; |
||
| 87 | $kernel = $this->conf['is_lumen'] ? null : $app->make(HttpKernel::class); |
||
| 88 | |||
| 89 | // Boot |
||
| 90 | if ($this->conf['is_lumen']) { |
||
| 91 | $this->configureLumen($app); |
||
| 92 | if (method_exists($app, 'boot')) { |
||
| 93 | $app->boot(); |
||
| 94 | } |
||
| 95 | } else { |
||
| 96 | $app->make(ConsoleKernel::class)->bootstrap(); |
||
| 97 | } |
||
| 98 | |||
| 99 | return [$app, $kernel]; |
||
| 100 | } |
||
| 101 | |||
| 102 | protected function configureLumen(Container $app) |
||
| 103 | { |
||
| 104 | $cfgPaths = [ |
||
| 105 | // Framework default configuration |
||
| 106 | $this->conf['root_path'] . '/vendor/laravel/lumen-framework/config/', |
||
| 107 | // App configuration |
||
| 108 | $this->conf['root_path'] . '/config/', |
||
| 109 | ]; |
||
| 110 | |||
| 111 | $keys = []; |
||
| 112 | foreach ($cfgPaths as $cfgPath) { |
||
| 113 | $configs = (array)glob($cfgPath . '*.php'); |
||
| 114 | foreach ($configs as $config) { |
||
| 115 | $config = substr(basename($config), 0, -4); |
||
| 116 | $keys[$config] = $config; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | foreach ($keys as $key) { |
||
| 121 | $app->configure($key); |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | public static function autoload($rootPath) |
||
| 126 | { |
||
| 127 | $autoload = $rootPath . '/bootstrap/autoload.php'; |
||
| 128 | if (file_exists($autoload)) { |
||
| 129 | require_once $autoload; |
||
| 130 | } else { |
||
| 131 | require_once $rootPath . '/vendor/autoload.php'; |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | public function getRawGlobals() |
||
| 136 | { |
||
| 137 | return $this->rawGlobals; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function handleDynamic(IlluminateRequest $request) |
||
| 141 | { |
||
| 142 | ob_start(); |
||
| 143 | |||
| 144 | if ($this->conf['is_lumen']) { |
||
| 145 | $response = $this->currentApp->dispatch($request); |
||
| 146 | if ($response instanceof SymfonyResponse) { |
||
| 147 | $content = $response->getContent(); |
||
| 148 | } else { |
||
| 149 | $content = $response; |
||
| 150 | } |
||
| 151 | |||
| 152 | $this->reflectionApp->callTerminableMiddleware($response); |
||
| 153 | } else { |
||
| 154 | $response = $this->kernel->handle($request); |
||
| 155 | $content = $response->getContent(); |
||
| 156 | $this->kernel->terminate($request, $response); |
||
| 157 | } |
||
| 158 | |||
| 159 | // prefer content in response, secondly ob |
||
| 160 | if (!($response instanceof StreamedResponse) && (string)$content === '' && ob_get_length() > 0) { |
||
| 161 | $response->setContent(ob_get_contents()); |
||
| 162 | } |
||
| 163 | |||
| 164 | ob_end_clean(); |
||
| 165 | |||
| 166 | return $response; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function handleStatic(IlluminateRequest $request) |
||
| 170 | { |
||
| 171 | $uri = $request->getRequestUri(); |
||
| 172 | $uri = (string)str_replace("\0", '', urldecode($uri)); |
||
| 173 | if (isset(self::$staticBlackList[$uri]) || strpos($uri, '/..') !== false) { |
||
| 174 | return false; |
||
| 175 | } |
||
| 176 | |||
| 177 | $requestFile = $this->conf['static_path'] . $uri; |
||
| 178 | if (is_file($requestFile)) { |
||
| 179 | return $this->createStaticResponse($requestFile, $request); |
||
| 180 | } |
||
| 181 | if (is_dir($requestFile)) { |
||
| 182 | $indexFile = $this->lookupIndex($requestFile); |
||
| 183 | if ($indexFile === false) { |
||
| 184 | return false; |
||
| 185 | } |
||
| 186 | return $this->createStaticResponse($indexFile, $request); |
||
| 187 | } |
||
| 188 | return false; |
||
| 189 | } |
||
| 190 | |||
| 191 | protected function lookupIndex($folder) |
||
| 192 | { |
||
| 193 | $folder = rtrim($folder, '/') . '/'; |
||
| 194 | foreach (self::$staticIndexList as $index) { |
||
| 195 | $tmpFile = $folder . $index; |
||
| 196 | if (is_file($tmpFile)) { |
||
| 197 | return $tmpFile; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | return false; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function createStaticResponse($requestFile, IlluminateRequest $request) |
||
| 204 | { |
||
| 205 | $response = new BinaryFileResponse($requestFile); |
||
| 206 | $response->prepare($request); |
||
| 207 | $response->isNotModified($request); |
||
| 208 | return $response; |
||
| 209 | } |
||
| 210 | |||
| 211 | public function clean() |
||
| 212 | { |
||
| 213 | $this->cleanerManager->clean(); |
||
| 214 | $this->cleanerManager->cleanControllers(); |
||
| 215 | } |
||
| 216 | |||
| 217 | public function cleanProviders() |
||
| 218 | { |
||
| 219 | $this->cleanerManager->cleanProviders(); |
||
| 220 | } |
||
| 221 | |||
| 222 | public function fireEvent($name, array $params = []) |
||
| 223 | { |
||
| 224 | $params[] = $this->currentApp; |
||
| 225 | return method_exists($this->currentApp['events'], 'dispatch') ? |
||
| 226 | $this->currentApp['events']->dispatch($name, $params) : $this->currentApp['events']->fire($name, $params); |
||
| 227 | } |
||
| 228 | |||
| 229 | public function bindRequest(IlluminateRequest $request) |
||
| 230 | { |
||
| 231 | $this->currentApp->instance('request', $request); |
||
| 232 | } |
||
| 233 | |||
| 234 | public function bindSwoole($swoole) |
||
| 238 | }); |
||
| 239 | } |
||
| 240 | |||
| 241 | public function bindSwooleHttpResponse(SwooleResponse $response) |
||
| 242 | { |
||
| 243 | $this->currentApp->instance('swoole-http-response', $response); |
||
| 244 | } |
||
| 245 | |||
| 246 | public function saveSession() |
||
| 250 | } |
||
| 251 | } |
||
| 252 | } |
||
| 253 |