Simplified and standardized DatabaseConfigCustomizerPlugin thanks to SymfonyStyle

This commit is contained in:
Alejandro Celaya 2017-12-31 16:53:18 +01:00
parent 6f9b727673
commit b17f96043a
2 changed files with 11 additions and 15 deletions

View file

@ -20,7 +20,7 @@ abstract class AbstractConfigCustomizerPlugin implements ConfigCustomizerPluginI
$text .= ' (defaults to ' . $default . ')';
}
do {
$value = $io->ask('<question>' . $text . ':</question> ', $default);
$value = $io->ask($text, $default);
if (empty($value) && ! $allowEmpty) {
$io->writeln('<error>Value can\'t be empty</error>');
}

View file

@ -43,7 +43,7 @@ class DatabaseConfigCustomizerPlugin extends AbstractConfigCustomizerPlugin
$io->title('DATABASE');
if ($appConfig->hasDatabase() && $io->confirm(
'<question>Do you want to keep imported database config? (Y/n):</question> '
'<question>Do you want to keep imported database config? (Y/n)</question> '
)) {
// 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('<error>It wasn\'t possible to import the SQLite database</error>');
$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(
'<question>Select database type (defaults to ' . $databases[0] . '):</question>',
$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';
}