Updated ApplicationConfigCustomizer to support new keys in the future

This commit is contained in:
Alejandro Celaya 2018-09-30 18:20:27 +02:00
parent 3a75ac0486
commit 757cf2e193
4 changed files with 42 additions and 12 deletions

View file

@ -6,28 +6,54 @@ namespace Shlinkio\Shlink\Installer\Config\Plugin;
use Shlinkio\Shlink\Common\Util\StringUtilsTrait;
use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig;
use Symfony\Component\Console\Style\SymfonyStyle;
use function array_diff;
use function array_keys;
class ApplicationConfigCustomizer implements ConfigCustomizerInterface
{
use StringUtilsTrait;
private const SECRET = 'SECRET';
private const DISABLE_TRACK_PARAM = 'DISABLE_TRACK_PARAM';
private const EXPECTED_KEYS = [
self::SECRET,
self::DISABLE_TRACK_PARAM,
];
public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
{
$io->title('APPLICATION');
if ($appConfig->hasApp() && $io->confirm('Do you want to keep imported application config?')) {
$app = $appConfig->getApp();
$keysToAskFor = $appConfig->hasApp() && $io->confirm('Do you want to keep imported application config?')
? array_diff(self::EXPECTED_KEYS, array_keys($app))
: self::EXPECTED_KEYS;
if (empty($keysToAskFor)) {
return;
}
$appConfig->setApp([
'SECRET' => $io->ask(
'Define a secret string that will be used to sign API tokens (leave empty to autogenerate one) '
. '<fg=red>[DEPRECATED. TO BE REMOVED]</>'
) ?: $this->generateRandomString(32),
'DISABLE_TRACK_PARAM' => $io->ask(
'Provide a parameter name that you will be able to use to disable tracking on specific request to '
. 'short URLs (leave empty and this feature won\'t be enabled)'
),
]);
foreach ($keysToAskFor as $key) {
$app[$key] = $this->ask($io, $key);
}
$appConfig->setApp($app);
}
private function ask(SymfonyStyle $io, string $key)
{
switch ($key) {
case self::SECRET:
return $io->ask(
'Define a secret string that will be used to sign API tokens (leave empty to autogenerate one) '
. '<fg=red>[DEPRECATED. TO BE REMOVED]</>'
) ?: $this->generateRandomString(32);
case self::DISABLE_TRACK_PARAM:
return $io->ask(
'Provide a parameter name that you will be able to use to disable tracking on specific request to '
. 'short URLs (leave empty and this feature won\'t be enabled)'
);
}
return '';
}
}

View file

@ -8,6 +8,7 @@ use Shlinkio\Shlink\Installer\Util\AskUtilsTrait;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use function array_keys;
class DatabaseConfigCustomizer implements ConfigCustomizerInterface
{
@ -55,7 +56,7 @@ class DatabaseConfigCustomizer implements ConfigCustomizerInterface
// Select database type
$params = [];
$databases = \array_keys(self::DATABASE_DRIVERS);
$databases = array_keys(self::DATABASE_DRIVERS);
$dbType = $io->choice('Select database type', $databases, $databases[0]);
$params['DRIVER'] = self::DATABASE_DRIVERS[$dbType];

View file

@ -114,6 +114,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface
{
$this->setApp([
'SECRET' => $array['app_options']['secret_key'] ?? null,
'DISABLE_TRACK_PARAM' => $array['app_options']['disable_track_param'] ?? null,
]);
$this->deserializeDatabase($array['entity_manager']['connection'] ?? []);

View file

@ -79,12 +79,14 @@ class ApplicationConfigCustomizerTest extends TestCase
$config = new CustomizableAppConfig();
$config->setApp([
'SECRET' => 'foo',
'DISABLE_TRACK_PARAM' => 'the_new_secret',
]);
$this->plugin->process($this->io->reveal(), $config);
$this->assertEquals([
'SECRET' => 'foo',
'DISABLE_TRACK_PARAM' => 'the_new_secret',
], $config->getApp());
$confirm->shouldHaveBeenCalledTimes(1);
}