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

ServerRunCommand   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 85.19%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 5
c 6
b 0
f 2
lcom 1
cbo 4
dl 0
loc 62
ccs 23
cts 27
cp 0.8519
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 9 1
A execute() 0 19 3
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