Total Complexity | 40 |
Total Lines | 231 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 3 | Features | 0 |
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' => true, |
||
33 | '/.htaccess' => true, |
||
34 | '/web.config' => true, |
||
35 | ]; |
||
36 | /**@var array */ |
||
37 | protected static $staticIndexList = [ |
||
38 | 'index.html', |
||
39 | ]; |
||
40 | |||
41 | /**@var array */ |
||
42 | private $rawGlobals = []; |
||
43 | |||
44 | /**@var CleanerManager */ |
||
45 | protected $cleanerManager; |
||
46 | |||
47 | public function __construct(array $conf = []) |
||
48 | { |
||
49 | $this->conf = $conf; |
||
50 | |||
51 | // Merge $_ENV $_SERVER |
||
52 | $this->rawGlobals['_SERVER'] = $_SERVER + $this->conf['_SERVER']; |
||
53 | $this->rawGlobals['_ENV'] = $_ENV + $this->conf['_ENV']; |
||
54 | } |
||
55 | |||
56 | public function prepareLaravel() |
||
57 | { |
||
58 | list($this->currentApp, $this->kernel) = $this->createAppKernel(); |
||
59 | |||
60 | $this->reflectionApp = new ReflectionApp($this->currentApp); |
||
61 | |||
62 | $this->saveSnapshot(); |
||
63 | |||
64 | // Create cleaner manager |
||
65 | $this->cleanerManager = new CleanerManager($this->currentApp, $this->snapshotApp, $this->conf); |
||
66 | } |
||
67 | |||
68 | protected function saveSnapshot() |
||
69 | { |
||
70 | $this->snapshotApp = clone $this->currentApp; |
||
71 | |||
72 | $instances = $this->reflectionApp->instances(); |
||
73 | |||
74 | foreach ($instances as $key => $value) { |
||
75 | $this->snapshotApp->offsetSet($key, is_object($value) ? clone $value : $value); |
||
76 | } |
||
77 | } |
||
78 | |||
79 | protected function createAppKernel() |
||
80 | { |
||
81 | // Register autoload |
||
82 | self::autoload($this->conf['root_path']); |
||
83 | |||
84 | // Make kernel for Laravel |
||
85 | $app = require $this->conf['root_path'] . '/bootstrap/app.php'; |
||
86 | $kernel = $this->conf['is_lumen'] ? null : $app->make(HttpKernel::class); |
||
87 | |||
88 | // Boot |
||
89 | if ($this->conf['is_lumen']) { |
||
90 | $this->configureLumen($app); |
||
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() |
||
137 | } |
||
138 | |||
139 | public function handleDynamic(IlluminateRequest $request) |
||
140 | { |
||
141 | ob_start(); |
||
142 | |||
143 | if ($this->conf['is_lumen']) { |
||
144 | $response = $this->currentApp->dispatch($request); |
||
145 | if ($response instanceof SymfonyResponse) { |
||
146 | $content = $response->getContent(); |
||
147 | } else { |
||
148 | $content = $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) && (string)$content === '' && 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 | $uri = (string)str_replace("\0", '', urldecode($uri)); |
||
172 | if (isset(self::$staticBlackList[$uri]) || strpos($uri, '/..') !== false) { |
||
173 | return false; |
||
174 | } |
||
175 | |||
176 | $requestFile = $this->conf['static_path'] . $uri; |
||
177 | if (is_file($requestFile)) { |
||
178 | return $this->createStaticResponse($requestFile, $request); |
||
179 | } |
||
180 | if (is_dir($requestFile)) { |
||
181 | $indexFile = $this->lookupIndex($requestFile); |
||
182 | if ($indexFile === false) { |
||
183 | return false; |
||
184 | } |
||
185 | return $this->createStaticResponse($indexFile, $request); |
||
186 | } |
||
187 | return false; |
||
188 | } |
||
189 | |||
190 | protected function lookupIndex($folder) |
||
191 | { |
||
192 | $folder = rtrim($folder, '/') . '/'; |
||
193 | foreach (self::$staticIndexList as $index) { |
||
194 | $tmpFile = $folder . $index; |
||
195 | if (is_file($tmpFile)) { |
||
196 | return $tmpFile; |
||
197 | } |
||
198 | } |
||
199 | return false; |
||
200 | } |
||
201 | |||
202 | public function createStaticResponse($requestFile, IlluminateRequest $request) |
||
203 | { |
||
204 | $response = new BinaryFileResponse($requestFile); |
||
205 | $response->prepare($request); |
||
206 | $response->isNotModified($request); |
||
207 | return $response; |
||
208 | } |
||
209 | |||
210 | public function clean() |
||
211 | { |
||
212 | $this->cleanerManager->clean(); |
||
213 | $this->cleanerManager->cleanControllers(); |
||
214 | } |
||
215 | |||
216 | public function cleanProviders() |
||
217 | { |
||
218 | $this->cleanerManager->cleanProviders(); |
||
219 | } |
||
220 | |||
221 | public function fireEvent($name, array $params = []) |
||
222 | { |
||
223 | $params[] = $this->currentApp; |
||
224 | return method_exists($this->currentApp['events'], 'dispatch') ? |
||
225 | $this->currentApp['events']->dispatch($name, $params) : $this->currentApp['events']->fire($name, $params); |
||
226 | } |
||
227 | |||
228 | public function bindRequest(IlluminateRequest $request) |
||
229 | { |
||
230 | $this->currentApp->instance('request', $request); |
||
231 | } |
||
232 | |||
233 | public function bindSwoole($swoole) |
||
234 | { |
||
235 | $this->currentApp->singleton('swoole', function () use ($swoole) { |
||
236 | return $swoole; |
||
237 | }); |
||
238 | } |
||
239 | |||
240 | public function saveSession() |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 |