Total Complexity | 73 |
Total Lines | 347 |
Duplicated Lines | 0 % |
Changes | 20 | ||
Bugs | 6 | 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() |
||
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 | $this->warning("Swoole [PID={$pid}] does not exist, or permission denied."); |
||
175 | return 0; |
||
176 | } |
||
177 | if (!self::kill($pid, SIGTERM)) { |
||
178 | $this->error("Swoole [PID={$pid}] is stopped failed."); |
||
179 | return 1; |
||
180 | } |
||
181 | // Make sure that master process quit |
||
182 | $time = 1; |
||
183 | $waitTime = isset($config['server']['swoole']['max_wait_time']) ? $config['server']['swoole']['max_wait_time'] : 60; |
||
184 | $this->info("The max time of waiting to forcibly stop is {$waitTime}s."); |
||
185 | while (self::kill($pid, 0)) { |
||
186 | if ($time > $waitTime) { |
||
187 | $this->warning("Swoole [PID={$pid}] cannot be stopped gracefully in {$waitTime}s, will be stopped forced right now."); |
||
188 | return 1; |
||
189 | } |
||
190 | $this->info("Waiting Swoole[PID={$pid}] to stop. [{$time}]"); |
||
191 | sleep(1); |
||
192 | $time++; |
||
193 | } |
||
194 | $basePath = dirname($pidFile); |
||
195 | $deleteFiles = [ |
||
196 | $pidFile, |
||
197 | $basePath . '/laravels-custom-processes.pid', |
||
198 | $basePath . '/laravels-timer-process.pid', |
||
199 | ]; |
||
200 | foreach ($deleteFiles as $deleteFile) { |
||
201 | if (file_exists($deleteFile)) { |
||
202 | unlink($deleteFile); |
||
203 | } |
||
204 | } |
||
205 | $this->info("Swoole [PID={$pid}] is stopped."); |
||
206 | return 0; |
||
207 | } |
||
208 | |||
209 | public function restart() |
||
210 | { |
||
211 | $code = $this->stop(); |
||
212 | if ($code !== 0) { |
||
213 | return $code; |
||
214 | } |
||
215 | return $this->start(); |
||
216 | } |
||
217 | |||
218 | public function reload() |
||
219 | { |
||
220 | $config = $this->getConfig(); |
||
221 | $pidFile = $config['server']['swoole']['pid_file']; |
||
222 | if (!file_exists($pidFile)) { |
||
223 | $this->error('It seems that Swoole is not running.'); |
||
224 | return 1; |
||
225 | } |
||
226 | |||
227 | // Reload worker processes |
||
228 | $pid = file_get_contents($pidFile); |
||
229 | if (!$pid || !self::kill($pid, 0)) { |
||
230 | $this->error("Swoole [PID={$pid}] does not exist, or permission denied."); |
||
231 | return 1; |
||
232 | } |
||
233 | if (self::kill($pid, SIGUSR1)) { |
||
234 | $this->info("Swoole [PID={$pid}] is reloaded."); |
||
235 | } else { |
||
236 | $this->error("Swoole [PID={$pid}] is reloaded failed."); |
||
237 | } |
||
238 | |||
239 | // Reload custom processes |
||
240 | $pidFile = dirname($pidFile) . '/laravels-custom-processes.pid'; |
||
241 | if (file_exists($pidFile)) { |
||
242 | $pids = (array)explode("\n", trim(file_get_contents($pidFile))); |
||
243 | unlink($pidFile); |
||
244 | foreach ($pids as $pid) { |
||
245 | if (!$pid || !self::kill($pid, 0)) { |
||
246 | $this->error("Custom process[PID={$pid}] does not exist, or permission denied."); |
||
247 | continue; |
||
248 | } |
||
249 | |||
250 | if (self::kill($pid, SIGUSR1)) { |
||
251 | $this->info("Custom process[PID={$pid}] is reloaded."); |
||
252 | } else { |
||
253 | $this->error("Custom process[PID={$pid}] is reloaded failed."); |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | // Reload timer process |
||
259 | if (!empty($config['server']['timer']['enable']) && !empty($config['server']['timer']['jobs'])) { |
||
260 | $pidFile = dirname($pidFile) . '/laravels-timer-process.pid'; |
||
261 | $pid = file_get_contents($pidFile); |
||
262 | if (!$pid || !self::kill($pid, 0)) { |
||
263 | $this->error("Timer process[PID={$pid}] does not exist, or permission denied."); |
||
264 | return 1; |
||
265 | } |
||
266 | |||
267 | if (self::kill($pid, SIGUSR1)) { |
||
268 | $this->info("Timer process[PID={$pid}] is reloaded."); |
||
269 | } else { |
||
270 | $this->error("Timer process[PID={$pid}] is reloaded failed."); |
||
271 | } |
||
272 | } |
||
273 | return 0; |
||
274 | } |
||
275 | |||
276 | public function showInfo() |
||
279 | } |
||
280 | |||
281 | public function loadApollo(array $options) |
||
289 | } |
||
290 | |||
291 | protected function isLoadSwooleByIni() |
||
292 | { |
||
293 | $iniFiles = explode(',', (string)php_ini_scanned_files()); |
||
294 | $iniFile = php_ini_loaded_file(); |
||
295 | if ($iniFile !== false) { |
||
296 | $iniFiles[] = $iniFile; |
||
297 | } |
||
298 | |||
299 | foreach ($iniFiles as $file) { |
||
300 | $content = str_replace(' ', '', file_get_contents(trim($file))); |
||
301 | if (stripos($content, 'extension=swoole') === 0) { |
||
302 | return true; |
||
303 | } |
||
304 | } |
||
305 | return false; |
||
306 | } |
||
307 | |||
308 | public function makeArtisanCmd($subCmd) |
||
309 | { |
||
310 | $iniFile = php_ini_loaded_file(); |
||
311 | if ($iniFile === false) { |
||
312 | $phpCmd = PHP_BINARY; |
||
313 | } else { |
||
314 | $phpCmd = sprintf('%s -c "%s"', PHP_BINARY, $iniFile); |
||
315 | } |
||
316 | |||
317 | if (!$this->isLoadSwooleByIni()) { |
||
318 | $phpCmd .= ' -d "extension=swoole"'; |
||
319 | } |
||
320 | |||
321 | $env = isset($_ENV['APP_ENV']) ? trim($_ENV['APP_ENV']) : ''; |
||
322 | $appEnv = $env === '' ? '' : "APP_ENV={$env}"; |
||
323 | return trim(sprintf('%s %s %s/artisan %s', $appEnv, $phpCmd, $this->basePath, $subCmd)); |
||
324 | } |
||
325 | |||
326 | public function runArtisanCommand($cmd) |
||
327 | { |
||
328 | $cmd = $this->makeArtisanCmd($cmd); |
||
329 | return self::runCommand($cmd); |
||
330 | } |
||
331 | |||
332 | public function getConfig() |
||
333 | { |
||
334 | return unserialize((string)file_get_contents($this->getConfPath())); |
||
335 | } |
||
336 | |||
337 | protected function getConfPath() |
||
338 | { |
||
339 | return $this->basePath . '/storage/laravels.conf'; |
||
340 | } |
||
341 | |||
342 | public static function runCommand($cmd, $input = null) |
||
343 | { |
||
344 | $fp = popen($cmd, 'w'); |
||
345 | if ($fp === false) { |
||
346 | return false; |
||
347 | } |
||
348 | if ($input !== null) { |
||
349 | $bytes = fwrite($fp, $input); |
||
350 | if ($bytes === false) { |
||
351 | return 1; |
||
352 | } |
||
353 | } |
||
354 | return pclose($fp); |
||
355 | } |
||
356 | |||
357 | public static function kill($pid, $sig) |
||
363 | } |
||
364 | } |
||
365 | } |
||
366 |