From b17f96043a6c2b3141597863e96c7f252edf51ed Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 31 Dec 2017 16:53:18 +0100 Subject: [PATCH] Simplified and standardized DatabaseConfigCustomizerPlugin thanks to SymfonyStyle --- .../Plugin/AbstractConfigCustomizerPlugin.php | 2 +- .../Plugin/DatabaseConfigCustomizerPlugin.php | 24 ++++++++----------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/module/CLI/src/Install/Plugin/AbstractConfigCustomizerPlugin.php b/module/CLI/src/Install/Plugin/AbstractConfigCustomizerPlugin.php index ed25eee1..d8ea5b85 100644 --- a/module/CLI/src/Install/Plugin/AbstractConfigCustomizerPlugin.php +++ b/module/CLI/src/Install/Plugin/AbstractConfigCustomizerPlugin.php @@ -20,7 +20,7 @@ abstract class AbstractConfigCustomizerPlugin implements ConfigCustomizerPluginI $text .= ' (defaults to ' . $default . ')'; } do { - $value = $io->ask('' . $text . ': ', $default); + $value = $io->ask($text, $default); if (empty($value) && ! $allowEmpty) { $io->writeln('Value can\'t be empty'); } diff --git a/module/CLI/src/Install/Plugin/DatabaseConfigCustomizerPlugin.php b/module/CLI/src/Install/Plugin/DatabaseConfigCustomizerPlugin.php index c4c9f0fd..bd1edda8 100644 --- a/module/CLI/src/Install/Plugin/DatabaseConfigCustomizerPlugin.php +++ b/module/CLI/src/Install/Plugin/DatabaseConfigCustomizerPlugin.php @@ -43,7 +43,7 @@ class DatabaseConfigCustomizerPlugin extends AbstractConfigCustomizerPlugin $io->title('DATABASE'); if ($appConfig->hasDatabase() && $io->confirm( - 'Do you want to keep imported database config? (Y/n): ' + 'Do you want to keep imported database config? (Y/n) ' )) { // If the user selected to keep DB config and is configured to use sqlite, copy DB file if ($appConfig->getDatabase()['DRIVER'] === self::DATABASE_DRIVERS['SQLite']) { @@ -53,7 +53,7 @@ class DatabaseConfigCustomizerPlugin extends AbstractConfigCustomizerPlugin CustomizableAppConfig::SQLITE_DB_PATH ); } catch (IOException $e) { - $output->writeln('It wasn\'t possible to import the SQLite database'); + $io->error('It wasn\'t possible to import the SQLite database'); throw $e; } } @@ -63,27 +63,23 @@ class DatabaseConfigCustomizerPlugin extends AbstractConfigCustomizerPlugin // Select database type $params = []; - $databases = array_keys(self::DATABASE_DRIVERS); - $dbType = $io->choice( - 'Select database type (defaults to ' . $databases[0] . '):', - $databases, - 0 - ); + $databases = \array_keys(self::DATABASE_DRIVERS); + $dbType = $io->choice('Select database type', $databases, $databases[0]); $params['DRIVER'] = self::DATABASE_DRIVERS[$dbType]; // Ask for connection params if database is not SQLite if ($params['DRIVER'] !== self::DATABASE_DRIVERS['SQLite']) { - $params['NAME'] = $this->ask($io, 'Database name', 'shlink'); - $params['USER'] = $this->ask($io, 'Database username'); - $params['PASSWORD'] = $this->ask($io, 'Database password'); - $params['HOST'] = $this->ask($io, 'Database host', 'localhost'); - $params['PORT'] = $this->ask($io, 'Database port', $this->getDefaultDbPort($params['DRIVER'])); + $params['NAME'] = $io->ask('Database name', 'shlink'); + $params['USER'] = $io->ask('Database username'); + $params['PASSWORD'] = $io->ask('Database password'); + $params['HOST'] = $io->ask('Database host', 'localhost'); + $params['PORT'] = $io->ask('Database port', $this->getDefaultDbPort($params['DRIVER'])); } $appConfig->setDatabase($params); } - private function getDefaultDbPort($driver) + private function getDefaultDbPort(string $driver): string { return $driver === 'pdo_mysql' ? '3306' : '5432'; }