StartServer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 4
c 3
b 0
f 0
lcom 0
cbo 6
dl 0
loc 58
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 12 2
A startWebServer() 0 20 1
1
<?php
2
3
namespace Dmitrynaum\SAM\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\Process\ProcessBuilder;
9
10
/**
11
 * Description of StartServer
12
 *
13
 * @author Naumov Dmitry <[email protected]>
14
 */
15
class StartServer extends Command
16
{
17
18
    protected function configure()
19
    {
20
        $this
21
            ->setName('start-server')
22
            ->setDescription('Starts a webserver to building assets in realtime (DEVELOPMENT ONLY!)')
23
            ->addArgument('manifest', null, 'Path to manifest file.', 'sam.json');
24
    }
25
26
    /**
27
     * Выполнить команду
28
     * @param InputInterface $input
29
     * @param OutputInterface $output
30
     * @throws \RuntimeException
31
     */
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
35
        $pathToServerWorker = realpath(__DIR__ . '/../../bin/');
36
        $serverProcess      = $this->startWebServer($input, $output, $pathToServerWorker);
37
38
        while ($serverProcess->isRunning()) {
39
            sleep(1);
40
        }
41
42
        throw new \RuntimeException('The HTTP server has stopped');
43
    }
44
45
    /**
46
     * Запустить сервер
47
     * @param InputInterface $input
48
     * @param OutputInterface $output
49
     * @param string $targetDirectory
50
     * @return \Symfony\Component\Process\Process
51
     */
52
    private function startWebServer(InputInterface $input, OutputInterface $output, $targetDirectory)
53
    {
54
        $manifestPath = getcwd() . '/' . $input->getArgument('manifest');
55
        
56
        $manifest = new \Dmitrynaum\SAM\Component\Manifest($manifestPath);
57
        $address  = $manifest->getServerAddress();
58
        
59
        $builder = new ProcessBuilder([PHP_BINARY, '-S', $address, 'server.php']);
60
        
61
        $builder->setWorkingDirectory($targetDirectory);
62
        $builder->setEnv('projectDir', getcwd());
63
        $builder->setEnv('manifestPath', $manifestPath);
64
        $builder->setTimeout(null);
65
        
66
        $process = $builder->getProcess();
67
        $process->start();
68
        $output->writeln(sprintf('Server running on <comment>%s</comment>', $address));
69
        
70
        return $process;
71
    }
72
}
73