Completed
Pull Request — master (#4187)
by Craig
04:32
created

StartCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 6
dl 0
loc 14
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula Foundation - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\CoreInstallerBundle\Command\Install;
15
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
21
use Symfony\Component\Filesystem\Filesystem;
22
use Symfony\Contracts\Translation\TranslatorInterface;
23
use Zikula\Bundle\CoreBundle\Helper\LocalDotEnvHelper;
24
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
25
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
26
use Zikula\Bundle\CoreInstallerBundle\Command\AbstractCoreInstallerCommand;
27
use Zikula\Bundle\CoreInstallerBundle\Form\Type\CreateAdminType;
28
use Zikula\Bundle\CoreInstallerBundle\Form\Type\DbCredsType;
29
use Zikula\Bundle\CoreInstallerBundle\Form\Type\LocaleType;
30
use Zikula\Bundle\CoreInstallerBundle\Form\Type\RequestContextType;
31
use Zikula\Bundle\CoreInstallerBundle\Helper\ControllerHelper;
32
use Zikula\Bundle\CoreInstallerBundle\Helper\DbCredsHelper;
33
use Zikula\Bundle\CoreInstallerBundle\Helper\ParameterHelper;
34
use Zikula\Component\Wizard\AbortStageException;
35
use Zikula\SettingsModule\Api\ApiInterface\LocaleApiInterface;
36
37
class StartCommand extends AbstractCoreInstallerCommand
38
{
39
    protected static $defaultName = 'zikula:install:start';
40
41
    /**
42
     * @var bool
43
     */
44
    private $installed;
45
46
    /**
47
     * @var ControllerHelper
48
     */
49
    private $controllerHelper;
50
51
    /**
52
     * @var LocaleApiInterface
53
     */
54
    private $localeApi;
55
56
    /**
57
     * @var ParameterHelper
58
     */
59
    private $parameterHelper;
60
61
    public function __construct(
62
        ZikulaHttpKernelInterface $kernel,
63
        string $installed,
64
        ControllerHelper $controllerHelper,
65
        LocaleApiInterface $localeApi,
66
        ParameterHelper $parameterHelper,
67
        TranslatorInterface $translator
68
    ) {
69
        $this->kernel = $kernel;
70
        $this->installed = '0.0.0' !== $installed;
71
        $this->controllerHelper = $controllerHelper;
72
        $this->localeApi = $localeApi;
73
        $this->parameterHelper = $parameterHelper;
74
        parent::__construct($kernel, $translator);
75
    }
76
77
    protected function configure()
78
    {
79
        $this->setDescription('call this command first');
80
81
        foreach ($this->settings as $name => $setting) {
82
            $this->addOption(
83
                $name,
84
                null,
85
                InputOption::VALUE_REQUIRED,
86
                $setting['description'],
87
                $setting['default']
88
            );
89
        }
90
    }
91
92
    protected function execute(InputInterface $input, OutputInterface $output): int
93
    {
94
        $io = new SymfonyStyle($input, $output);
95
        $io->title($this->translator->trans('Zikula Installer Script'));
96
97
        if (true === $this->installed) {
98
            $io->error($this->translator->trans('Zikula already appears to be installed.'));
99
100
            return 1;
101
        }
102
103
        $warnings = $this->controllerHelper->initPhp();
104
        if (!empty($warnings)) {
105
            $this->printWarnings($output, $warnings);
106
107
            return 2;
108
        }
109
        $checks = $this->controllerHelper->requirementsMet();
110
        if (true !== $checks) {
0 ignored issues
show
introduced by
The condition true !== $checks is always true.
Loading history...
111
            $this->printRequirementsWarnings($output, $checks);
112
113
            return 2;
114
        }
115
116
        if ($input->isInteractive()) {
117
            $io->comment($this->translator->trans('Configuring Zikula installation in %env% environment.', ['%env%' => $this->kernel->getEnvironment()]));
118
            $io->comment($this->translator->trans('Please follow the instructions to install Zikula %version%.', ['%version%' => ZikulaKernel::VERSION]));
119
        }
120
121
        // get the settings from user input
122
        $settings = $this->getHelper('form')->interactUsingForm(LocaleType::class, $input, $output, [
123
            'choices' => $this->localeApi->getSupportedLocaleNames(),
124
            'choice_loader' => null
125
        ]);
126
        $data = $this->getHelper('form')->interactUsingForm(RequestContextType::class, $input, $output);
127
        foreach ($data as $k => $v) {
128
            $newKey = str_replace(':', '.', $k);
129
            $data[$newKey] = $v;
130
            unset($data[$k]);
131
        }
132
        $settings = array_merge($settings, $data);
133
        $data = $this->getHelper('form')->interactUsingForm(DbCredsType::class, $input, $output);
134
135
        $dbCredsHelper = new DbCredsHelper();
136
        $databaseUrl = $dbCredsHelper->buildDatabaseUrl($data);
137
        try {
138
            $vars = ['DATABASE_URL' => '\'' . $databaseUrl . '\''];
139
            $helper = new LocalDotEnvHelper($this->kernel->getProjectDir());
140
            $helper->writeLocalEnvVars($vars);
141
        } catch (IOExceptionInterface $exception) {
142
            $io->error(sprintf('Cannot write to %s file.', $this->kernel->getProjectDir() . '/.env.local') . ' ' . $exception->getMessage());
143
        }
144
145
        $data = $this->getHelper('form')->interactUsingForm(CreateAdminType::class, $input, $output);
146
        foreach ($data as $k => $v) {
0 ignored issues
show
Comprehensibility Bug introduced by
$v is overwriting a variable from outer foreach loop.
Loading history...
Comprehensibility Bug introduced by
$k is overwriting a variable from outer foreach loop.
Loading history...
147
            $data[$k] = base64_encode($v); // encode so values are 'safe' for json
148
        }
149
        $settings = array_merge($settings, $data);
150
151
        if ($input->isInteractive()) {
152
            $io->success($this->translator->trans('Configuration successful. Please verify your parameters below:'));
153
            $io->comment($this->translator->trans('(Admin credentials have been encoded to make them json-safe.)'));
154
        }
155
156
        $this->printSettings($settings, $io);
157
        $io->newLine();
158
159
        if ($input->isInteractive()) {
160
            $confirmation = $io->confirm($this->translator->trans('Start installation?'), true);
161
162
            if (!$confirmation) {
163
                $io->error($this->translator->trans('Installation aborted'));
164
165
                return 3;
166
            }
167
        }
168
169
        // write parameters into config/services_custom.yaml and env vars into .env.local
170
        $this->parameterHelper->initializeParameters($settings);
171
172
        $io->success($this->translator->trans('First stage of installation complete. Run `php bin/console zikula:install:finish` to complete the installation.'));
173
174
        return 0;
175
    }
176
}
177