From 757cf2e193da4ed7ba399fcf7b32cdb608276d08 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 30 Sep 2018 18:20:27 +0200 Subject: [PATCH] Updated ApplicationConfigCustomizer to support new keys in the future --- .../Plugin/ApplicationConfigCustomizer.php | 48 ++++++++++++++----- .../Plugin/DatabaseConfigCustomizer.php | 3 +- .../src/Model/CustomizableAppConfig.php | 1 + .../ApplicationConfigCustomizerTest.php | 2 + 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/module/Installer/src/Config/Plugin/ApplicationConfigCustomizer.php b/module/Installer/src/Config/Plugin/ApplicationConfigCustomizer.php index 74c8ac16..fbd9c6a4 100644 --- a/module/Installer/src/Config/Plugin/ApplicationConfigCustomizer.php +++ b/module/Installer/src/Config/Plugin/ApplicationConfigCustomizer.php @@ -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) ' - . '[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) ' + . '[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 ''; } } diff --git a/module/Installer/src/Config/Plugin/DatabaseConfigCustomizer.php b/module/Installer/src/Config/Plugin/DatabaseConfigCustomizer.php index c6ada322..3868dc74 100644 --- a/module/Installer/src/Config/Plugin/DatabaseConfigCustomizer.php +++ b/module/Installer/src/Config/Plugin/DatabaseConfigCustomizer.php @@ -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]; diff --git a/module/Installer/src/Model/CustomizableAppConfig.php b/module/Installer/src/Model/CustomizableAppConfig.php index 49929a0b..cbb50d30 100644 --- a/module/Installer/src/Model/CustomizableAppConfig.php +++ b/module/Installer/src/Model/CustomizableAppConfig.php @@ -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'] ?? []); diff --git a/module/Installer/test/Config/Plugin/ApplicationConfigCustomizerTest.php b/module/Installer/test/Config/Plugin/ApplicationConfigCustomizerTest.php index 9a5bc3c5..68f13532 100644 --- a/module/Installer/test/Config/Plugin/ApplicationConfigCustomizerTest.php +++ b/module/Installer/test/Config/Plugin/ApplicationConfigCustomizerTest.php @@ -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); }