Total Complexity | 41 |
Total Lines | 239 |
Duplicated Lines | 0 % |
Changes | 17 | ||
Bugs | 4 | Features | 2 |
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 |
||
13 | class Laravel |
||
14 | { |
||
15 | /**@var Container */ |
||
16 | protected $currentApp; |
||
17 | |||
18 | /**@var Container */ |
||
19 | protected $snapshotApp; |
||
20 | |||
21 | /**@var ReflectionApp */ |
||
22 | protected $reflectionApp; |
||
23 | |||
24 | /**@var HttpKernel */ |
||
25 | protected $kernel; |
||
26 | |||
27 | /**@var array */ |
||
28 | protected $conf = []; |
||
29 | |||
30 | /**@var array */ |
||
31 | protected static $staticBlackList = [ |
||
32 | '/index.php' => 1, |
||
33 | '/.htaccess' => 1, |
||
34 | '/web.config' => 1, |
||
35 | ]; |
||
36 | |||
37 | /**@var array */ |
||
38 | private $rawGlobals = []; |
||
39 | |||
40 | /**@var CleanerManager */ |
||
41 | protected $cleanerManager; |
||
42 | |||
43 | public function __construct(array $conf = []) |
||
44 | { |
||
45 | $this->conf = $conf; |
||
46 | |||
47 | // Merge $_ENV $_SERVER |
||
48 | $this->rawGlobals['_SERVER'] = $_SERVER + $this->conf['_SERVER']; |
||
49 | $this->rawGlobals['_ENV'] = $_ENV + $this->conf['_ENV']; |
||
50 | } |
||
51 | |||
52 | public function prepareLaravel() |
||
53 | { |
||
54 | list($this->currentApp, $this->kernel) = $this->createAppKernel(); |
||
55 | |||
56 | $this->reflectionApp = new ReflectionApp($this->currentApp); |
||
57 | |||
58 | $this->saveSnapshot(); |
||
59 | |||
60 | // Create cleaner manager |
||
61 | $this->cleanerManager = new CleanerManager($this->currentApp, $this->snapshotApp, $this->conf); |
||
62 | } |
||
63 | |||
64 | protected function saveSnapshot() |
||
65 | { |
||
66 | $this->snapshotApp = clone $this->currentApp; |
||
67 | |||
68 | $instances = $this->reflectionApp->instances(); |
||
69 | |||
70 | foreach ($instances as $key => $value) { |
||
71 | $this->snapshotApp->offsetSet($key, is_object($value) ? clone $value : $value); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | protected function createAppKernel() |
||
76 | { |
||
77 | // Register autoload |
||
78 | self::autoload($this->conf['root_path']); |
||
79 | |||
80 | // Make kernel for Laravel |
||
81 | $app = require $this->conf['root_path'] . '/bootstrap/app.php'; |
||
82 | $kernel = $this->isLumen() ? null : $app->make(HttpKernel::class); |
||
83 | |||
84 | // Load all Configurations for Lumen |
||
85 | if ($this->isLumen()) { |
||
86 | $this->configureLumen($app); |
||
87 | } |
||
88 | |||
89 | // Boot |
||
90 | if ($this->isLumen()) { |
||
91 | if (method_exists($app, 'boot')) { |
||
92 | $app->boot(); |
||
93 | } |
||
94 | } else { |
||
95 | $app->make(ConsoleKernel::class)->bootstrap(); |
||
96 | } |
||
97 | |||
98 | return [$app, $kernel]; |
||
99 | } |
||
100 | |||
101 | protected function configureLumen(Container $app) |
||
102 | { |
||
103 | $cfgPaths = [ |
||
104 | // Framework default configuration |
||
105 | $this->conf['root_path'] . '/vendor/laravel/lumen-framework/config/', |
||
106 | // App configuration |
||
107 | $this->conf['root_path'] . '/config/', |
||
108 | ]; |
||
109 | |||
110 | $keys = []; |
||
111 | foreach ($cfgPaths as $cfgPath) { |
||
112 | $configs = (array)glob($cfgPath . '*.php'); |
||
113 | foreach ($configs as $config) { |
||
114 | $config = substr(basename($config), 0, -4); |
||
115 | $keys[$config] = $config; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | foreach ($keys as $key) { |
||
120 | $app->configure($key); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | public static function autoload($rootPath) |
||
125 | { |
||
126 | $autoload = $rootPath . '/bootstrap/autoload.php'; |
||
127 | if (file_exists($autoload)) { |
||
128 | require_once $autoload; |
||
129 | } else { |
||
130 | require_once $rootPath . '/vendor/autoload.php'; |
||
131 | } |
||
132 | } |
||
133 | |||
134 | public function getRawGlobals() |
||
135 | { |
||
136 | return $this->rawGlobals; |
||
137 | } |
||
138 | |||
139 | public function handleDynamic(IlluminateRequest $request) |
||
140 | { |
||
141 | ob_start(); |
||
142 | |||
143 | if ($this->isLumen()) { |
||
144 | $response = $this->currentApp->dispatch($request); |
||
145 | if ($response instanceof SymfonyResponse) { |
||
146 | $content = $response->getContent(); |
||
147 | } else { |
||
148 | $content = (string)$response; |
||
149 | } |
||
150 | |||
151 | $this->reflectionApp->callTerminableMiddleware($response); |
||
152 | } else { |
||
153 | $response = $this->kernel->handle($request); |
||
154 | $content = $response->getContent(); |
||
155 | $this->kernel->terminate($request, $response); |
||
156 | } |
||
157 | |||
158 | // prefer content in response, secondly ob |
||
159 | if (!($response instanceof StreamedResponse) && strlen($content) === 0 && ob_get_length() > 0) { |
||
160 | $response->setContent(ob_get_contents()); |
||
161 | } |
||
162 | |||
163 | ob_end_clean(); |
||
164 | |||
165 | return $response; |
||
166 | } |
||
167 | |||
168 | public function handleStatic(IlluminateRequest $request) |
||
169 | { |
||
170 | $uri = $request->getRequestUri(); |
||
171 | if (isset(self::$staticBlackList[$uri])) { |
||
172 | return false; |
||
173 | } |
||
174 | $uri = urldecode($uri); |
||
175 | |||
176 | $publicPath = $this->conf['static_path']; |
||
177 | $requestFile = $publicPath . $uri; |
||
178 | if (is_file($requestFile)) { |
||
179 | return $this->createStaticResponse($requestFile, $request); |
||
180 | } elseif (is_dir($requestFile)) { |
||
181 | $indexFile = $this->lookupIndex($requestFile); |
||
182 | if ($indexFile === false) { |
||
183 | return false; |
||
184 | } else { |
||
185 | return $this->createStaticResponse($indexFile, $request); |
||
186 | } |
||
187 | } else { |
||
188 | return false; |
||
189 | } |
||
190 | } |
||
191 | |||
192 | protected function lookupIndex($folder) |
||
202 | } |
||
203 | |||
204 | public function createStaticResponse($requestFile, IlluminateRequest $request) |
||
205 | { |
||
206 | $response = new BinaryFileResponse($requestFile); |
||
207 | $response->prepare($request); |
||
208 | $response->isNotModified($request); |
||
209 | return $response; |
||
210 | } |
||
211 | |||
212 | public function clean() |
||
213 | { |
||
214 | $this->cleanerManager->clean(); |
||
215 | $this->cleanerManager->cleanControllers(); |
||
216 | } |
||
217 | |||
218 | public function cleanProviders() |
||
219 | { |
||
220 | $this->cleanerManager->cleanProviders(); |
||
221 | } |
||
222 | |||
223 | public function fireEvent($name, array $params = []) |
||
224 | { |
||
225 | $params[] = $this->currentApp; |
||
226 | return method_exists($this->currentApp['events'], 'dispatch') ? |
||
227 | $this->currentApp['events']->dispatch($name, $params) : $this->currentApp['events']->fire($name, $params); |
||
228 | } |
||
229 | |||
230 | public function bindRequest(IlluminateRequest $request) |
||
231 | { |
||
232 | $this->currentApp->instance('request', $request); |
||
233 | } |
||
234 | |||
235 | public function bindSwoole($swoole) |
||
236 | { |
||
237 | $this->currentApp->singleton('swoole', function () use ($swoole) { |
||
238 | return $swoole; |
||
239 | }); |
||
240 | } |
||
241 | |||
242 | public function saveSession() |
||
243 | { |
||
244 | if (isset($this->currentApp['session'])) { |
||
245 | $this->currentApp['session']->save(); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | protected function isLumen() |
||
252 | } |
||
253 | } |
||
254 |