Passed
Push — master ( 318d64...79e67c )
by Biao
04:20
created

LaravelSCommand::loadConfigManually()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

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