Completed
Push — master ( 7e0874...14a89c )
by Vladimir
04:01 queued 01:49
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
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Console\Command;
9
10
use allejo\stakx\Server\DevServer;
11
use allejo\stakx\Server\PageViewRouter;
12
use allejo\stakx\Website;
13
use React\EventLoop\Factory;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class ServeCommand extends BuildCommand
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23
    protected function configure()
24
    {
25
        parent::configure();
26
27
        $this->setName('serve');
28
        $this->setDescription('Start a web server serving the stakx website');
29
30
        $this->addOption('port', 'p', InputOption::VALUE_REQUIRED, 'The port the local development server will be listening on', 8000);
31
        $this->addOption('bind', null, InputOption::VALUE_REQUIRED, 'The IP the local development server will bind to', '0.0.0.0');
32
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output)
35
    {
36
        $this->handleDeprecations($input, $output);
37
        $this->setRunTimeOptions($input);
38
39
        try
40
        {
41
            $this->configureConfigurationFile($input);
42
            $website = $this->getContainer()->get(Website::class);
43
44
            /** @var PageViewRouter $router */
45
            $router = $this->getContainer()->get(PageViewRouter::class);
46
            $router->setBaseUrl($website->getConfiguration()->getBaseUrl());
47
48
            $loop = Factory::create();
49
            $socket = new \React\Socket\Server(
50
                $input->getOption('bind') . ':' . $input->getOption('port'),
51
                $loop
52
            );
53
54
            $server = DevServer::create($router, $website->getCompiler());
55
            $server->listen($socket);
56
57
            $output->writeln('Listening on ' . str_replace('tcp:', 'http:', $socket->getAddress()));
58
59
            $loop->run();
60
        }
61
        catch (\Exception $e)
62
        {
63
            $output->writeln($e->getMessage());
64
        }
65
    }
66
}
67