Completed
Pull Request — master (#30)
by Anton
17:34
created

StartCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 23.53%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
lcom 2
cbo 4
dl 0
loc 75
ccs 8
cts 34
cp 0.2353
rs 10
c 2
b 0
f 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 16 1
B execute() 0 47 6
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 Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Process\Process;
13
14
/**
15
 * ServerCommand
16
 *
17
 * @package  Bluzman\Command\Server
18
 *
19
 * @author   Pavel Machekhin
20
 * @created  2013-05-24 19:23
21
 */
22
class StartCommand extends AbstractServerCommand
23
{
24
    /**
25
     * Command configuration
26
     */
27 14
    protected function configure()
28
    {
29
        $this
30
            // the name of the command (the part after "bin/bluzman")
31 14
            ->setName('server:start')
32
            // the short description shown while running "php bin/bluzman list"
33 14
            ->setDescription('Launche a 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 start built-in PHP server')
37
        ;
38
39 14
        $this->addOption('host', null, InputOption::VALUE_OPTIONAL, 'IP address of the server', '0.0.0.0');
40 14
        $this->addOption('port', null, InputOption::VALUE_OPTIONAL, 'Port of the server', '8000');
41 14
        $this->addOption('background', 'b', InputOption::VALUE_NONE, 'Run the server in the background');
42 14
    }
43
44
    /**
45
     * @param InputInterface $input
46
     * @param OutputInterface $output
47
     * @return int|null|void
48
     */
49
    protected function execute(InputInterface $input, OutputInterface $output)
50
    {
51
        $this->info('Running "server:start" command ... [' . $input->getOption('env') . ']');
52
53
        $host = $input->getOption('host');
54
        $port = $input->getOption('port');
55
        $env = $input->getOption('env');
56
57
        $publicDir = $this->getApplication()->getWorkingPath() . DS . 'public';
58
59
        // setup BLUZ_ENV to environment
60
        // enable BLUZ_DEBUG
61
        // use public/routing.php
62
        $process = new Process(
63
            "export BLUZ_ENV=$env && export BLUZ_DEBUG=1 && php -S $host:$port routing.php",
64
            $publicDir
65
        );
66
67
        $this->write("Server has been started at <info>$host:$port</info>");
68
69
        if ($input->getOption('background')) {
70
            $process->disableOutput();
71
            $process->start();
72
73
            $processId = $this->getProcessId($input->getOption('host'), $input->getOption('port'));
74
75
            $this->write("PID is <info>$processId</info>");
76
        } else {
77
            while ($process instanceof Process) {
78
                if (!$process->isStarted()) {
79
                    $process->start();
80
                    continue;
81
                }
82
83
                echo $process->getIncrementalOutput();
84
                echo $process->getIncrementalErrorOutput();
85
86
                if (!$process->isRunning() || $process->isTerminated()) {
87
                    $process = false;
88
89
                    $this->info('Server has been stopped.');
90
                }
91
92
                sleep(1);
93
            }
94
        }
95
    }
96
}
97