Completed
Pull Request — master (#10)
by Matze
07:31
created

ServerRunCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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