Passed
Push — master ( 13e321...dfd46e )
by Biao
01:55
created

LaravelSCommand::getDescription()   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
3
namespace Hhxsv5\LaravelS\Illuminate;
4
5
use Illuminate\Console\Command;
0 ignored issues
show
Bug introduced by
The type Illuminate\Console\Command 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\Filesystem\Filesystem;
0 ignored issues
show
Bug introduced by
The type Illuminate\Filesystem\Filesystem 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
8
class LaravelSCommand extends Command
9
{
10
    protected $signature = 'laravels {action?}';
11
12
    protected $description = 'LaravelS Console Tool';
13
14
    protected $actions;
15
16
    protected $isLumen = false;
17
18
    protected $files;
19
20
    public function __construct(Filesystem $files)
21
    {
22
        parent::__construct();
23
24
        $this->actions = ['start', 'stop', 'restart', 'reload', 'publish'];
25
        $this->description .= ': ' . implode('|', $this->actions);
26
        $this->files = $files;
27
    }
28
29
    public function fire()
30
    {
31
        $this->handle();
32
    }
33
34
    public function handle()
35
    {
36
        $action = $this->argument('action');
37
        if (!in_array($action, $this->actions, true)) {
38
            $this->info('php laravels {' . implode('|', $this->actions) . '}');
39
            return;
40
        }
41
        $this->info('LaravelS for ' . $this->getApplication()->getLongVersion());
42
        $this->isLumen = stripos($this->getApplication()->getVersion(), 'Lumen') !== false;
43
        $this->{$action}();
44
    }
45
46
    protected function start()
47
    {
48
        $laravelConf = ['rootPath' => base_path(), 'isLumen' => $this->isLumen];
0 ignored issues
show
Bug introduced by
The function base_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

48
        $laravelConf = ['rootPath' => /** @scrutinizer ignore-call */ base_path(), 'isLumen' => $this->isLumen];
Loading history...
49
        $svrConf = config('laravels');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

49
        $svrConf = /** @scrutinizer ignore-call */ config('laravels');
Loading history...
50
        if (file_exists($svrConf['swoole']['pid_file'])) {
51
            $pid = file_get_contents($svrConf['swoole']['pid_file']);
52
            if (posix_kill($pid, 0)) {
0 ignored issues
show
Bug introduced by
$pid of type string is incompatible with the type integer expected by parameter $pid of posix_kill(). ( Ignorable by Annotation )

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

52
            if (posix_kill(/** @scrutinizer ignore-type */ $pid, 0)) {
Loading history...
53
                $this->warn("LaravelS: PID[{$pid}] is already running.");
54
                return;
55
            }
56
        }
57
58
        // Implements gracefully reload, avoid including laravel's files before worker start
59
        $cmd = sprintf('%s %s/../GoLaravelS.php', PHP_BINARY, __DIR__);
60
        $fp = popen($cmd, 'w');
61
        fwrite($fp, json_encode(compact('svrConf', 'laravelConf')));
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

61
        fwrite(/** @scrutinizer ignore-type */ $fp, json_encode(compact('svrConf', 'laravelConf')));
Loading history...
62
        fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

62
        fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
63
        $pidFile = config('laravels.swoole.pid_file');
64
        $this->info(sprintf('LaravelS: PID[%s] is running.', file_get_contents($pidFile)));
65
    }
66
67
    protected function stop($ignoreErr = false)
68
    {
69
        $pidFile = config('laravels.swoole.pid_file');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

69
        $pidFile = /** @scrutinizer ignore-call */ config('laravels.swoole.pid_file');
Loading history...
70
        if (file_exists($pidFile)) {
71
            $pid = file_get_contents($pidFile);
72
            if (posix_kill($pid, 0)) {
0 ignored issues
show
Bug introduced by
$pid of type string is incompatible with the type integer expected by parameter $pid of posix_kill(). ( Ignorable by Annotation )

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

72
            if (posix_kill(/** @scrutinizer ignore-type */ $pid, 0)) {
Loading history...
73
                if (posix_kill($pid, SIGTERM)) {
74
                    // Make sure that master process quit
75
                    $time = 0;
76
                    while (posix_getpgid($pid) && $time <= 20) {
0 ignored issues
show
Bug introduced by
$pid of type string is incompatible with the type integer expected by parameter $pid of posix_getpgid(). ( Ignorable by Annotation )

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

76
                    while (posix_getpgid(/** @scrutinizer ignore-type */ $pid) && $time <= 20) {
Loading history...
77
                        usleep(100000);
78
                        $time++;
79
                    }
80
                    $this->info("LaravelS: PID[{$pid}] is stopped.");
81
                } else {
82
                    $this->error("LaravelS: PID[{$pid}] is stopped failed.");
83
                }
84
            } else {
85
                $this->warn("LaravelS: PID[{$pid}] does not exist, or permission denied.");
86
                if (!$ignoreErr) {
87
                    return;
88
                }
89
            }
90
        } else {
91
            $this->info('LaravelS: already stopped.');
92
        }
93
    }
94
95
    protected function restart()
96
    {
97
        $this->stop(true);
98
        $this->start();
99
    }
100
101
    protected function reload()
102
    {
103
        $pidFile = config('laravels.swoole.pid_file');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

103
        $pidFile = /** @scrutinizer ignore-call */ config('laravels.swoole.pid_file');
Loading history...
104
        if (!file_exists($pidFile)) {
105
            $this->error('LaravelS: it seems that LaravelS is not running.');
106
            return;
107
        }
108
109
        $pid = file_get_contents($pidFile);
110
        if (!posix_kill($pid, 0)) {
0 ignored issues
show
Bug introduced by
$pid of type string is incompatible with the type integer expected by parameter $pid of posix_kill(). ( Ignorable by Annotation )

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

110
        if (!posix_kill(/** @scrutinizer ignore-type */ $pid, 0)) {
Loading history...
111
            $this->error("LaravelS: PID[{$pid}] does not exist, or permission denied.");
112
            return;
113
        }
114
115
        if (posix_kill($pid, SIGUSR1)) {
116
            $this->info("LaravelS: PID[{$pid}] is reloaded.");
117
        } else {
118
            $this->error("LaravelS: PID[{$pid}] is reloaded failed.");
119
        }
120
    }
121
122
    protected function publish()
123
    {
124
        $to = base_path('config/laravels.php');
0 ignored issues
show
Bug introduced by
The function base_path was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

124
        $to = /** @scrutinizer ignore-call */ base_path('config/laravels.php');
Loading history...
125
        if (file_exists($to)) {
126
            $choice = $this->anticipate($to . ' already exists, do you want to override it ? Y/N', ['Y', 'N'], 'N');
127
            if (!$choice || strtoupper($choice) === 'N') {
128
                $this->info('Publishing complete.');
129
                return;
130
            }
131
        }
132
133
        try {
134
            $this->call('vendor:publish', ['--provider' => LaravelSServiceProvider::class, '--force' => true]);
135
            return;
136
        } catch (\Exception $e) {
137
            if (!($e instanceof \InvalidArgumentException)) {
138
                throw $e;
139
            }
140
        }
141
        $from = __DIR__ . '/../Config/laravels.php';
142
143
        $toDir = dirname($to);
144
145
        if (!$this->files->isDirectory($toDir)) {
146
            $this->files->makeDirectory($toDir, 0755, true);
147
        }
148
149
        $this->files->copy($from, $to);
150
151
        $from = str_replace(base_path(), '', realpath($from));
152
153
        $to = str_replace(base_path(), '', realpath($to));
154
155
        $this->line('<info>Copied File</info> <comment>[' . $from . ']</comment> <info>To</info> <comment>[' . $to . ']</comment>');
156
    }
157
158
    public function getDescription()
159
    {
160
        return $this->description;
161
    }
162
}