Completed
Pull Request — master (#4)
by Tomáš
03:11
created

GenerateCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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 Sculpin\Bundle\SculpinBundle\Command;
13
14
use Sculpin\Bundle\SculpinBundle\HttpServer\HttpServer;
15
use Sculpin\Core\Console\Command\ContainerAwareCommand;
16
use Sculpin\Core\Io\ConsoleIo;
17
use 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\Console\Question\ConfirmationQuestion;
23
use Symfony\Component\DependencyInjection\ContainerInterface;
24
25
final class GenerateCommand extends Command
26
{
27
    /**
28
     * @var ContainerInterface
29
     */
30
    private $container;
31
32
    public function __construct(ContainerInterface $container)
33
    {
34
        $this->container = $container;
35
36
        parent::__construct();
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function configure()
43
    {
44
        $this
45
            ->setName('generate')
46
            ->setDescription('Generate a site from source.')
47
            ->setDefinition([
48
                new InputOption('clean', null, InputOption::VALUE_NONE, 'Cleans the output directory prior to generation.'),
49
                new InputOption('watch', null, InputOption::VALUE_NONE, 'Watch source and regenerate site as changes are made.'),
50
                new InputOption('server', null, InputOption::VALUE_NONE, 'Start an HTTP server to host your generated site'),
51
                new InputOption('url', null, InputOption::VALUE_REQUIRED, 'Override URL.'),
52
                new InputOption('port', null, InputOption::VALUE_REQUIRED, 'Port'),
53
            ])
54
            ->setHelp('The <info>generate</info> command generates a site.');
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected function execute(InputInterface $input, OutputInterface $output)
61
    {
62
        $docroot = $this->container->getParameter('sculpin.output_dir');
63
        if ($input->getOption('clean')) {
64
            $this->clean($input, $output, $docroot);
65
        }
66
67
        $watch = $input->getOption('watch') ?: false;
68
        $sculpin = $this->container->get('sculpin');
69
        $dataSource = $this->container->get('sculpin.data_source');
70
        $sourceSet = new SourceSet();
71
72
        $config = $this->container->get('sculpin.site_configuration');
73
        if ($url = $input->getOption('url')) {
74
            $config->set('url', $url);
75
        }
76
77
        $consoleIo = new ConsoleIo($input, $output, $this->getApplication()->getHelperSet());
0 ignored issues
show
Unused Code introduced by
The call to ConsoleIo::__construct() has too many arguments starting with $this->getApplication()->getHelperSet().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
78
79
        if ($input->getOption('server')) {
80
            $sculpin->run($dataSource, $sourceSet, $consoleIo);
81
82
            $kernel = $this->container->get('kernel');
83
84
            $httpServer = new HttpServer(
85
                $output,
86
                $docroot,
87
                $kernel->getEnvironment(),
88
                $kernel->isDebug(),
89
                $input->getOption('port')
90
            );
91
92
            if ($watch) {
93
                $httpServer->addPeriodicTimer(1, function () use ($sculpin, $dataSource, $sourceSet, $consoleIo) {
94
                    clearstatcache();
95
                    $sourceSet->reset();
96
97
                    $sculpin->run($dataSource, $sourceSet, $consoleIo);
98
                });
99
            }
100
101
            $httpServer->run();
102
        } else {
103
            do {
104
                $sculpin->run($dataSource, $sourceSet, $consoleIo);
105
106
                if ($watch) {
107
                    sleep(2);
108
                    clearstatcache();
109
                    $sourceSet->reset();
110
                }
111
            } while ($watch);
112
        }
113
    }
114
115
    /**
116
     * Cleanup an output directory by deleting it.
117
     *
118
     * @param InputInterface  $input  An InputInterface instance
119
     * @param OutputInterface $output An OutputInterface instance
120
     * @param string          $dir    The directory to remove
121
     */
122
    protected function clean(InputInterface $input, OutputInterface $output, $dir)
123
    {
124
        $fileSystem = $this->container->get('filesystem');
125
        if ($fileSystem->exists($dir)) {
126
            if ($input->isInteractive()) {
127
                // Prompt the user for confirmation.
128
                /** @var \Symfony\Component\Console\Helper\QuestionHelper $helper */
129
                $helper = $this->getHelper('question');
130
                $question = new ConfirmationQuestion(sprintf('Are you sure you want to delete all the contents of the %s directory?', $dir), false);
131
                if (!$helper->ask($input, $output, $question)) {
132
                    return;
133
                }
134
            }
135
            $output->writeln(sprintf('Deleting %s', $dir));
136
            $fileSystem->remove($dir);
137
        }
138
    }
139
}
140