Passed
Push — master ( 39dd08...3e832b )
by Biao
04:47
created

LaravelSCommand::showInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 25
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace Hhxsv5\LaravelS\Illuminate;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
8
class LaravelSCommand extends Command
9
{
10
    protected $signature = 'laravels {action? : publish|config|info|output}
11
    {--d|daemonize : Whether run as a daemon for "start & restart"}
12
    {--i|ignore : Whether ignore checking process pid for "start & restart"}
13
    {--l|level= : Set the level of console log for "output"}
14
    {--c|content= : Set the content of console log for "output", need base64 encoded}';
15
16
    protected $description = 'LaravelS console tool';
17
18
    public function fire()
19
    {
20
        $this->handle();
21
    }
22
23
    public function handle()
24
    {
25
        $action = (string)$this->argument('action');
26
        switch ($action) {
27
            case 'publish':
28
                $this->publish();
29
                break;
30
            case 'info':
31
                $this->showInfo();
32
                break;
33
            case 'config':
34
                $this->makeConfig();
35
                break;
36
            case 'output':
37
                $this->output();
38
                break;
39
            default:
40
                $this->info('Usage: php artisan laravels publish|config|info');
41
                break;
42
        }
43
    }
44
45
    protected function isLumen()
46
    {
47
        return stripos($this->getApplication()->getVersion(), 'Lumen') !== false;
48
    }
49
50
    protected function loadConfigManually()
51
    {
52
        // Load configuration laravel.php manually for Lumen
53
        $basePath = config('laravels.laravel_base_path') ?: base_path();
54
        if ($this->isLumen() && file_exists($basePath . '/config/laravels.php')) {
55
            $this->getLaravel()->configure('laravels');
0 ignored issues
show
Bug introduced by
The method configure() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

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

55
            $this->getLaravel()->/** @scrutinizer ignore-call */ configure('laravels');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
        }
57
    }
58
59
    public function output()
60
    {
61
        $level = strtolower($this->option('level'));
62
        if (!in_array($level, ['info', 'warn', 'error'], true)) {
63
            $this->error('Bad level for output');
64
            return;
65
        }
66
        $content = base64_decode($this->option('content'));
67
        $this->{$level}($content);
68
    }
69
70
    protected function showInfo()
71
    {
72
        static $logo = <<<EOS
73
 _                               _  _____ 
74
| |                             | |/ ____|
75
| |     __ _ _ __ __ ___   _____| | (___  
76
| |    / _` | '__/ _` \ \ / / _ \ |\___ \ 
77
| |___| (_| | | | (_| |\ V /  __/ |____) |
78
|______\__,_|_|  \__,_| \_/ \___|_|_____/ 
79
                                           
80
EOS;
81
        parent::info($logo);
82
        parent::info('Speed up your Laravel/Lumen');
83
        $this->table(['Component', 'Version'], [
84
            [
85
                'Component' => 'PHP',
86
                'Version'   => phpversion(),
87
            ],
88
            [
89
                'Component' => 'Swoole',
90
                'Version'   => swoole_version(),
91
            ],
92
            [
93
                'Component' => $this->getApplication()->getName(),
94
                'Version'   => $this->getApplication()->getVersion(),
95
            ],
96
        ]);
97
    }
98
99
    protected function makeConfig()
100
    {
101
        $this->loadConfigManually();
102
103
        $svrConf = config('laravels');
104
105
        $this->preSet($svrConf);
106
107
        $ret = $this->preCheck($svrConf);
108
        if ($ret !== 0) {
109
            return $ret;
110
        }
111
112
        $laravelConf = [
113
            'root_path'          => $svrConf['laravel_base_path'],
114
            'static_path'        => $svrConf['swoole']['document_root'],
115
            'register_providers' => array_unique((array)array_get($svrConf, 'register_providers', [])),
116
            'is_lumen'           => $this->isLumen(),
117
            '_SERVER'            => $_SERVER,
118
            '_ENV'               => $_ENV,
119
        ];
120
121
        $config = json_encode(compact('svrConf', 'laravelConf'), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
122
        file_put_contents(base_path('config/laravels.json'), $config);
123
        $this->info('Make configuration successfully');
124
        return 0;
125
    }
126
127
    protected function preSet(array &$svrConf)
128
    {
129
        if (!isset($svrConf['enable_gzip'])) {
130
            $svrConf['enable_gzip'] = false;
131
        }
132
        if (empty($svrConf['laravel_base_path'])) {
133
            $svrConf['laravel_base_path'] = base_path();
134
        }
135
        if (empty($svrConf['process_prefix'])) {
136
            $svrConf['process_prefix'] = $svrConf['laravel_base_path'];
137
        }
138
        if (empty($svrConf['swoole']['document_root'])) {
139
            $svrConf['swoole']['document_root'] = $svrConf['laravel_base_path'] . '/public';
140
        }
141
        if ($this->option('daemonize')) {
142
            $svrConf['swoole']['daemonize'] = true;
143
        }
144
        if (empty($svrConf['swoole']['pid_file'])) {
145
            $svrConf['swoole']['pid_file'] = storage_path('laravels.pid');
146
        }
147
        return 0;
148
    }
149
150
    protected function preCheck(array $svrConf)
151
    {
152
        if (!empty($svrConf['enable_gzip']) && version_compare(swoole_version(), '4.1.0', '>=')) {
153
            $this->error('enable_gzip is DEPRECATED since Swoole 4.1.0, set http_compression of Swoole instead, http_compression is disabled by default.');
154
            $this->info('If there is a proxy server like Nginx, suggest that enable gzip in Nginx and disable gzip in Swoole, to avoid the repeated gzip compression for response.');
155
            return 1;
156
        }
157
        if (!empty($svrConf['events'])) {
158
            if (empty($svrConf['swoole']['task_worker_num']) || $svrConf['swoole']['task_worker_num'] <= 0) {
159
                $this->error('Asynchronous event listening needs to set task_worker_num > 0');
160
                return 1;
161
            }
162
        }
163
        return 0;
164
    }
165
166
167
    public function publish()
168
    {
169
        $basePath = config('laravels.laravel_base_path') ?: base_path();
170
        $configPath = $basePath . '/config/laravels.php';
171
        $todoList = [
172
            ['from' => __DIR__ . '/../../config/laravels.php', 'to' => $configPath, 'mode' => 0644],
173
            ['from' => __DIR__ . '/../../bin/laravels', 'to' => $basePath . '/bin/laravels', 'mode' => 0755],
174
            ['from' => __DIR__ . '/../../bin/fswatch', 'to' => $basePath . '/bin/fswatch', 'mode' => 0755],
175
        ];
176
        if (file_exists($configPath)) {
177
            $choice = $this->anticipate($configPath . ' already exists, do you want to override it ? Y/N',
178
                ['Y', 'N'],
179
                'N'
180
            );
181
            if (!$choice || strtoupper($choice) !== 'Y') {
182
                array_shift($todoList);
183
            }
184
        }
185
186
        /**
187
         * @var Filesystem $files
188
         */
189
        $files = app(Filesystem::class);
190
        foreach ($todoList as $todo) {
191
            $toDir = dirname($todo['to']);
192
            if (!$files->isDirectory($toDir)) {
193
                $files->makeDirectory($toDir, 0755, true);
194
            }
195
            $files->copy($todo['from'], $todo['to']);
196
            chmod($todo['to'], $todo['mode']);
197
            $from = str_replace($basePath, '', realpath($todo['from']));
198
            $to = str_replace($basePath, '', realpath($todo['to']));
199
            $this->line('<info>Copied File</info> <comment>[' . $from . ']</comment> <info>To</info> <comment>[' . $to . ']</comment>');
200
        }
201
        return 0;
202
    }
203
204
    public function info($string)
205
    {
206
        $string = 'LaravelS: ' . $string;
207
        parent::info($string);
208
    }
209
210
    public function warn($string)
211
    {
212
        $string = 'LaravelS: ' . $string;
213
        parent::warn($string);
214
    }
215
216
    public function error($string)
217
    {
218
        $string = 'LaravelS: ' . $string;
219
        parent::error($string);
220
    }
221
}
222