Total Complexity | 66 |
Total Lines | 322 |
Duplicated Lines | 0 % |
Changes | 19 | ||
Bugs | 5 | Features | 2 |
Complex classes like Portal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Portal, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Portal extends Command |
||
17 | { |
||
18 | use LogTrait; |
||
19 | |||
20 | |||
21 | /**@var string */ |
||
22 | protected $basePath; |
||
23 | |||
24 | /**@var InputInterface */ |
||
25 | protected $input; |
||
26 | |||
27 | /**@var OutputInterface */ |
||
28 | protected $output; |
||
29 | |||
30 | public function __construct($basePath) |
||
34 | } |
||
35 | |||
36 | protected function configure() |
||
47 | } |
||
48 | |||
49 | protected function execute(InputInterface $input, OutputInterface $output) |
||
100 | } |
||
101 | } |
||
102 | |||
103 | public function start() |
||
104 | { |
||
105 | if (!extension_loaded('swoole')) { |
||
106 | $this->error('LaravelS requires swoole extension, try to `pecl install swoole` and `php --ri swoole`.'); |
||
107 | return 1; |
||
108 | } |
||
109 | |||
110 | // Generate conf file storage/laravels.conf |
||
111 | $options = $this->input->getOptions(); |
||
112 | if (isset($options['env']) && $options['env'] !== '') { |
||
113 | $_SERVER['LARAVEL_ENV'] = $_ENV['LARAVEL_ENV'] = $options['env']; |
||
114 | } |
||
115 | if (isset($options['x-version']) && $options['x-version'] !== '') { |
||
116 | $_SERVER['X_VERSION'] = $_ENV['X_VERSION'] = $options['x-version']; |
||
117 | } |
||
118 | |||
119 | // Load Apollo configurations to .env file |
||
120 | if (!empty($options['enable-apollo'])) { |
||
121 | $this->loadApollo($options); |
||
122 | } |
||
123 | |||
124 | $passOptionStr = ''; |
||
125 | $passOptions = ['daemonize', 'ignore', 'x-version']; |
||
126 | foreach ($passOptions as $key) { |
||
127 | if (!isset($options[$key])) { |
||
128 | continue; |
||
129 | } |
||
130 | $value = $options[$key]; |
||
131 | if ($value === false) { |
||
132 | continue; |
||
133 | } |
||
134 | $passOptionStr .= sprintf('--%s%s ', $key, is_bool($value) ? '' : ('=' . $value)); |
||
135 | } |
||
136 | $statusCode = $this->runArtisanCommand(trim('laravels config ' . $passOptionStr)); |
||
137 | if ($statusCode !== 0) { |
||
138 | return $statusCode; |
||
139 | } |
||
140 | |||
141 | // Here we go... |
||
142 | $config = $this->getConfig(); |
||
143 | |||
144 | if (!$config['server']['ignore_check_pid'] && file_exists($config['server']['swoole']['pid_file'])) { |
||
145 | $pid = (int)file_get_contents($config['server']['swoole']['pid_file']); |
||
146 | if ($pid > 0 && self::kill($pid, 0)) { |
||
147 | $this->warning(sprintf('Swoole[PID=%d] is already running.', $pid)); |
||
148 | return 1; |
||
149 | } |
||
150 | } |
||
151 | |||
152 | if ($config['server']['swoole']['daemonize']) { |
||
153 | $this->trace('Swoole is running in daemon mode, see "ps -ef|grep laravels".'); |
||
154 | } else { |
||
155 | $this->trace('Swoole is running, press Ctrl+C to quit.'); |
||
156 | } |
||
157 | |||
158 | (new LaravelS($config['server'], $config['laravel']))->run(); |
||
159 | |||
160 | return 0; |
||
161 | } |
||
162 | |||
163 | public function stop() |
||
164 | { |
||
165 | $config = $this->getConfig(); |
||
166 | $pidFile = $config['server']['swoole']['pid_file']; |
||
167 | if (!file_exists($pidFile)) { |
||
168 | $this->warning('It seems that Swoole is not running.'); |
||
169 | return 0; |
||
170 | } |
||
171 | |||
172 | $pid = file_get_contents($pidFile); |
||
173 | if (self::kill($pid, 0)) { |
||
174 | if (self::kill($pid, SIGTERM)) { |
||
175 | // Make sure that master process quit |
||
176 | $time = 1; |
||
177 | $waitTime = isset($config['server']['swoole']['max_wait_time']) ? $config['server']['swoole']['max_wait_time'] : 60; |
||
178 | $this->info("The max time of waiting to forcibly stop is {$waitTime}s."); |
||
179 | while (self::kill($pid, 0)) { |
||
180 | if ($time > $waitTime) { |
||
181 | $this->warning("Swoole [PID={$pid}] cannot be stopped gracefully in {$waitTime}s, will be stopped forced right now."); |
||
182 | return 1; |
||
183 | } |
||
184 | $this->info("Waiting Swoole[PID={$pid}] to stop. [{$time}]"); |
||
185 | sleep(1); |
||
186 | $time++; |
||
187 | } |
||
188 | $basePath = dirname($pidFile); |
||
189 | $deleteFiles = [ |
||
190 | $pidFile, |
||
191 | $basePath . '/laravels-custom-processes.pid', |
||
192 | $basePath . '/laravels-timer-process.pid', |
||
193 | ]; |
||
194 | foreach ($deleteFiles as $deleteFile) { |
||
195 | if (file_exists($deleteFile)) { |
||
196 | unlink($deleteFile); |
||
197 | } |
||
198 | } |
||
199 | $this->info("Swoole [PID={$pid}] is stopped."); |
||
200 | return 0; |
||
201 | } else { |
||
202 | $this->error("Swoole [PID={$pid}] is stopped failed."); |
||
203 | return 1; |
||
204 | } |
||
205 | } else { |
||
206 | $this->warning("Swoole [PID={$pid}] does not exist, or permission denied."); |
||
207 | return 0; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | public function restart() |
||
212 | { |
||
213 | $code = $this->stop(); |
||
214 | if ($code !== 0) { |
||
215 | return $code; |
||
216 | } |
||
217 | return $this->start(); |
||
218 | } |
||
219 | |||
220 | public function reload() |
||
221 | { |
||
222 | $config = $this->getConfig(); |
||
223 | $pidFile = $config['server']['swoole']['pid_file']; |
||
224 | if (!file_exists($pidFile)) { |
||
225 | $this->error('It seems that Swoole is not running.'); |
||
226 | return 1; |
||
227 | } |
||
228 | |||
229 | // Reload worker processes |
||
230 | $pid = file_get_contents($pidFile); |
||
231 | if (!$pid || !self::kill($pid, 0)) { |
||
232 | $this->error("Swoole [PID={$pid}] does not exist, or permission denied."); |
||
233 | return 1; |
||
234 | } |
||
235 | if (self::kill($pid, SIGUSR1)) { |
||
236 | $this->info("Swoole [PID={$pid}] is reloaded."); |
||
237 | } else { |
||
238 | $this->error("Swoole [PID={$pid}] is reloaded failed."); |
||
239 | } |
||
240 | |||
241 | // Reload custom processes |
||
242 | $pidFile = dirname($pidFile) . '/laravels-custom-processes.pid'; |
||
243 | if (file_exists($pidFile)) { |
||
244 | $pids = (array)explode("\n", trim(file_get_contents($pidFile))); |
||
245 | unlink($pidFile); |
||
246 | foreach ($pids as $pid) { |
||
247 | if (!$pid || !self::kill($pid, 0)) { |
||
248 | $this->error("Custom process[PID={$pid}] does not exist, or permission denied."); |
||
249 | continue; |
||
250 | } |
||
251 | |||
252 | if (self::kill($pid, SIGUSR1)) { |
||
253 | $this->info("Custom process[PID={$pid}] is reloaded."); |
||
254 | } else { |
||
255 | $this->error("Custom process[PID={$pid}] is reloaded failed."); |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | |||
260 | // Reload timer process |
||
261 | if (!empty($config['server']['timer']['enable']) && !empty($config['server']['timer']['jobs'])) { |
||
262 | $pidFile = dirname($pidFile) . '/laravels-timer-process.pid'; |
||
263 | $pid = file_get_contents($pidFile); |
||
264 | if (!$pid || !self::kill($pid, 0)) { |
||
265 | $this->error("Timer process[PID={$pid}] does not exist, or permission denied."); |
||
266 | return 1; |
||
267 | } |
||
268 | |||
269 | if (self::kill($pid, SIGUSR1)) { |
||
270 | $this->info("Timer process[PID={$pid}] is reloaded."); |
||
271 | } else { |
||
272 | $this->error("Timer process[PID={$pid}] is reloaded failed."); |
||
273 | } |
||
274 | } |
||
275 | return 0; |
||
276 | } |
||
277 | |||
278 | public function showInfo() |
||
281 | } |
||
282 | |||
283 | public function loadApollo(array $options) |
||
284 | { |
||
285 | Client::putCommandOptionsToEnv($options); |
||
286 | $envFile = $this->basePath . '/.env'; |
||
287 | if (isset($options['env'])) { |
||
288 | $envFile .= '.' . $options['env']; |
||
289 | } |
||
290 | Client::createFromCommandOptions($options)->pullAllAndSave($envFile); |
||
291 | } |
||
292 | |||
293 | public function makeArtisanCmd($subCmd) |
||
294 | { |
||
295 | $phpCmd = sprintf('%s -c "%s"', PHP_BINARY, php_ini_loaded_file()); |
||
296 | $env = $this->input->getOption('env'); |
||
297 | $envs = $env ? "APP_ENV={$env}" : ''; |
||
298 | return trim(sprintf('%s %s %s/artisan %s', $envs, $phpCmd, $this->basePath, $subCmd)); |
||
299 | } |
||
300 | |||
301 | public function runArtisanCommand($cmd) |
||
305 | } |
||
306 | |||
307 | public function getConfig() |
||
308 | { |
||
309 | return unserialize(file_get_contents($this->getConfPath())); |
||
310 | } |
||
311 | |||
312 | protected function getConfPath() |
||
313 | { |
||
314 | return $this->basePath . '/storage/laravels.conf'; |
||
315 | } |
||
316 | |||
317 | public static function runCommand($cmd, $input = null) |
||
318 | { |
||
319 | $fp = popen($cmd, 'w'); |
||
320 | if ($fp === false) { |
||
321 | return false; |
||
322 | } |
||
323 | if ($input !== null) { |
||
324 | $bytes = fwrite($fp, $input); |
||
325 | if ($bytes === false) { |
||
326 | return 1; |
||
327 | } |
||
328 | } |
||
329 | return pclose($fp); |
||
330 | } |
||
331 | |||
332 | public static function kill($pid, $sig) |
||
338 | } |
||
339 | } |
||
340 | } |
||
341 |