1 | <?php |
||
20 | class InstallCommand extends Command |
||
21 | { |
||
22 | const GENERATED_CONFIG_PATH = 'config/params/generated_config.php'; |
||
23 | |||
24 | /** |
||
25 | * @var InputInterface |
||
26 | */ |
||
27 | private $input; |
||
28 | /** |
||
29 | * @var OutputInterface |
||
30 | */ |
||
31 | private $output; |
||
32 | /** |
||
33 | * @var QuestionHelper |
||
34 | */ |
||
35 | private $questionHelper; |
||
36 | /** |
||
37 | * @var ProcessHelper |
||
38 | */ |
||
39 | private $processHelper; |
||
40 | /** |
||
41 | * @var WriterInterface |
||
42 | */ |
||
43 | private $configWriter; |
||
44 | /** |
||
45 | * @var Filesystem |
||
46 | */ |
||
47 | private $filesystem; |
||
48 | /** |
||
49 | * @var ConfigCustomizerPluginManagerInterface |
||
50 | */ |
||
51 | private $configCustomizers; |
||
52 | /** |
||
53 | * @var bool |
||
54 | */ |
||
55 | private $isUpdate; |
||
56 | |||
57 | /** |
||
58 | * InstallCommand constructor. |
||
59 | * @param WriterInterface $configWriter |
||
60 | * @param Filesystem $filesystem |
||
61 | * @param bool $isUpdate |
||
62 | * @throws LogicException |
||
63 | */ |
||
64 | 5 | public function __construct( |
|
76 | |||
77 | 5 | public function configure() |
|
78 | { |
||
79 | $this |
||
80 | 5 | ->setName('shlink:install') |
|
81 | 5 | ->setDescription('Installs or updates Shlink'); |
|
82 | 5 | } |
|
83 | |||
84 | 4 | public function execute(InputInterface $input, OutputInterface $output) |
|
85 | { |
||
86 | 4 | $this->input = $input; |
|
87 | 4 | $this->output = $output; |
|
88 | 4 | $this->questionHelper = $this->getHelper('question'); |
|
89 | 4 | $this->processHelper = $this->getHelper('process'); |
|
90 | |||
91 | 4 | $output->writeln([ |
|
92 | 4 | '<info>Welcome to Shlink!!</info>', |
|
93 | 'This will guide you through the installation process.', |
||
94 | ]); |
||
95 | |||
96 | // Check if a cached config file exists and drop it if so |
||
97 | 4 | if ($this->filesystem->exists('data/cache/app_config.php')) { |
|
98 | 2 | $output->write('Deleting old cached config...'); |
|
99 | try { |
||
100 | 2 | $this->filesystem->remove('data/cache/app_config.php'); |
|
101 | 1 | $output->writeln(' <info>Success</info>'); |
|
102 | 1 | } catch (IOException $e) { |
|
103 | 1 | $output->writeln( |
|
104 | ' <error>Failed!</error> You will have to manually delete the data/cache/app_config.php file to get' |
||
105 | 1 | . ' new config applied.' |
|
106 | ); |
||
107 | 1 | if ($output->isVerbose()) { |
|
108 | $this->getApplication()->renderException($e, $output); |
||
109 | } |
||
110 | 1 | return; |
|
111 | } |
||
112 | } |
||
113 | |||
114 | // If running update command, ask the user to import previous config |
||
115 | 3 | $config = $this->isUpdate ? $this->importConfig() : new CustomizableAppConfig(); |
|
116 | |||
117 | // Ask for custom config params |
||
118 | foreach ([ |
||
119 | 3 | Plugin\DatabaseConfigCustomizerPlugin::class, |
|
120 | Plugin\UrlShortenerConfigCustomizerPlugin::class, |
||
121 | Plugin\LanguageConfigCustomizerPlugin::class, |
||
122 | Plugin\ApplicationConfigCustomizerPlugin::class, |
||
123 | ] as $pluginName) { |
||
124 | /** @var Plugin\ConfigCustomizerPluginInterface $configCustomizer */ |
||
125 | 3 | $configCustomizer = $this->configCustomizers->get($pluginName); |
|
126 | 3 | $configCustomizer->process($input, $output, $config); |
|
127 | } |
||
128 | |||
129 | // Generate config params files |
||
130 | 3 | $this->configWriter->toFile(self::GENERATED_CONFIG_PATH, $config->getArrayCopy(), false); |
|
131 | 3 | $output->writeln(['<info>Custom configuration properly generated!</info>', '']); |
|
132 | |||
133 | // If current command is not update, generate database |
||
134 | 3 | if (! $this->isUpdate) { |
|
135 | 2 | $this->output->writeln('Initializing database...'); |
|
136 | 2 | if (! $this->runCommand( |
|
137 | 2 | 'php vendor/bin/doctrine.php orm:schema-tool:create', |
|
138 | 2 | 'Error generating database.' |
|
139 | )) { |
||
140 | return; |
||
141 | } |
||
142 | } |
||
143 | |||
144 | // Run database migrations |
||
145 | 3 | $output->writeln('Updating database...'); |
|
146 | 3 | if (! $this->runCommand('php vendor/bin/doctrine-migrations migrations:migrate', 'Error updating database.')) { |
|
147 | return; |
||
148 | } |
||
149 | |||
150 | // Generate proxies |
||
151 | 3 | $output->writeln('Generating proxies...'); |
|
152 | 3 | if (! $this->runCommand('php vendor/bin/doctrine.php orm:generate-proxies', 'Error generating proxies.')) { |
|
153 | return; |
||
154 | } |
||
155 | 3 | } |
|
156 | |||
157 | /** |
||
158 | * @return CustomizableAppConfig |
||
159 | * @throws RuntimeException |
||
160 | */ |
||
161 | 1 | private function importConfig() |
|
199 | |||
200 | /** |
||
201 | * @param string $text |
||
202 | * @param string|null $default |
||
203 | * @param bool $allowEmpty |
||
204 | * @return string |
||
205 | * @throws RuntimeException |
||
206 | */ |
||
207 | 1 | private function ask($text, $default = null, $allowEmpty = false) |
|
208 | { |
||
209 | 1 | if ($default !== null) { |
|
210 | $text .= ' (defaults to ' . $default . ')'; |
||
211 | } |
||
212 | do { |
||
213 | 1 | $value = $this->questionHelper->ask($this->input, $this->output, new Question( |
|
214 | 1 | '<question>' . $text . ':</question> ', |
|
215 | 1 | $default |
|
216 | )); |
||
217 | 1 | if (empty($value) && ! $allowEmpty) { |
|
218 | $this->output->writeln('<error>Value can\'t be empty</error>'); |
||
219 | } |
||
220 | 1 | } while (empty($value) && $default === null && ! $allowEmpty); |
|
221 | |||
222 | 1 | return $value; |
|
223 | } |
||
224 | |||
225 | /** |
||
226 | * @param string $command |
||
227 | * @param string $errorMessage |
||
228 | * @return bool |
||
229 | */ |
||
230 | 3 | private function runCommand($command, $errorMessage) |
|
247 | } |
||
248 |