Refactored Language and UrlShortener config customizers

This commit is contained in:
Alejandro Celaya 2018-10-03 18:55:20 +02:00
parent d5736756f7
commit 1b5081ae21
3 changed files with 78 additions and 22 deletions

View File

@ -5,23 +5,48 @@ namespace Shlinkio\Shlink\Installer\Config\Plugin;
use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig; use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
use function array_diff;
use function array_keys;
class LanguageConfigCustomizer implements ConfigCustomizerInterface class LanguageConfigCustomizer implements ConfigCustomizerInterface
{ {
private const SUPPORTED_LANGUAGES = ['en', 'es']; private const SUPPORTED_LANGUAGES = ['en', 'es'];
private const DEFAULT_LANG = 'DEFAULT';
private const CLI_LANG = 'CLI';
private const EXPECTED_KEYS = [
self::DEFAULT_LANG,
self::CLI_LANG,
];
public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
{ {
$io->title('LANGUAGE'); $io->title('LANGUAGE');
if ($appConfig->hasLanguage() && $io->confirm('Do you want to keep imported language?')) { $lang = $appConfig->getLanguage();
$keysToAskFor = $appConfig->hasLanguage() && $io->confirm('Do you want to keep imported language?')
? array_diff(self::EXPECTED_KEYS, array_keys($lang))
: self::EXPECTED_KEYS;
if (empty($keysToAskFor)) {
return; return;
} }
$appConfig->setLanguage([ foreach ($keysToAskFor as $key) {
'DEFAULT' => $this->chooseLanguage($io, 'Select default language for the application in general'), $lang[$key] = $this->ask($io, $key);
'CLI' => $this->chooseLanguage($io, 'Select default language for CLI executions'), }
]); $appConfig->setLanguage($lang);
}
private function ask(SymfonyStyle $io, string $key)
{
switch ($key) {
case self::DEFAULT_LANG:
return $this->chooseLanguage($io, 'Select default language for the application in general');
case self::CLI_LANG:
return $this->chooseLanguage($io, 'Select default language for CLI executions');
}
return '';
} }
private function chooseLanguage(SymfonyStyle $io, string $message): string private function chooseLanguage(SymfonyStyle $io, string $message): string

View File

@ -7,31 +7,62 @@ use Shlinkio\Shlink\Core\Service\UrlShortener;
use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig; use Shlinkio\Shlink\Installer\Model\CustomizableAppConfig;
use Shlinkio\Shlink\Installer\Util\AskUtilsTrait; use Shlinkio\Shlink\Installer\Util\AskUtilsTrait;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
use function array_diff;
use function array_keys;
use function str_shuffle; use function str_shuffle;
class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface class UrlShortenerConfigCustomizer implements ConfigCustomizerInterface
{ {
use AskUtilsTrait; use AskUtilsTrait;
private const SCHEMA = 'SCHEMA';
private const HOSTNAME = 'HOSTNAME';
private const CHARS = 'CHARS';
private const VALIDATE_URL = 'VALIDATE_URL';
private const EXPECTED_KEYS = [
self::SCHEMA,
self::HOSTNAME,
self::CHARS,
self::VALIDATE_URL,
];
public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void public function process(SymfonyStyle $io, CustomizableAppConfig $appConfig): void
{ {
$io->title('URL SHORTENER'); $io->title('URL SHORTENER');
if ($appConfig->hasUrlShortener() && $io->confirm('Do you want to keep imported URL shortener config?')) { $urlShortener = $appConfig->getUrlShortener();
$diffKeys = $appConfig->hasUrlShortener() && $io->confirm('Do you want to keep imported URL shortener config?');
$keysToAskFor = $diffKeys ? array_diff(self::EXPECTED_KEYS, array_keys($urlShortener)) : self::EXPECTED_KEYS;
if (empty($keysToAskFor)) {
return; return;
} }
// Ask for URL shortener params foreach ($keysToAskFor as $key) {
$appConfig->setUrlShortener([ $urlShortener[$key] = $this->ask($io, $key);
'SCHEMA' => $io->choice( }
'Select schema for generated short URLs', $appConfig->setUrlShortener($urlShortener);
['http', 'https'], }
'http'
), private function ask(SymfonyStyle $io, string $key)
'HOSTNAME' => $this->askRequired($io, 'hostname', 'Hostname for generated URLs'), {
'CHARS' => $io->ask('Character set for generated short codes (leave empty to autogenerate one)') switch ($key) {
?: str_shuffle(UrlShortener::DEFAULT_CHARS), case self::SCHEMA:
'VALIDATE_URL' => $io->confirm('Do you want to validate long urls by 200 HTTP status code on response'), return $io->choice(
]); 'Select schema for generated short URLs',
['http', 'https'],
'http'
);
case self::HOSTNAME:
return $this->askRequired($io, 'hostname', 'Hostname for generated URLs');
case self::CHARS:
return $io->ask(
'Character set for generated short codes (leave empty to autogenerate one)'
) ?: str_shuffle(UrlShortener::DEFAULT_CHARS);
case self::VALIDATE_URL:
return $io->confirm('Do you want to validate long urls by 200 HTTP status code on response');
}
return '';
} }
} }

View File

@ -117,7 +117,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface
'DISABLE_TRACK_PARAM' => $array['app_options']['disable_track_param'] ?? null, 'DISABLE_TRACK_PARAM' => $array['app_options']['disable_track_param'] ?? null,
]); ]);
$this->deserializeDatabase($array['entity_manager']['connection'] ?? []); $this->setDatabase($this->deserializeDatabase($array['entity_manager']['connection'] ?? []));
$this->setLanguage([ $this->setLanguage([
'DEFAULT' => $array['translator']['locale'] ?? null, 'DEFAULT' => $array['translator']['locale'] ?? null,
@ -132,10 +132,10 @@ final class CustomizableAppConfig implements ArraySerializableInterface
]); ]);
} }
private function deserializeDatabase(array $conn): void private function deserializeDatabase(array $conn): array
{ {
if (! isset($conn['driver'])) { if (! isset($conn['driver'])) {
return; return [];
} }
$driver = $conn['driver']; $driver = $conn['driver'];
@ -148,7 +148,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface
$params['PORT'] = $conn['port'] ?? null; $params['PORT'] = $conn['port'] ?? null;
} }
$this->setDatabase($params); return $params;
} }
public function getArrayCopy(): array public function getArrayCopy(): array