Total Complexity | 57 |
Total Lines | 280 |
Duplicated Lines | 0 % |
Changes | 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 $app; |
||
17 | |||
18 | /**@var HttpKernel */ |
||
19 | protected $kernel; |
||
20 | |||
21 | /**@var array */ |
||
22 | protected $snapshots = []; |
||
23 | |||
24 | /**@var array */ |
||
25 | protected $conf = []; |
||
26 | |||
27 | /**@var array */ |
||
28 | protected static $staticBlackList = [ |
||
29 | '/index.php' => 1, |
||
30 | '/.htaccess' => 1, |
||
31 | '/web.config' => 1, |
||
32 | ]; |
||
33 | |||
34 | /**@var array */ |
||
35 | private $rawGlobals = []; |
||
36 | |||
37 | public function __construct(array $conf = []) |
||
38 | { |
||
39 | $this->conf = $conf; |
||
40 | } |
||
41 | |||
42 | public function prepareLaravel() |
||
43 | { |
||
44 | static::autoload($this->conf['root_path']); |
||
45 | $this->createApp(); |
||
46 | $this->createKernel(); |
||
47 | $this->setLaravel(); |
||
48 | $this->loadAllConfigurations(); |
||
49 | $this->bootstrap(); |
||
50 | $this->saveSnapshots(); |
||
51 | } |
||
52 | |||
53 | public static function autoload($rootPath) |
||
54 | { |
||
55 | $autoload = $rootPath . '/bootstrap/autoload.php'; |
||
56 | if (file_exists($autoload)) { |
||
57 | require_once $autoload; |
||
58 | } else { |
||
59 | require_once $rootPath . '/vendor/autoload.php'; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | protected function createApp() |
||
66 | } |
||
67 | |||
68 | protected function createKernel() |
||
72 | } |
||
73 | } |
||
74 | |||
75 | protected function setLaravel() |
||
76 | { |
||
77 | // Load configuration laravel.php manually for Lumen |
||
78 | if ($this->conf['is_lumen'] && file_exists($this->conf['root_path'] . '/config/laravels.php')) { |
||
79 | $this->app->configure('laravels'); |
||
|
|||
80 | } |
||
81 | |||
82 | $server = isset($this->conf['_SERVER']) ? $this->conf['_SERVER'] : []; |
||
83 | $env = isset($this->conf['_ENV']) ? $this->conf['_ENV'] : []; |
||
84 | $this->rawGlobals['_SERVER'] = array_merge($_SERVER, $server); |
||
85 | $this->rawGlobals['_ENV'] = array_merge($_ENV, $env); |
||
86 | } |
||
87 | |||
88 | protected function bootstrap() |
||
89 | { |
||
90 | if ($this->conf['is_lumen']) { |
||
91 | if (method_exists($this->app, 'boot')) { |
||
92 | $this->app->boot(); |
||
93 | } |
||
94 | } else { |
||
95 | $this->app->make(ConsoleKernel::class)->bootstrap(); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | public function loadAllConfigurations() |
||
100 | { |
||
101 | if (!$this->conf['is_lumen']) { |
||
102 | return; |
||
103 | } |
||
104 | |||
105 | $cfgPaths = [ |
||
106 | // Framework default configuration |
||
107 | $this->conf['root_path'] . '/vendor/laravel/lumen-framework/config/', |
||
108 | // App configuration |
||
109 | $this->conf['root_path'] . '/config/', |
||
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 | foreach ($keys as $key) { |
||
120 | $this->app->configure($key); |
||
121 | } |
||
122 | } |
||
123 | |||
124 | protected function saveSnapshots() |
||
125 | { |
||
126 | $this->snapshots['config'] = $this->app['config']->all(); |
||
127 | } |
||
128 | |||
129 | protected function applySnapshots() |
||
130 | { |
||
131 | $this->app['config']->set($this->snapshots['config']); |
||
132 | if (isset($this->app['cookie'])) { |
||
133 | foreach ($this->app['cookie']->getQueuedCookies() as $name => $cookie) { |
||
134 | $this->app['cookie']->unqueue($name); |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | public function getRawGlobals() |
||
142 | } |
||
143 | |||
144 | public function handleDynamic(IlluminateRequest $request) |
||
145 | { |
||
146 | $this->applySnapshots(); |
||
147 | |||
148 | ob_start(); |
||
149 | |||
150 | if ($this->conf['is_lumen']) { |
||
151 | $response = $this->app->dispatch($request); |
||
152 | if ($response instanceof SymfonyResponse) { |
||
153 | $content = $response->getContent(); |
||
154 | } else { |
||
155 | $content = (string)$response; |
||
156 | } |
||
157 | |||
158 | $laravelReflect = new \ReflectionObject($this->app); |
||
159 | $middleware = $laravelReflect->getProperty('middleware'); |
||
160 | $middleware->setAccessible(true); |
||
161 | if (!empty($middleware->getValue($this->app))) { |
||
162 | $callTerminableMiddleware = $laravelReflect->getMethod('callTerminableMiddleware'); |
||
163 | $callTerminableMiddleware->setAccessible(true); |
||
164 | $callTerminableMiddleware->invoke($this->app, $response); |
||
165 | } |
||
166 | } else { |
||
167 | $response = $this->kernel->handle($request); |
||
168 | $content = $response->getContent(); |
||
169 | $this->kernel->terminate($request, $response); |
||
170 | } |
||
171 | |||
172 | // prefer content in response, secondly ob |
||
173 | if (strlen($content) === 0 && ob_get_length() > 0) { |
||
174 | $response->setContent(ob_get_contents()); |
||
175 | } |
||
176 | |||
177 | ob_end_clean(); |
||
178 | |||
179 | return $response; |
||
180 | } |
||
181 | |||
182 | public function handleStatic(IlluminateRequest $request) |
||
183 | { |
||
184 | $uri = $request->getRequestUri(); |
||
185 | if (isset(self::$staticBlackList[$uri])) { |
||
186 | return false; |
||
187 | } |
||
188 | |||
189 | $publicPath = $this->conf['static_path']; |
||
190 | $requestFile = $publicPath . $uri; |
||
191 | if (is_file($requestFile)) { |
||
192 | return $this->createStaticResponse($requestFile, $request); |
||
193 | } elseif (is_dir($requestFile)) { |
||
194 | $indexFile = $this->lookupIndex($requestFile); |
||
195 | if ($indexFile === false) { |
||
196 | return false; |
||
197 | } else { |
||
198 | return $this->createStaticResponse($indexFile, $request); |
||
199 | } |
||
200 | } else { |
||
201 | return false; |
||
202 | } |
||
203 | } |
||
204 | |||
205 | protected function lookupIndex($folder) |
||
206 | { |
||
207 | $folder = rtrim($folder, '/') . '/'; |
||
208 | foreach (['index.html', 'index.htm'] as $index) { |
||
209 | $tmpFile = $folder . $index; |
||
210 | if (is_file($tmpFile)) { |
||
211 | return $tmpFile; |
||
212 | } |
||
213 | } |
||
214 | return false; |
||
215 | } |
||
216 | |||
217 | public function createStaticResponse($requestFile, IlluminateRequest $request) |
||
218 | { |
||
219 | $response = new BinaryFileResponse($requestFile); |
||
220 | $response->prepare($request); |
||
221 | $response->isNotModified($request); |
||
222 | return $response; |
||
223 | } |
||
224 | |||
225 | protected function cleanProviders(array $providers, $force = false) |
||
245 | } |
||
246 | } |
||
247 | |||
248 | public function clean() |
||
249 | { |
||
250 | foreach ($this->conf['cleaners'] as $cleanerCls) { |
||
251 | /**@var \Hhxsv5\LaravelS\Illuminate\Cleaners\CleanerInterface $cleaner */ |
||
252 | $cleaner = $this->app->make($cleanerCls); |
||
253 | $cleaner->clean($this->app); |
||
254 | } |
||
255 | $this->cleanProviders($this->conf['register_providers']); |
||
256 | } |
||
257 | |||
258 | public function fireEvent($name, array $params = []) |
||
259 | { |
||
260 | $params[] = $this->app; |
||
261 | return method_exists('dispatch', $this->app['events']) ? |
||
262 | $this->app['events']->dispatch($name, $params) : $this->app['events']->fire($name, $params); |
||
263 | } |
||
264 | |||
265 | public function bindRequest(IlluminateRequest $request) |
||
266 | { |
||
267 | $this->app->instance('request', $request); |
||
268 | } |
||
269 | |||
270 | public function bindSwoole($swoole) |
||
271 | { |
||
272 | $this->app->singleton('swoole', function () use ($swoole) { |
||
273 | return $swoole; |
||
274 | }); |
||
275 | } |
||
276 | |||
277 | public function make($abstract, array $parameters = []) |
||
280 | } |
||
281 | |||
282 | public function cleanSession() |
||
283 | { |
||
284 | /**@var \Hhxsv5\LaravelS\Illuminate\Cleaners\CleanerInterface $cleaner */ |
||
285 | $cleaner = $this->app->make(SessionCleaner::class); |
||
286 | $cleaner->clean($this->app); |
||
287 | } |
||
288 | |||
289 | public function saveSession() |
||
293 | } |
||
294 | } |
||
295 | } |
||
296 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.