Passed
Push — master ( 18281d...3cf8e5 )
by Biao
03:28
created

LaravelSCommand   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 102
dl 0
loc 175
rs 9.84
c 0
b 0
f 0
wmc 32

9 Methods

Rating   Name   Duplication   Size   Complexity  
A loadConfigManually() 0 6 4
A isLumen() 0 3 1
A fire() 0 3 1
A handle() 0 16 4
A showInfo() 0 25 1
A prepareConfig() 0 26 2
B publish() 0 33 7
A preSet() 0 20 6
A preCheck() 0 14 6
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}
11
    {--d|daemonize : Whether run as a daemon for "start & restart"}
12
    {--i|ignore : Whether ignore checking process pid for "start & restart"}';
13
14
    protected $description = 'LaravelS console tool';
15
16
    public function fire()
17
    {
18
        $this->handle();
19
    }
20
21
    public function handle()
22
    {
23
        $action = (string)$this->argument('action');
24
        switch ($action) {
25
            case 'publish':
26
                $this->publish();
27
                break;
28
            case 'config':
29
                $this->prepareConfig();
30
                break;
31
            case 'info':
32
                $this->showInfo();
33
                break;
34
            default:
35
                $this->info('Usage: php artisan laravels publish|config|info');
36
                break;
37
        }
38
    }
39
40
    protected function isLumen()
41
    {
42
        return stripos($this->getApplication()->getVersion(), 'Lumen') !== false;
43
    }
44
45
    protected function loadConfigManually()
46
    {
47
        // Load configuration laravel.php manually for Lumen
48
        $basePath = config('laravels.laravel_base_path') ?: base_path();
49
        if ($this->isLumen() && file_exists($basePath . '/config/laravels.php')) {
50
            $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

50
            $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...
51
        }
52
    }
53
54
    protected function showInfo()
55
    {
56
        static $logo = <<<EOS
57
 _                               _  _____ 
58
| |                             | |/ ____|
59
| |     __ _ _ __ __ ___   _____| | (___  
60
| |    / _` | '__/ _` \ \ / / _ \ |\___ \ 
61
| |___| (_| | | | (_| |\ V /  __/ |____) |
62
|______\__,_|_|  \__,_| \_/ \___|_|_____/ 
63
                                           
64
EOS;
65
        $this->info($logo);
66
        $this->comment('Speed up your Laravel/Lumen');
67
        $this->table(['Component', 'Version'], [
68
            [
69
                'Component' => 'PHP',
70
                'Version'   => phpversion(),
71
            ],
72
            [
73
                'Component' => 'Swoole',
74
                'Version'   => swoole_version(),
75
            ],
76
            [
77
                'Component' => $this->getApplication()->getName(),
78
                'Version'   => $this->getApplication()->getVersion(),
79
            ],
80
        ]);
81
    }
82
83
    protected function prepareConfig()
84
    {
85
        $this->loadConfigManually();
86
87
        $svrConf = config('laravels');
88
89
        $this->preSet($svrConf);
90
91
        $ret = $this->preCheck($svrConf);
92
        if ($ret !== 0) {
93
            return $ret;
94
        }
95
96
        $laravelConf = [
97
            'root_path'          => $svrConf['laravel_base_path'],
98
            'static_path'        => $svrConf['swoole']['document_root'],
99
            'register_providers' => array_unique((array)array_get($svrConf, 'register_providers', [])),
100
            'is_lumen'           => $this->isLumen(),
101
            '_SERVER'            => $_SERVER,
102
            '_ENV'               => $_ENV,
103
        ];
104
105
        $config = json_encode(compact('svrConf', 'laravelConf'), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
106
        file_put_contents(base_path('storage/laravels.json'), $config);
107
        $this->info('Prepare configuration successfully');
108
        return 0;
109
    }
110
111
    protected function preSet(array &$svrConf)
112
    {
113
        if (!isset($svrConf['enable_gzip'])) {
114
            $svrConf['enable_gzip'] = false;
115
        }
116
        if (empty($svrConf['laravel_base_path'])) {
117
            $svrConf['laravel_base_path'] = base_path();
118
        }
119
        if (empty($svrConf['process_prefix'])) {
120
            $svrConf['process_prefix'] = $svrConf['laravel_base_path'];
121
        }
122
        $svrConf['ignore_check_pid'] = $this->option('ignore');
123
        if (empty($svrConf['swoole']['document_root'])) {
124
            $svrConf['swoole']['document_root'] = $svrConf['laravel_base_path'] . '/public';
125
        }
126
        $svrConf['swoole']['daemonize'] = $this->option('daemonize');
127
        if (empty($svrConf['swoole']['pid_file'])) {
128
            $svrConf['swoole']['pid_file'] = storage_path('laravels.pid');
129
        }
130
        return 0;
131
    }
132
133
    protected function preCheck(array $svrConf)
134
    {
135
        if (!empty($svrConf['enable_gzip']) && version_compare(swoole_version(), '4.1.0', '>=')) {
136
            $this->error('enable_gzip is DEPRECATED since Swoole 4.1.0, set http_compression of Swoole instead, http_compression is disabled by default.');
137
            $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.');
138
            return 1;
139
        }
140
        if (!empty($svrConf['events'])) {
141
            if (empty($svrConf['swoole']['task_worker_num']) || $svrConf['swoole']['task_worker_num'] <= 0) {
142
                $this->error('Asynchronous event listening needs to set task_worker_num > 0');
143
                return 1;
144
            }
145
        }
146
        return 0;
147
    }
148
149
150
    public function publish()
151
    {
152
        $basePath = config('laravels.laravel_base_path') ?: base_path();
153
        $configPath = $basePath . '/config/laravels.php';
154
        $todoList = [
155
            ['from' => __DIR__ . '/../../config/laravels.php', 'to' => $configPath, 'mode' => 0644],
156
            ['from' => __DIR__ . '/../../bin/laravels', 'to' => $basePath . '/bin/laravels', 'mode' => 0755],
157
            ['from' => __DIR__ . '/../../bin/fswatch', 'to' => $basePath . '/bin/fswatch', 'mode' => 0755],
158
        ];
159
        if (file_exists($configPath)) {
160
            $choice = $this->anticipate($configPath . ' already exists, do you want to override it ? Y/N',
161
                ['Y', 'N'],
162
                'N'
163
            );
164
            if (!$choice || strtoupper($choice) !== 'Y') {
165
                array_shift($todoList);
166
            }
167
        }
168
169
        /** @var Filesystem $file */
170
        $file = app(Filesystem::class);
171
        foreach ($todoList as $todo) {
172
            $toDir = dirname($todo['to']);
173
            if (!$file->isDirectory($toDir)) {
174
                $file->makeDirectory($toDir, 0755, true);
175
            }
176
            $file->copy($todo['from'], $todo['to']);
177
            chmod($todo['to'], $todo['mode']);
178
            $from = str_replace($basePath, '', realpath($todo['from']));
179
            $to = str_replace($basePath, '', realpath($todo['to']));
180
            $this->line('<info>Copied File</info> <comment>[' . $from . ']</comment> <info>To</info> <comment>[' . $to . ']</comment>');
181
        }
182
        return 0;
183
    }
184
}
185