Completed
Pull Request — master (#75)
by Vladimir
02:44
created

ServeCommand::execute()   A

Complexity

Conditions 2
Paths 10

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 2
nc 10
nop 2
1
<?php
2
3
namespace allejo\stakx\Console\Command;
4
5
use allejo\stakx\Server\DevServer;
6
use allejo\stakx\Server\PageViewRouter;
7
use allejo\stakx\Website;
8
use React\EventLoop\Factory;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class ServeCommand extends BuildCommand
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    protected function configure()
18
    {
19
        parent::configure();
20
21
        $this->setName('serve');
22
        $this->setDescription('Start a web server serving the stakx website');
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output)
26
    {
27
        $this->handleDeprecations($input, $output);
28
        $this->setRunTimeOptions($input);
29
30
        try
31
        {
32
            $this->configureConfigurationFile($input);
33
            $website = $this->getContainer()->get(Website::class);
34
35
            /** @var PageViewRouter $router */
36
            $router = $this->getContainer()->get(PageViewRouter::class);
37
38
            $loop = Factory::create();
39
            $socket = new \React\Socket\Server('0.0.0.0:8000', $loop);
40
41
            $server = DevServer::create($router, $website->getCompiler());
42
            $server->listen($socket);
43
44
            $output->writeln('Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()));
45
46
            $loop->run();
47
        }
48
        catch (\Exception $e)
49
        {
50
            $output->writeln($e->getMessage());
51
        }
52
    }
53
}
54