Completed
Push — master ( 550846...8c8483 )
by Anton
09:53
created

StartCommand::startServer()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 44
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
dl 0
loc 44
rs 8.439
c 2
b 0
f 1
cc 6
eloc 25
nc 5
nop 0
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
 * @category Command
18
 * @package  Bluzman
19
 *
20
 * @author   Pavel Machekhin
21
 * @created  2013-05-24 19:23
22
 */
23
class StartCommand extends AbstractServerCommand
24
{
25
    /**
26
     * Command configuration
27
     */
28
    protected function configure()
29
    {
30
        $this
31
            // the name of the command (the part after "bin/bluzman")
32
            ->setName('server:start')
33
            // the short description shown while running "php bin/bluzman list"
34
            ->setDescription('Launches a built-in PHP server')
35
            // the full command description shown when running the command with
36
            // the "--help" option
37
            ->setHelp('This command allows you to start built-in PHP server')
38
        ;
39
40
        $this->addOption('host', null, InputOption::VALUE_OPTIONAL, 'IP address of the server', '0.0.0.0');
41
        $this->addOption('port', null, InputOption::VALUE_OPTIONAL, 'Port of the server', '8000');
42
        $this->addOption('background', 'b', InputOption::VALUE_NONE, 'Run the server in the background');
43
    }
44
45
    /**
46
     * @param InputInterface $input
47
     * @param OutputInterface $output
48
     * @return int|null|void
49
     */
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        $this->info('Running "server:start" command ... [' . $input->getOption('env'). ']');
53
54
        $host = $input->getOption('host');
55
        $port = $input->getOption('port');
56
        $env = $input->getOption('env');
57
58
        $publicDir = $this->getApplication()->getWorkingPath() . DS . 'public';
59
60
        // setup BLUZ_ENV to environment
61
        // enable BLUZ_DEBUG
62
        // use public/routing.php
63
        $process = new Process(
64
            "export BLUZ_ENV=$env && export BLUZ_DEBUG=1 && php -S $host:$port routing.php",
65
            $publicDir
66
        );
67
68
        $this->write("Server has been started at <info>$host:$port</info>");
69
70
        if ($input->getOption('background')) {
71
            $process->disableOutput();
72
            $process->start();
73
74
            $processId = $this->getProcessId($input->getOption('host'), $input->getOption('port'));
75
76
            $this->write("PID is <info>$processId</info>");
77
        } else {
78
            while ($process instanceof Process) {
79
                if (!$process->isStarted()) {
80
                    $process->start();
81
                    continue;
82
                }
83
84
                echo $process->getIncrementalOutput();
85
                echo $process->getIncrementalErrorOutput();
86
87
                if (!$process->isRunning() || $process->isTerminated()) {
88
                    $process = false;
89
90
                    $this->info('Server has been stopped.');
91
                }
92
93
                sleep(1);
94
            }
95
        }
96
    }
97
}
98