Completed
Pull Request — master (#6)
by Tomáš
04:54
created

GenerateCommand::execute()   C

Complexity

Conditions 8
Paths 32

Size

Total Lines 40
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 32
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 24
nc 32
nop 2
crap 72
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\Bundle\SculpinBundle\Command;
13
14
use Dflydev\DotAccessConfiguration\Configuration;
15
use Symplify\PHP7_Sculpin\Bundle\SculpinBundle\HttpServer\HttpServer;
16
use Symplify\PHP7_Sculpin\Core\Sculpin;
17
use Symplify\PHP7_Sculpin\Core\Source\CompositeDataSource;
18
use Symplify\PHP7_Sculpin\Core\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
        $this->addOption('clean', null, InputOption::VALUE_NONE, 'Cleans the output directory prior to generation.');
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('url', null, InputOption::VALUE_REQUIRED, 'Override URL.');
86
        $this->addOption('project-dir', null, InputOption::VALUE_REQUIRED, 'The project directory.', '.');
87
88
        $this->setHelp('The <info>generate</info> command generates a site.');
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    protected function execute(InputInterface $input, OutputInterface $output)
95
    {
96
        if ($input->getOption('clean')) {
97
            $this->clean($output, $this->outputDirectory);
98
        }
99
100
        $watch = $input->getOption('watch') ?: false;
101
102
        if ($url = $input->getOption('url')) {
103
            $this->configuration->set('url', $url);
104
        }
105
106
        $sourceSet = new SourceSet();
107
        if ($input->getOption('server')) {
108
            $this->sculpin->run($this->compositeDataSource, $sourceSet);
109
110
            $this->httpServer->init();
111
112
            if ($watch) {
113
                $this->httpServer->addPeriodicTimer(1, function () use ($sourceSet) {
114
                    clearstatcache();
115
                    $sourceSet->reset();
116
117
                    $this->sculpin->run($this->compositeDataSource, $sourceSet);
118
                });
119
            }
120
121
            $this->httpServer->run();
122
        } else {
123
            do {
124
                $this->sculpin->run($this->compositeDataSource, $sourceSet);
125
126
                if ($watch) {
127
                    sleep(2);
128
                    clearstatcache();
129
                    $sourceSet->reset();
130
                }
131
            } while ($watch);
132
        }
133
    }
134
135
    private function clean(OutputInterface $output, string $dir)
136
    {
137
        if ($this->filesystem->exists($dir)) {
138
            $output->writeln(sprintf('Deleting %s', $dir));
139
            $this->filesystem->remove($dir);
140
        }
141
    }
142
}
143