Completed
Pull Request — master (#68)
by Vladimir
03:57
created

BuildCommand::configureConfigurationFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Console\Command;
9
10
use allejo\stakx\Configuration;
11
use allejo\stakx\Exception\FileAwareException;
12
use allejo\stakx\Filesystem\File;
13
use allejo\stakx\Filesystem\FilesystemLoader as fs;
14
use allejo\stakx\RuntimeStatus;
15
use allejo\stakx\Service;
16
use allejo\stakx\Utilities\StrUtils;
17
use allejo\stakx\Website;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Input\InputOption;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
class BuildCommand extends ContainerAwareCommand
23
{
24
    const NO_CONF = 'no-conf';
25
    const NO_CLEAN = 'no-clean';
26
    const USE_DRAFTS = 'use-drafts';
27
    const WATCHING = 'watching';
28
    const USE_CACHE = 'use-cache';
29
    const SAFE_MODE = 'safe';
30
    const BUILD_PROFILE = 'profile';
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    protected function configure()
36
    {
37
        $this->addOption(self::NO_CONF, 'l', InputOption::VALUE_NONE, 'Build a stakx website without a configuration file');
38
39
        $this->addOption('conf', 'c', InputOption::VALUE_REQUIRED, 'The configuration file to be used', Configuration::DEFAULT_NAME);
40
        $this->addOption(self::SAFE_MODE, 's', InputOption::VALUE_NONE, 'Disable file system access from Twig');
41
        $this->addOption(self::NO_CLEAN, 'x', InputOption::VALUE_NONE, "Don't clean the _site before recompiling the website");
42
        $this->addOption(self::USE_DRAFTS, 'd', InputOption::VALUE_NONE, 'Publish all ContentItems marked as drafts');
43
        $this->addOption(self::USE_CACHE, null, InputOption::VALUE_NONE, 'Use the existing cache folder for building the website');
44
        $this->addOption(self::BUILD_PROFILE, null, InputOption::VALUE_NONE, 'Display a profile of Twig templates');
45
46
        $this->setName('build');
47
        $this->setDescription('Builds the stakx website');
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    protected function execute(InputInterface $input, OutputInterface $output)
54
    {
55
        $this->handleDeprecations($input, $output);
56
57
        try
58
        {
59
            $this->setRunTimeOptions($input);
60
            $this->configureConfigurationFile($input);
61
62
            $website = $this->getContainer()->get(Website::class);
63
            $website->build();
64
65
            $output->writeln(sprintf('Your site built successfully! It can be found at: %s',
66
                $website->getConfiguration()->getTargetFolder() . DIRECTORY_SEPARATOR
67
            ));
68
69
            return 0;
70
        }
71
        catch (FileAwareException $e)
72
        {
73
            $output->writeln(StrUtils::interpolate(
74
                "Your website failed to build with the following error in file '{file}'{line}: {message}", [
75
                    'file' => $e->getPath(),
76
                    'line' => (($l = $e->getLineNumber()) >= 0) ? ' on line ' . $l : '',
77
                    'message' => $e->getMessage(),
78
                ]
79
            ));
80
        }
81
        catch (\Exception $e)
82
        {
83
            $output->writeln(sprintf('Your website failed to build with the following error: %s',
84
                $e->getMessage()
85
            ));
86
        }
87
88
        return 1;
89
    }
90
91
    /**
92
     * @param InputInterface $input
93
     *
94
     * @throws \Exception
95
     */
96
    private function configureConfigurationFile(InputInterface $input)
97
    {
98
        $confFilePath = $input->getOption('conf');
99
        $siteRoot = fs::getFolderPath(realpath($confFilePath));
0 ignored issues
show
Documentation introduced by
realpath($confFilePath) is of type string, but the function expects a object<string>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
        Service::setWorkingDirectory($siteRoot);
101
102
        $configFile = new File($confFilePath);
103
104
        /** @var Configuration $conf */
105
        $conf = $this->getContainer()->get(Configuration::class);
106
        $conf->parse($configFile);
107
    }
108
109
    private function setRunTimeOptions(InputInterface $input)
110
    {
111
        if ($input->getOption(self::NO_CLEAN))
112
        {
113
            Service::setRuntimeFlag(RuntimeStatus::BOOT_WITHOUT_CLEAN);
114
        }
115
116
        if ($input->getOption(self::USE_DRAFTS))
117
        {
118
            Service::setRuntimeFlag(RuntimeStatus::USING_DRAFTS);
119
        }
120
121
        if ($input->getOption(self::USE_CACHE))
122
        {
123
            Service::setRuntimeFlag(RuntimeStatus::USING_CACHE);
124
        }
125
126
        if ($input->getOption(self::SAFE_MODE))
127
        {
128
            Service::setRuntimeFlag(RuntimeStatus::IN_SAFE_MODE);
129
        }
130
131
        if ($input->getOption(self::BUILD_PROFILE))
132
        {
133
            Service::setRuntimeFlag(RuntimeStatus::IN_PROFILE_MODE);
134
        }
135
    }
136
137
    private function handleDeprecations(InputInterface $input, OutputInterface $output)
138
    {
139
        if ($input->getOption(self::NO_CONF))
140
        {
141
            $output->writeln('Deprecation: The "--no-conf" option is no longer supported. You must have a configuration file.');
142
        }
143
    }
144
}
145