|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Bluz PHP Team |
|
4
|
|
|
* @link https://github.com/bluzphp/bluzman |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Bluzman\Command\Server; |
|
8
|
|
|
|
|
9
|
|
|
use Bluzman\Input\InputOption; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
use Symfony\Component\Process\Process; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* StatusCommand |
|
16
|
|
|
* |
|
17
|
|
|
* @package Bluzman\Command\Server |
|
18
|
|
|
* |
|
19
|
|
|
* @author Pavel Machekhin |
|
20
|
|
|
* @created 2013-06-17 14:52 |
|
21
|
|
|
*/ |
|
22
|
|
|
class StatusCommand extends AbstractServerCommand |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Command configuration |
|
26
|
|
|
*/ |
|
27
|
14 |
View Code Duplication |
protected function configure() |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
$this |
|
30
|
|
|
// the name of the command (the part after "bin/bluzman") |
|
31
|
14 |
|
->setName('server:status') |
|
32
|
|
|
// the short description shown while running "php bin/bluzman list" |
|
33
|
14 |
|
->setDescription('Get the status of built-in PHP server') |
|
34
|
|
|
// the full command description shown when running the command with |
|
35
|
|
|
// the "--help" option |
|
36
|
14 |
|
->setHelp('This command allows you to check built-in PHP server') |
|
37
|
|
|
; |
|
38
|
14 |
|
$this->addOption('host', null, InputOption::VALUE_OPTIONAL, 'IP address of the server', '0.0.0.0'); |
|
39
|
14 |
|
$this->addOption('port', null, InputOption::VALUE_OPTIONAL, 'Port of the server', '8000'); |
|
40
|
14 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param InputInterface $input |
|
44
|
|
|
* @param OutputInterface $output |
|
45
|
|
|
* @return int|null|void |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->info('Running "server:status" command'); |
|
50
|
|
|
try { |
|
51
|
|
|
$host = $input->getOption('host'); |
|
52
|
|
|
$port = $input->getOption('port'); |
|
53
|
|
|
|
|
54
|
|
|
$pid = $this->getProcessId($host, $port) ?: false; |
|
55
|
|
|
|
|
56
|
|
|
if (!$pid) { |
|
|
|
|
|
|
57
|
|
|
throw new NotRunningException; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$process = new Process("ps -p $pid -o comm="); |
|
61
|
|
|
$process->run(); |
|
62
|
|
|
|
|
63
|
|
|
$processOutput = $process->getOutput(); |
|
64
|
|
|
|
|
65
|
|
|
if (empty($processOutput)) { |
|
66
|
|
|
throw new NotRunningException; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$this->write("Server <info>$host:$port</info> is running. PID is <info>$pid</info>"); |
|
70
|
|
|
} catch (NotRunningException $e) { |
|
71
|
|
|
$this->comment('Server is not running'); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.