forked from shlinkio/shlink-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetOptionCommand.php
More file actions
84 lines (71 loc) · 2.98 KB
/
SetOptionCommand.php
File metadata and controls
84 lines (71 loc) · 2.98 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
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Installer\Command;
use Generator;
use Shlinkio\Shlink\Installer\Config\ConfigOptionsManagerInterface;
use Shlinkio\Shlink\Installer\Config\Option\ConfigOptionInterface;
use Shlinkio\Shlink\Installer\Exception\InvalidShlinkPathException;
use Shlinkio\Shlink\Installer\Service\ShlinkAssetsHandler;
use Shlinkio\Shlink\Installer\Service\ShlinkAssetsHandlerInterface;
use Shlinkio\Shlink\Installer\Util\ArrayUtils;
use Shlinkio\Shlink\Installer\Util\ConfigWriterInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Filesystem;
use function array_filter;
use function array_keys;
use function getcwd;
use function is_iterable;
use function is_numeric;
use function iterator_to_array;
#[AsCommand(SetOptionCommand::NAME, 'Allows you to set new values for any config option')]
class SetOptionCommand extends Command
{
public const string NAME = 'set-option';
private array $groups;
private string $generatedConfigPath;
public function __construct(
private readonly ConfigWriterInterface $configWriter,
private readonly ShlinkAssetsHandlerInterface $assetsHandler,
private readonly ConfigOptionsManagerInterface $optionsManager,
private readonly Filesystem $filesystem,
array $groups,
array|null $enabledOptions,
) {
parent::__construct();
$this->groups = array_filter(
iterator_to_array($this->flattenGroupsWithTitle($groups)),
static fn (string $configOption) => $enabledOptions === null || ArrayUtils::contains(
$configOption,
$enabledOptions,
),
);
$this->generatedConfigPath = getcwd() . '/' . ShlinkAssetsHandler::GENERATED_CONFIG_PATH;
}
private function flattenGroupsWithTitle(iterable $groups): Generator
{
foreach ($groups as $key => $value) {
if (is_iterable($value)) {
yield from $this->flattenGroupsWithTitle($value);
} elseif (! is_numeric($key)) {
yield $key => $value;
}
}
}
public function __invoke(SymfonyStyle $io): int
{
if (! $this->filesystem->exists($this->generatedConfigPath)) {
throw InvalidShlinkPathException::forCurrentPath();
}
$optionTitle = $io->choice('What config option do you want to change', array_keys($this->groups));
/** @var ConfigOptionInterface $plugin */
$plugin = $this->optionsManager->get($this->groups[$optionTitle]);
$answers = include $this->generatedConfigPath;
$answers[$plugin->getEnvVar()] = $plugin->ask($io, $answers);
$this->configWriter->toFile($this->generatedConfigPath, $answers);
$this->assetsHandler->dropCachedConfigIfAny($io);
$io->success('Configuration properly updated');
return self::SUCCESS;
}
}