Passed
Push — master ( 9ac57c...18281d )
by Biao
03:34
created

LaravelSCommand::publish()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nc 9
nop 0
dl 0
loc 33
rs 8.6186
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}
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
        if (empty($svrConf['swoole']['document_root'])) {
123
            $svrConf['swoole']['document_root'] = $svrConf['laravel_base_path'] . '/public';
124
        }
125
        if ($this->option('daemonize')) {
126
            $svrConf['swoole']['daemonize'] = true;
127
        }
128
        if (empty($svrConf['swoole']['pid_file'])) {
129
            $svrConf['swoole']['pid_file'] = storage_path('laravels.pid');
130
        }
131
        return 0;
132
    }
133
134
    protected function preCheck(array $svrConf)
135
    {
136
        if (!empty($svrConf['enable_gzip']) && version_compare(swoole_version(), '4.1.0', '>=')) {
137
            $this->error('enable_gzip is DEPRECATED since Swoole 4.1.0, set http_compression of Swoole instead, http_compression is disabled by default.');
138
            $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.');
139
            return 1;
140
        }
141
        if (!empty($svrConf['events'])) {
142
            if (empty($svrConf['swoole']['task_worker_num']) || $svrConf['swoole']['task_worker_num'] <= 0) {
143
                $this->error('Asynchronous event listening needs to set task_worker_num > 0');
144
                return 1;
145
            }
146
        }
147
        return 0;
148
    }
149
150
151
    public function publish()
152
    {
153
        $basePath = config('laravels.laravel_base_path') ?: base_path();
154
        $configPath = $basePath . '/config/laravels.php';
155
        $todoList = [
156
            ['from' => __DIR__ . '/../../config/laravels.php', 'to' => $configPath, 'mode' => 0644],
157
            ['from' => __DIR__ . '/../../bin/laravels', 'to' => $basePath . '/bin/laravels', 'mode' => 0755],
158
            ['from' => __DIR__ . '/../../bin/fswatch', 'to' => $basePath . '/bin/fswatch', 'mode' => 0755],
159
        ];
160
        if (file_exists($configPath)) {
161
            $choice = $this->anticipate($configPath . ' already exists, do you want to override it ? Y/N',
162
                ['Y', 'N'],
163
                'N'
164
            );
165
            if (!$choice || strtoupper($choice) !== 'Y') {
166
                array_shift($todoList);
167
            }
168
        }
169
170
        /** @var Filesystem $file */
171
        $file = app(Filesystem::class);
172
        foreach ($todoList as $todo) {
173
            $toDir = dirname($todo['to']);
174
            if (!$file->isDirectory($toDir)) {
175
                $file->makeDirectory($toDir, 0755, true);
176
            }
177
            $file->copy($todo['from'], $todo['to']);
178
            chmod($todo['to'], $todo['mode']);
179
            $from = str_replace($basePath, '', realpath($todo['from']));
180
            $to = str_replace($basePath, '', realpath($todo['to']));
181
            $this->line('<info>Copied File</info> <comment>[' . $from . ']</comment> <info>To</info> <comment>[' . $to . ']</comment>');
182
        }
183
        return 0;
184
    }
185
}
186