Passed
Push — master ( 55a072...f0c027 )
by Biao
09:18
created

Laravel::bindSwoole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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->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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function configureLumen()
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function autoload()
Loading history...
125
    {
126
        $autoload = $rootPath . '/bootstrap/autoload.php';
127
        if (file_exists($autoload)) {
128
            require_once $autoload;
0 ignored issues
show
Coding Style introduced by
File is being conditionally included; use "include_once" instead
Loading history...
129
        } else {
130
            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...
131
        }
132
    }
133
134
    public function getRawGlobals()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getRawGlobals()
Loading history...
135
    {
136
        return $this->rawGlobals;
137
    }
138
139
    public function handleDynamic(IlluminateRequest $request)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function handleDynamic()
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function handleStatic()
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function lookupIndex()
Loading history...
193
    {
194
        $folder = rtrim($folder, '/') . '/';
195
        foreach (['index.html', 'index.htm'] as $index) {
196
            $tmpFile = $folder . $index;
197
            if (is_file($tmpFile)) {
198
                return $tmpFile;
199
            }
200
        }
201
        return false;
202
    }
203
204
    public function createStaticResponse($requestFile, IlluminateRequest $request)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function createStaticResponse()
Loading history...
205
    {
206
        $response = new BinaryFileResponse($requestFile);
207
        $response->prepare($request);
208
        $response->isNotModified($request);
209
        return $response;
210
    }
211
212
    public function clean()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function clean()
Loading history...
213
    {
214
        $this->cleanerManager->clean();
215
        $this->cleanerManager->cleanControllers();
216
    }
217
218
    public function cleanProviders()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function cleanProviders()
Loading history...
219
    {
220
        $this->cleanerManager->cleanProviders();
221
    }
222
223
    public function fireEvent($name, array $params = [])
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function fireEvent()
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function bindRequest()
Loading history...
231
    {
232
        $this->currentApp->instance('request', $request);
233
    }
234
235
    public function bindSwoole($swoole)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function bindSwoole()
Loading history...
236
    {
237
        $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...
238
            return $swoole;
239
        });
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...
240
    }
241
242
    public function saveSession()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function saveSession()
Loading history...
243
    {
244
        if (isset($this->currentApp['session'])) {
245
            $this->currentApp['session']->save();
246
        }
247
    }
248
249
    protected function isLumen()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function isLumen()
Loading history...
250
    {
251
        return $this->conf['is_lumen'];
252
    }
253
}
254