Passed
Push — master ( d9a6d1...03a9e3 )
by Biao
04:01
created

Laravel::isLumen()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Illuminate;
4
5
use Illuminate\Container\Container;
0 ignored issues
show
Bug introduced by
The type Illuminate\Container\Container was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Console\Kernel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
use Illuminate\Contracts\Http\Kernel as HttpKernel;
0 ignored issues
show
Bug introduced by
The type Illuminate\Contracts\Http\Kernel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Illuminate\Http\Request as IlluminateRequest;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\HttpFoundation\BinaryFileResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFo...tion\BinaryFileResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Symfony\Component\HttpFoundation\StreamedResponse;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\StreamedResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
13
class Laravel
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Laravel
Loading history...
14
{
15
    /**@var Container */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
16
    protected $currentApp;
17
18
    /**@var Container */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
19
    protected $snapshotApp;
20
21
    /**@var ReflectionApp */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
22
    protected $reflectionApp;
23
24
    /**@var HttpKernel */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
25
    protected $kernel;
26
27
    /**@var array */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
28
    protected $conf = [];
29
30
    /**@var array */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
31
    protected static $staticBlackList = [
32
        '/index.php'  => 1,
33
        '/.htaccess'  => 1,
34
        '/web.config' => 1,
35
    ];
36
37
    /**@var array */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
38
    private $rawGlobals = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "rawGlobals" must be prefixed with an underscore
Loading history...
39
40
    /**@var CleanerManager */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
41
    protected $cleanerManager;
42
43
    public function __construct(array $conf = [])
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function prepareLaravel()
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function saveSnapshot()
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createAppKernel()
Loading history...
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';
0 ignored issues
show
Coding Style introduced by
File is being conditionally included; use "include" instead
Loading history...
82
        $kernel = $this->conf['is_lumen'] ? null : $app->make(HttpKernel::class);
83
84
        // Boot
85
        if ($this->conf['is_lumen']) {
86
            $this->configureLumen($app);
87
            if (method_exists($app, 'boot')) {
88
                $app->boot();
89
            }
90
        } else {
91
            $app->make(ConsoleKernel::class)->bootstrap();
92
        }
93
94
        return [$app, $kernel];
95
    }
96
97
    protected function configureLumen(Container $app)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function configureLumen()
Loading history...
98
    {
99
        $cfgPaths = [
100
            // Framework default configuration
101
            $this->conf['root_path'] . '/vendor/laravel/lumen-framework/config/',
102
            // App configuration
103
            $this->conf['root_path'] . '/config/',
104
        ];
105
106
        $keys = [];
107
        foreach ($cfgPaths as $cfgPath) {
108
            $configs = (array)glob($cfgPath . '*.php');
109
            foreach ($configs as $config) {
110
                $config = substr(basename($config), 0, -4);
111
                $keys[$config] = $config;
112
            }
113
        }
114
115
        foreach ($keys as $key) {
116
            $app->configure($key);
117
        }
118
    }
119
120
    public static function autoload($rootPath)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function autoload()
Loading history...
121
    {
122
        $autoload = $rootPath . '/bootstrap/autoload.php';
123
        if (file_exists($autoload)) {
124
            require_once $autoload;
0 ignored issues
show
Coding Style introduced by
File is being conditionally included; use "include_once" instead
Loading history...
125
        } else {
126
            require_once $rootPath . '/vendor/autoload.php';
0 ignored issues
show
Coding Style introduced by
File is being conditionally included; use "include_once" instead
Loading history...
127
        }
128
    }
129
130
    public function getRawGlobals()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRawGlobals()
Loading history...
131
    {
132
        return $this->rawGlobals;
133
    }
134
135
    public function handleDynamic(IlluminateRequest $request)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function handleDynamic()
Loading history...
136
    {
137
        ob_start();
138
139
        if ($this->conf['is_lumen']) {
140
            $response = $this->currentApp->dispatch($request);
141
            if ($response instanceof SymfonyResponse) {
142
                $content = $response->getContent();
143
            } else {
144
                $content = $response;
145
            }
146
147
            $this->reflectionApp->callTerminableMiddleware($response);
148
        } else {
149
            $response = $this->kernel->handle($request);
150
            $content = $response->getContent();
151
            $this->kernel->terminate($request, $response);
152
        }
153
154
        // prefer content in response, secondly ob
155
        if (!($response instanceof StreamedResponse) && (string)$content === '' && ob_get_length() > 0) {
156
            $response->setContent(ob_get_contents());
157
        }
158
159
        ob_end_clean();
160
161
        return $response;
162
    }
163
164
    public function handleStatic(IlluminateRequest $request)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function handleStatic()
Loading history...
165
    {
166
        $uri = $request->getRequestUri();
167
        if (isset(self::$staticBlackList[$uri])) {
168
            return false;
169
        }
170
        $uri = urldecode($uri);
171
172
        $publicPath = $this->conf['static_path'];
173
        $requestFile = $publicPath . $uri;
174
        if (is_file($requestFile)) {
175
            return $this->createStaticResponse($requestFile, $request);
176
        }
177
        if (is_dir($requestFile)) {
178
            $indexFile = $this->lookupIndex($requestFile);
179
            if ($indexFile === false) {
180
                return false;
181
            }
182
            return $this->createStaticResponse($indexFile, $request);
183
        }
184
        return false;
185
    }
186
187
    protected function lookupIndex($folder)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function lookupIndex()
Loading history...
188
    {
189
        $folder = rtrim($folder, '/') . '/';
190
        foreach (['index.html', 'index.htm'] as $index) {
191
            $tmpFile = $folder . $index;
192
            if (is_file($tmpFile)) {
193
                return $tmpFile;
194
            }
195
        }
196
        return false;
197
    }
198
199
    public function createStaticResponse($requestFile, IlluminateRequest $request)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createStaticResponse()
Loading history...
200
    {
201
        $response = new BinaryFileResponse($requestFile);
202
        $response->prepare($request);
203
        $response->isNotModified($request);
204
        return $response;
205
    }
206
207
    public function clean()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function clean()
Loading history...
208
    {
209
        $this->cleanerManager->clean();
210
        $this->cleanerManager->cleanControllers();
211
    }
212
213
    public function cleanProviders()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function cleanProviders()
Loading history...
214
    {
215
        $this->cleanerManager->cleanProviders();
216
    }
217
218
    public function fireEvent($name, array $params = [])
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function fireEvent()
Loading history...
219
    {
220
        $params[] = $this->currentApp;
221
        return method_exists($this->currentApp['events'], 'dispatch') ?
222
            $this->currentApp['events']->dispatch($name, $params) : $this->currentApp['events']->fire($name, $params);
223
    }
224
225
    public function bindRequest(IlluminateRequest $request)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function bindRequest()
Loading history...
226
    {
227
        $this->currentApp->instance('request', $request);
228
    }
229
230
    public function bindSwoole($swoole)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function bindSwoole()
Loading history...
231
    {
232
        $this->currentApp->singleton('swoole', function () use ($swoole) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
233
            return $swoole;
234
        });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
235
    }
236
237
    public function saveSession()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function saveSession()
Loading history...
238
    {
239
        if (isset($this->currentApp['session'])) {
240
            $this->currentApp['session']->save();
241
        }
242
    }
243
}
244