Test Failed
Pull Request — master (#448)
by Biao
13:27
created

Laravel::bindSwooleHttpResponse()   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 1
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 Swoole\Http\Response as SwooleResponse;
10
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...
11
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...
12
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...
13
14
class Laravel
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Laravel
Loading history...
15
{
16
    /**@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...
17
    protected $currentApp;
18
19
    /**@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...
20
    protected $snapshotApp;
21
22
    /**@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...
23
    protected $reflectionApp;
24
25
    /**@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...
26
    protected $kernel;
27
28
    /**@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...
29
    protected $conf = [];
30
31
    /**@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...
32
    protected static $staticBlackList = [
33
        '/index.php'  => true,
34
        '/.htaccess'  => true,
35
        '/web.config' => true,
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
    protected static $staticIndexList = [
39
        'index.html',
40
    ];
41
42
    /**@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...
43
    private $rawGlobals = [];
0 ignored issues
show
Coding Style introduced by
Private member variable "rawGlobals" must be prefixed with an underscore
Loading history...
44
45
    /**@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...
46
    protected $cleanerManager;
47
48
    public function __construct(array $conf = [])
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function prepareLaravel()
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function saveSnapshot()
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createAppKernel()
Loading history...
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';
0 ignored issues
show
Coding Style introduced by
File is being conditionally included; use "include" instead
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function configureLumen()
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function autoload()
Loading history...
126
    {
127
        $autoload = $rootPath . '/bootstrap/autoload.php';
128
        if (file_exists($autoload)) {
129
            require_once $autoload;
0 ignored issues
show
Coding Style introduced by
File is being conditionally included; use "include_once" instead
Loading history...
130
        } else {
131
            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...
132
        }
133
    }
134
135
    public function getRawGlobals()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRawGlobals()
Loading history...
136
    {
137
        return $this->rawGlobals;
138
    }
139
140
    public function handleDynamic(IlluminateRequest $request)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function handleDynamic()
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function handleStatic()
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function lookupIndex()
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createStaticResponse()
Loading history...
204
    {
205
        $response = new BinaryFileResponse($requestFile);
206
        $response->prepare($request);
207
        $response->isNotModified($request);
208
        return $response;
209
    }
210
211
    public function clean()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function clean()
Loading history...
212
    {
213
        $this->cleanerManager->clean();
214
        $this->cleanerManager->cleanControllers();
215
    }
216
217
    public function cleanProviders()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function cleanProviders()
Loading history...
218
    {
219
        $this->cleanerManager->cleanProviders();
220
    }
221
222
    public function fireEvent($name, array $params = [])
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function fireEvent()
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function bindRequest()
Loading history...
230
    {
231
        $this->currentApp->instance('request', $request);
232
    }
233
234
    public function bindSwoole($swoole)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function bindSwoole()
Loading history...
235
    {
236
        $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...
237
            return $swoole;
238
        });
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...
239
    }
240
241
    public function bindSwooleHttpResponse(SwooleResponse $response)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function bindSwooleHttpResponse()
Loading history...
242
    {
243
        $this->currentApp->instance('swoole-http-response', $response);
244
    }
245
246
    public function saveSession()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function saveSession()
Loading history...
247
    {
248
        if (isset($this->currentApp['session'])) {
249
            $this->currentApp['session']->save();
250
        }
251
    }
252
}
253