Completed
Push — master ( 97f6b5...9ef69c )
by Matze
08:25
created

ServerRunCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BrainExe\Core\Console;
4
5
use BrainExe\Core\Annotations\Inject;
6
use Symfony\Component\Console\Command\Command;
7
use Symfony\Component\Console\Input\InputArgument;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Process\ProcessBuilder;
12
use BrainExe\Core\Annotations\Command as CommandAnnotation;
13
14
/**
15
 * @todo matze
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
16
 * @CommandAnnotation
17
 */
18
class ServerRunCommand extends Command
19
{
20
21
    /**
22
     * @var string
23
     */
24
    private $serverAddress;
25
26
    /**
27
     * @var ProcessBuilder
28
     */
29
    private $processBuilder;
30
31
    /**
32
     * @Inject({
33
     *     "processBuilder" = "@ProcessBuilder",
34
     *     "address" = "%server.host%"
35
     * })
36
     * @param ProcessBuilder $processBuilder
37
     * @param string $address
38
     */
39 2
    public function __construct(ProcessBuilder $processBuilder, string $address)
40
    {
41 2
        $this->serverAddress  = $address;
42 2
        $this->processBuilder = $processBuilder;
43
44 2
        parent::__construct(null);
45 2
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 2
    protected function configure()
51
    {
52
        $this
53 2
            ->addArgument('address', InputArgument::OPTIONAL, 'host:port')
54 2
            ->addOption('quiet', 'q', InputOption::VALUE_NONE)
55 2
            ->setName('server:run')
56 2
            ->setDescription('Runs PHP built-in web server');
57 2
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62 1
    protected function execute(InputInterface $input, OutputInterface $output)
63
    {
64 1
        $address = $input->getArgument('address') ?: $this->serverAddress;
65
66 1
        $output->writeln(sprintf("Server running on <info>%s</info>\n", $address));
67
68 1
        $process = $this->processBuilder
69 1
            ->setArguments([PHP_BINARY, '-S', $address])
70 1
            ->setWorkingDirectory(ROOT . 'web/')
71 1
            ->setTimeout(null)
72 1
            ->getProcess();
73
74 1
        $process->run(function ($type, $buffer) use ($output, $input) {
75
            unset($type);
76
            if (!$input->getOption('quiet')) {
77
                $output->write($buffer);
78
            }
79 1
        });
80 1
    }
81
}
82