Completed
Pull Request — master (#10)
by Tomáš
03:17
created

GenerateCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 6
dl 0
loc 103
ccs 0
cts 52
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A configure() 0 11 1
B execute() 0 34 6
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\SculpinBundle\Command;
13
14
use Dflydev\DotAccessConfiguration\Configuration;
15
use Symplify\PHP7_Sculpin\SculpinBundle\HttpServer\HttpServer;
16
use Symplify\PHP7_Sculpin\Sculpin;
17
use Symplify\PHP7_Sculpin\Source\CompositeDataSource;
18
use Symplify\PHP7_Sculpin\Source\SourceSet;
19
use Symfony\Component\Console\Command\Command;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
use Symfony\Component\Filesystem\Filesystem;
24
25
final class GenerateCommand extends Command
26
{
27
    /**
28
     * @var string
29
     */
30
    private $outputDirectory;
31
32
    /**
33
     * @var Filesystem
34
     */
35
    private $filesystem;
36
37
    /**
38
     * @var Sculpin
39
     */
40
    private $sculpin;
41
42
    /**
43
     * @var CompositeDataSource
44
     */
45
    private $compositeDataSource;
46
47
    /**
48
     * @var Configuration
49
     */
50
    private $configuration;
51
52
    /**
53
     * @var HttpServer
54
     */
55
    private $httpServer;
56
57
    public function __construct(
58
        string $outputDirectory,
59
        Filesystem $filesystem,
60
        Sculpin $sculpin,
61
        CompositeDataSource $compositeDataSource,
62
        Configuration $configuration,
63
        HttpServer $httpServer
64
    ) {
65
        $this->filesystem = $filesystem;
66
        $this->sculpin = $sculpin;
67
        $this->compositeDataSource = $compositeDataSource;
68
        $this->configuration = $configuration;
69
        $this->outputDirectory = $outputDirectory;
70
        $this->httpServer = $httpServer;
71
72
        parent::__construct();
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    protected function configure()
79
    {
80
        $this->setName('generate');
81
        $this->setDescription('Generate a site from source.');
82
83
        $this->addOption('watch', null, InputOption::VALUE_NONE, 'Watch source and regenerate site as changes are made.');
84
        $this->addOption('server', null, InputOption::VALUE_NONE, 'Start an HTTP server to host your generated site');
85
        $this->addOption('project-dir', null, InputOption::VALUE_REQUIRED, 'The project directory.', '.');
86
87
        $this->setHelp('The <info>generate</info> command generates a site.');
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    protected function execute(InputInterface $input, OutputInterface $output)
94
    {
95
        $this->filesystem->remove($this->outputDirectory);
96
97
        $watch = $input->getOption('watch') ?: false;
98
99
        $sourceSet = new SourceSet();
100
        if ($input->getOption('server')) {
101
            $this->sculpin->run($this->compositeDataSource, $sourceSet);
102
103
            $this->httpServer->init();
104
105
            if ($watch) {
106
                $this->httpServer->addPeriodicTimer(1, function () use ($sourceSet) {
107
                    clearstatcache();
108
                    $sourceSet->reset();
109
110
                    $this->sculpin->run($this->compositeDataSource, $sourceSet);
111
                });
112
            }
113
114
            $this->httpServer->run();
115
        } else {
116
            do {
117
                $this->sculpin->run($this->compositeDataSource, $sourceSet);
118
119
                if ($watch) {
120
                    sleep(2);
121
                    clearstatcache();
122
                    $sourceSet->reset();
123
                }
124
            } while ($watch);
125
        }
126
    }
127
}
128