Passed
Pull Request — PHP-7.x (#489)
by
unknown
03:52
created

LaravelS::__construct()   B

Complexity

Conditions 9
Paths 192

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 21
c 0
b 0
f 0
nc 192
nop 2
dl 0
loc 32
rs 7.2888
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS;
4
5
use Hhxsv5\LaravelS\Illuminate\Laravel;
6
use Hhxsv5\LaravelS\Illuminate\LaravelTrait;
7
use Hhxsv5\LaravelS\Illuminate\LogTrait;
8
use Hhxsv5\LaravelS\Swoole\DynamicResponse;
9
use Hhxsv5\LaravelS\Swoole\Events\ServerStartInterface;
10
use Hhxsv5\LaravelS\Swoole\Events\ServerStopInterface;
11
use Hhxsv5\LaravelS\Swoole\Events\WorkerErrorInterface;
12
use Hhxsv5\LaravelS\Swoole\Events\WorkerStartInterface;
13
use Hhxsv5\LaravelS\Swoole\Events\WorkerStopInterface;
14
use Hhxsv5\LaravelS\Swoole\InotifyTrait;
15
use Hhxsv5\LaravelS\Swoole\ChokidarWatchTrait;
16
use Hhxsv5\LaravelS\Swoole\Process\CustomProcessTrait;
17
use Hhxsv5\LaravelS\Swoole\Process\ProcessTitleTrait;
18
use Hhxsv5\LaravelS\Swoole\Request;
19
use Hhxsv5\LaravelS\Swoole\Server;
20
use Hhxsv5\LaravelS\Swoole\StaticResponse;
21
use Hhxsv5\LaravelS\Swoole\Timer\TimerTrait;
22
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...
23
use Swoole\Http\Request as SwooleRequest;
24
use Swoole\Http\Response as SwooleResponse;
25
use Swoole\Http\Server as HttpServer;
26
use Swoole\Server\Port;
27
use Symfony\Component\Console\Style\OutputStyle;
28
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...
29
30
31
/**
32
 * Swoole Request => Laravel Request
33
 * Laravel Request => Laravel handle => Laravel Response
34
 * Laravel Response => Swoole Response
35
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
36
class LaravelS extends Server
37
{
38
    /**
39
     * Fix conflicts of traits
40
     */
41
    use InotifyTrait, ChokidarWatchTrait, LaravelTrait, LogTrait, ProcessTitleTrait, TimerTrait, CustomProcessTrait;
0 ignored issues
show
introduced by
The trait Hhxsv5\LaravelS\Swoole\Process\CustomProcessTrait requires some properties which are not provided by Hhxsv5\LaravelS\LaravelS: $setting, $pid
Loading history...
introduced by
The trait Hhxsv5\LaravelS\Swoole\Timer\TimerTrait requires some properties which are not provided by Hhxsv5\LaravelS\LaravelS: $setting, $pid
Loading history...
42
43
    /**@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...
44
    protected $laravelConf;
45
46
    /**@var Laravel */
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...
47
    protected $laravel;
48
49
    public function __construct(array $svrConf, array $laravelConf)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
50
    {
51
        parent::__construct($svrConf);
52
        $this->laravelConf = $laravelConf;
53
54
        $timerConf = isset($this->conf['timer']) ? $this->conf['timer'] : [];
55
        $timerConf['process_prefix'] = $svrConf['process_prefix'];
56
        $this->swoole->timerProcess = $this->addTimerProcess($this->swoole, $timerConf, $this->laravelConf);
0 ignored issues
show
Bug introduced by
The property timerProcess does not seem to exist on Swoole\Http\Server.
Loading history...
Bug introduced by
The property timerProcess does not seem to exist on Swoole\WebSocket\Server.
Loading history...
57
58
        $inotifyConf = isset($this->conf['inotify_reload']) ? $this->conf['inotify_reload'] : [];
59
        if (!isset($inotifyConf['watch_path'])) {
60
            $inotifyConf['watch_path'] = $this->laravelConf['root_path'];
61
        }
62
        $inotifyConf['process_prefix'] = $svrConf['process_prefix'];
63
        $this->swoole->inotifyProcess = $this->addInotifyProcess($this->swoole, $inotifyConf, $this->laravelConf);
0 ignored issues
show
Bug introduced by
The property inotifyProcess does not seem to exist on Swoole\WebSocket\Server.
Loading history...
Bug introduced by
The property inotifyProcess does not seem to exist on Swoole\Http\Server.
Loading history...
64
65
        $chokidarConf = isset($this->conf['chokidar_reload']) ? $this->conf['chokidar_reload'] : [];
66
        if (!empty($chokidarConf['enable'])) {
67
            if (!isset($chokidarConf['watch_path'])) {
68
                $chokidarConf['watch_path'] = $this->laravelConf['root_path'];
69
            }
70
            $chokidarConf['process_prefix'] = $svrConf['process_prefix'];
71
            $this->swoole->chokidarProcess = $this->addChokidarWatchProcess($this->swoole, $chokidarConf, $this->laravelConf);
0 ignored issues
show
Bug introduced by
The property chokidarProcess does not seem to exist on Swoole\WebSocket\Server.
Loading history...
Bug introduced by
The property chokidarProcess does not seem to exist on Swoole\Http\Server.
Loading history...
72
        }
73
74
        $processes = isset($this->conf['processes']) ? $this->conf['processes'] : [];
75
        $this->swoole->customProcesses = $this->addCustomProcesses($this->swoole, $svrConf['process_prefix'], $processes, $this->laravelConf);
0 ignored issues
show
Bug introduced by
The property customProcesses does not seem to exist on Swoole\WebSocket\Server.
Loading history...
Bug introduced by
The property customProcesses does not seem to exist on Swoole\Http\Server.
Loading history...
76
77
        // Fire ServerStart event
78
        if (isset($this->conf['event_handlers']['ServerStart'])) {
79
            Laravel::autoload($this->laravelConf['root_path']);
80
            $this->fireEvent('ServerStart', ServerStartInterface::class, [$this->swoole]);
81
        }
82
    }
83
84
    protected function beforeWebSocketHandShake(SwooleRequest $request)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function beforeWebSocketHandShake()
Loading history...
85
    {
86
        // Start Laravel's lifetime, then support session ...middleware.
87
        $laravelRequest = $this->convertRequest($this->laravel, $request);
88
        $this->laravel->bindRequest($laravelRequest);
89
        $this->laravel->fireEvent('laravels.received_request', [$laravelRequest]);
90
        $this->laravel->cleanProviders();
91
        $laravelResponse = $this->laravel->handleDynamic($laravelRequest);
92
        $this->laravel->fireEvent('laravels.generated_response', [$laravelRequest, $laravelResponse]);
93
    }
94
95
    protected function afterWebSocketOpen(SwooleRequest $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

95
    protected function afterWebSocketOpen(/** @scrutinizer ignore-unused */ SwooleRequest $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Missing doc comment for function afterWebSocketOpen()
Loading history...
96
    {
97
        // End Laravel's lifetime.
98
        $this->laravel->saveSession();
99
        $this->laravel->clean();
100
    }
101
102
    protected function triggerWebSocketEvent($event, array $params)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function triggerWebSocketEvent()
Loading history...
103
    {
104
        if ($event === 'onHandShake') {
105
            $this->beforeWebSocketHandShake($params[0]);
106
            if (!empty($this->conf['server'])) {
107
                $params[1]->header('Server', $this->conf['server']);
108
            }
109
        }
110
111
        parent::triggerWebSocketEvent($event, $params);
112
113
        switch ($event) {
114
            case 'onHandShake':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
115
                if (isset($params[1]->header['Sec-Websocket-Accept'])) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
116
                    // Successful handshake
117
                    parent::triggerWebSocketEvent('onOpen', [$this->swoole, $params[0]]);
118
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
119
                $this->afterWebSocketOpen($params[0]);
120
                break;
121
            case 'onOpen':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
122
                $this->afterWebSocketOpen($params[1]);
123
                break;
124
        }
125
    }
126
127
    protected function triggerPortEvent(Port $port, $handlerClass, $event, array $params)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function triggerPortEvent()
Loading history...
128
    {
129
        switch ($event) {
130
            case 'onHandShake':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
131
                $this->beforeWebSocketHandShake($params[0]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment if this fall-through is intended.
Loading history...
132
            case 'onRequest':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
133
                if (!empty($this->conf['server'])) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
134
                    $params[1]->header('Server', $this->conf['server']);
135
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
136
                break;
137
        }
138
139
        parent::triggerPortEvent($port, $handlerClass, $event, $params);
140
141
        switch ($event) {
142
            case 'onHandShake':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
143
                if (isset($params[1]->header['Sec-Websocket-Accept'])) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
144
                    // Successful handshake
145
                    parent::triggerPortEvent($port, $handlerClass, 'onOpen', [$this->swoole, $params[0]]);
146
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
147
                $this->afterWebSocketOpen($params[0]);
148
                break;
149
            case 'onOpen':
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
150
                $this->afterWebSocketOpen($params[1]);
151
                break;
152
        }
153
    }
154
155
    public function onShutdown(HttpServer $server)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function onShutdown()
Loading history...
156
    {
157
        parent::onShutdown($server);
158
159
        // Fire ServerStop event
160
        if (isset($this->conf['event_handlers']['ServerStop'])) {
161
            $this->laravel = $this->initLaravel($this->laravelConf, $this->swoole);
162
            $this->fireEvent('ServerStop', ServerStopInterface::class, [$server]);
163
        }
164
    }
165
166
    public function onWorkerStart(HttpServer $server, $workerId)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function onWorkerStart()
Loading history...
167
    {
168
        parent::onWorkerStart($server, $workerId);
169
170
        // To implement gracefully reload
171
        // Delay to create Laravel
172
        // Delay to include Laravel's autoload.php
173
        $this->laravel = $this->initLaravel($this->laravelConf, $this->swoole);
174
175
        // Fire WorkerStart event
176
        $this->fireEvent('WorkerStart', WorkerStartInterface::class, func_get_args());
177
    }
178
179
    public function onWorkerStop(HttpServer $server, $workerId)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function onWorkerStop()
Loading history...
180
    {
181
        parent::onWorkerStop($server, $workerId);
182
183
        // Fire WorkerStop event
184
        $this->fireEvent('WorkerStop', WorkerStopInterface::class, func_get_args());
185
    }
186
187
    public function onWorkerError(HttpServer $server, $workerId, $workerPId, $exitCode, $signal)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function onWorkerError()
Loading history...
188
    {
189
        parent::onWorkerError($server, $workerId, $workerPId, $exitCode, $signal);
190
191
        Laravel::autoload($this->laravelConf['root_path']);
192
193
        // Fire WorkerError event
194
        $this->fireEvent('WorkerError', WorkerErrorInterface::class, func_get_args());
195
    }
196
197
    protected function convertRequest(Laravel $laravel, SwooleRequest $request)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function convertRequest()
Loading history...
198
    {
199
        $rawGlobals = $laravel->getRawGlobals();
200
        return (new Request($request))->toIlluminateRequest($rawGlobals['_SERVER'], $rawGlobals['_ENV']);
201
    }
202
203
    public function onRequest(SwooleRequest $swooleRequest, SwooleResponse $swooleResponse)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function onRequest()
Loading history...
204
    {
205
        try {
206
            $laravelRequest = $this->convertRequest($this->laravel, $swooleRequest);
207
            $this->laravel->bindRequest($laravelRequest);
208
            $this->laravel->fireEvent('laravels.received_request', [$laravelRequest]);
209
            $handleStaticSuccess = false;
210
            if ($this->conf['handle_static']) {
211
                // For Swoole < 1.9.17
212
                $handleStaticSuccess = $this->handleStaticResource($this->laravel, $laravelRequest, $swooleResponse);
213
            }
214
            if (!$handleStaticSuccess) {
215
                $this->handleDynamicResource($this->laravel, $laravelRequest, $swooleResponse);
216
            }
217
        } catch (\Exception $e) {
218
            $this->handleException($e, $swooleResponse);
219
        }
220
    }
221
222
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
223
     * @param \Exception $e
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
224
     * @param SwooleResponse $response
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
225
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
226
    protected function handleException($e, SwooleResponse $response)
227
    {
228
        $error = sprintf(
229
            'onRequest: Uncaught exception "%s"([%d]%s) at %s:%s, %s%s',
230
            get_class($e),
231
            $e->getCode(),
232
            $e->getMessage(),
233
            $e->getFile(),
234
            $e->getLine(),
235
            PHP_EOL,
236
            $e->getTraceAsString()
237
        );
238
        $this->error($error);
239
        try {
240
            $response->status(500);
241
            $response->end('Oops! An unexpected error occurred');
242
        } catch (\Exception $e) {
243
            $this->logException($e);
244
        }
245
    }
246
247
    protected function handleStaticResource(Laravel $laravel, IlluminateRequest $laravelRequest, SwooleResponse $swooleResponse)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function handleStaticResource()
Loading history...
248
    {
249
        $laravelResponse = $laravel->handleStatic($laravelRequest);
250
        if ($laravelResponse === false) {
251
            return false;
252
        }
253
        if (!empty($this->conf['server'])) {
254
            $laravelResponse->headers->set('Server', $this->conf['server'], true);
255
        }
256
        $laravel->fireEvent('laravels.generated_response', [$laravelRequest, $laravelResponse]);
257
        $response = new StaticResponse($swooleResponse, $laravelResponse);
258
        $response->setChunkLimit($this->conf['swoole']['buffer_output_size']);
259
        $response->send($this->conf['enable_gzip']);
260
        return true;
261
    }
262
263
    protected function handleDynamicResource(Laravel $laravel, IlluminateRequest $laravelRequest, SwooleResponse $swooleResponse)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function handleDynamicResource()
Loading history...
264
    {
265
        $laravel->cleanProviders();
266
        $laravelResponse = $laravel->handleDynamic($laravelRequest);
267
        if (!empty($this->conf['server'])) {
268
            $laravelResponse->headers->set('Server', $this->conf['server'], true);
269
        }
270
        $laravel->fireEvent('laravels.generated_response', [$laravelRequest, $laravelResponse]);
271
        if ($laravelResponse instanceof BinaryFileResponse) {
272
            $response = new StaticResponse($swooleResponse, $laravelResponse);
273
        } else {
274
            $response = new DynamicResponse($swooleResponse, $laravelResponse);
275
        }
276
        $response->setChunkLimit($this->conf['swoole']['buffer_output_size']);
277
        $response->send($this->conf['enable_gzip']);
278
        $laravel->clean();
279
        return true;
280
    }
281
282
    /**@var OutputStyle */
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...
283
    protected static $outputStyle;
284
285
    public static function setOutputStyle(OutputStyle $outputStyle)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setOutputStyle()
Loading history...
286
    {
287
        static::$outputStyle = $outputStyle;
288
    }
289
290
    public static function getOutputStyle()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getOutputStyle()
Loading history...
291
    {
292
        return static::$outputStyle;
293
    }
294
}
295