-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAbstractInstallCommand.php
More file actions
92 lines (74 loc) · 3.25 KB
/
AbstractInstallCommand.php
File metadata and controls
92 lines (74 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Installer\Command;
use Shlinkio\Shlink\Installer\Command\Model\InitOption;
use Shlinkio\Shlink\Installer\Config\ConfigGeneratorInterface;
use Shlinkio\Shlink\Installer\Model\ImportedConfig;
use Shlinkio\Shlink\Installer\Service\ShlinkAssetsHandler;
use Shlinkio\Shlink\Installer\Service\ShlinkAssetsHandlerInterface;
use Shlinkio\Shlink\Installer\Util\ConfigWriterInterface;
use Shlinkio\Shlink\Installer\Util\Utils;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
abstract class AbstractInstallCommand extends Command
{
public function __construct(
private readonly ConfigWriterInterface $configWriter,
private readonly ShlinkAssetsHandlerInterface $assetsHandler,
private readonly ConfigGeneratorInterface $configGenerator,
) {
parent::__construct();
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$io->text([
'<info>Welcome to Shlink!!</info>',
'This tool will guide you through the installation process.',
]);
// Check if a cached config file exists and drop it if so
$this->assetsHandler->dropCachedConfigIfAny($io);
$importedConfig = $this->resolvePreviousConfig($io);
if ($this->isUpdate()) {
$this->assetsHandler->importShlinkAssetsFromPath($io, $importedConfig->importPath);
}
$config = $this->configGenerator->generateConfigInteractively($io, $importedConfig->importedConfig);
$normalizedConfig = Utils::normalizeAndKeepEnvVarKeys($config);
// Generate config params files
$this->configWriter->toFile(ShlinkAssetsHandler::GENERATED_CONFIG_PATH, $normalizedConfig);
$io->text('<info>Custom configuration properly generated!</info>');
$io->newLine();
if (! $this->execInitCommand($io, $importedConfig)) {
return -1;
}
$io->success('Installation complete!');
return 0;
}
private function resolvePreviousConfig(SymfonyStyle $io): ImportedConfig
{
if ($this->isUpdate()) {
return $this->assetsHandler->resolvePreviousConfig($io);
}
return ImportedConfig::notImported();
}
private function execInitCommand(SymfonyStyle $io, ImportedConfig $importedConfig): bool
{
$isUpdate = $this->isUpdate();
$input = [
InitOption::SKIP_INITIALIZE_DB->asCliFlag() => $isUpdate,
InitOption::CLEAR_DB_CACHE->asCliFlag() => $isUpdate,
InitOption::DOWNLOAD_RR_BINARY->asCliFlag() =>
$isUpdate && $this->assetsHandler->roadRunnerBinaryExistsInPath($importedConfig->importPath),
];
if (! $isUpdate) {
$input[InitOption::INITIAL_API_KEY->asCliFlag()] = null;
}
$command = $this->getApplication()?->find(InitCommand::NAME);
$exitCode = $command?->run(new ArrayInput($input), $io);
return $exitCode === 0;
}
abstract protected function isUpdate(): bool;
}