Completed
Pull Request — master (#9)
by Tomáš
05:02
created

GenerateCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 8
cp 0
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 2
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 Symplify\PHP7_Sculpin\Bundle\SculpinBundle\HttpServer\HttpServer;
15
use Symplify\PHP7_Sculpin\Core\Sculpin;
16
use Symplify\PHP7_Sculpin\Core\Source\CompositeDataSource;
17
use Symplify\PHP7_Sculpin\Core\Source\SourceSet;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
use Symfony\Component\Filesystem\Filesystem;
23
24
final class GenerateCommand extends Command
25
{
26
    /**
27
     * @var string
28
     */
29
    private $outputDirectory;
30
31
    /**
32
     * @var Filesystem
33
     */
34
    private $filesystem;
35
36
    /**
37
     * @var Sculpin
38
     */
39
    private $sculpin;
40
41
    /**
42
     * @var CompositeDataSource
43
     */
44
    private $compositeDataSource;
45
46
    /**
47
     * @var HttpServer
48
     */
49
    private $httpServer;
50
51
    public function __construct(
52
        string $outputDirectory,
53
        Filesystem $filesystem,
54
        Sculpin $sculpin,
55
        CompositeDataSource $compositeDataSource,
56
        HttpServer $httpServer
57
    ) {
58
        $this->filesystem = $filesystem;
59
        $this->sculpin = $sculpin;
60
        $this->compositeDataSource = $compositeDataSource;
61
        $this->outputDirectory = $outputDirectory;
62
        $this->httpServer = $httpServer;
63
64
        parent::__construct();
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    protected function configure()
71
    {
72
        $this->setName('generate');
73
        $this->setDescription('Generate a site from source.');
74
75
        $this->addOption('watch', null, InputOption::VALUE_NONE, 'Watch source and regenerate site as changes are made.');
76
        $this->addOption('server', null, InputOption::VALUE_NONE, 'Start an HTTP server to host your generated site.');
77
        $this->addOption('project-dir', null, InputOption::VALUE_REQUIRED, 'The project directory.', '.');
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    protected function execute(InputInterface $input, OutputInterface $output)
84
    {
85
        $this->filesystem->remove($this->outputDirectory);
86
87
        $watch = $input->getOption('watch') ?: false;
88
89
        $sourceSet = new SourceSet();
90
        if ($input->getOption('server')) {
91
            $this->sculpin->run($this->compositeDataSource, $sourceSet);
92
93
            $this->httpServer->init();
94
95
            if ($watch) {
96
                $this->httpServer->addPeriodicTimer(1, function () use ($sourceSet) {
97
                    clearstatcache();
98
                    $sourceSet->reset();
99
100
                    $this->sculpin->run($this->compositeDataSource, $sourceSet);
101
                });
102
            }
103
104
            $this->httpServer->run();
105
        } else {
106
            do {
107
                $this->sculpin->run($this->compositeDataSource, $sourceSet);
108
109
                if ($watch) {
110
                    sleep(2);
111
                    clearstatcache();
112
                    $sourceSet->reset();
113
                }
114
            } while ($watch);
115
        }
116
    }
117
}
118