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

ServeCommand   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 3
lcom 2
cbo 9
dl 0
loc 49
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
A execute() 0 32 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\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
13
class ServeCommand extends BuildCommand
14
{
15
    /**
16
     * {@inheritdoc}
17
     */
18
    protected function configure()
19
    {
20
        parent::configure();
21
22
        $this->setName('serve');
23
        $this->setDescription('Start a web server serving the stakx website');
24
25
        $this->addOption('port', 'p', InputOption::VALUE_REQUIRED, 'The port the local development server will be listening on', 8000);
26
        $this->addOption('bind', null, InputOption::VALUE_REQUIRED, 'The IP the local development server will bind to', '0.0.0.0');
27
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31
        $this->handleDeprecations($input, $output);
32
        $this->setRunTimeOptions($input);
33
34
        try
35
        {
36
            $this->configureConfigurationFile($input);
37
            $website = $this->getContainer()->get(Website::class);
38
39
            /** @var PageViewRouter $router */
40
            $router = $this->getContainer()->get(PageViewRouter::class);
41
            $router->setBaseUrl($website->getConfiguration()->getBaseUrl());
42
43
            $loop = Factory::create();
44
            $socket = new \React\Socket\Server(
45
                $input->getOption('bind') . ':' . $input->getOption('port'),
46
                $loop
47
            );
48
49
            $server = DevServer::create($router, $website->getCompiler());
50
            $server->listen($socket);
51
52
            $output->writeln('Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()));
53
54
            $loop->run();
55
        }
56
        catch (\Exception $e)
57
        {
58
            $output->writeln($e->getMessage());
59
        }
60
    }
61
}
62