mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Simplified and standardized DatabaseConfigCustomizerPlugin thanks to SymfonyStyle
This commit is contained in:
parent
6f9b727673
commit
b17f96043a
@ -20,7 +20,7 @@ abstract class AbstractConfigCustomizerPlugin implements ConfigCustomizerPluginI
|
|||||||
$text .= ' (defaults to ' . $default . ')';
|
$text .= ' (defaults to ' . $default . ')';
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
$value = $io->ask('<question>' . $text . ':</question> ', $default);
|
$value = $io->ask($text, $default);
|
||||||
if (empty($value) && ! $allowEmpty) {
|
if (empty($value) && ! $allowEmpty) {
|
||||||
$io->writeln('<error>Value can\'t be empty</error>');
|
$io->writeln('<error>Value can\'t be empty</error>');
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ class DatabaseConfigCustomizerPlugin extends AbstractConfigCustomizerPlugin
|
|||||||
$io->title('DATABASE');
|
$io->title('DATABASE');
|
||||||
|
|
||||||
if ($appConfig->hasDatabase() && $io->confirm(
|
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 the user selected to keep DB config and is configured to use sqlite, copy DB file
|
||||||
if ($appConfig->getDatabase()['DRIVER'] === self::DATABASE_DRIVERS['SQLite']) {
|
if ($appConfig->getDatabase()['DRIVER'] === self::DATABASE_DRIVERS['SQLite']) {
|
||||||
@ -53,7 +53,7 @@ class DatabaseConfigCustomizerPlugin extends AbstractConfigCustomizerPlugin
|
|||||||
CustomizableAppConfig::SQLITE_DB_PATH
|
CustomizableAppConfig::SQLITE_DB_PATH
|
||||||
);
|
);
|
||||||
} catch (IOException $e) {
|
} 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;
|
throw $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -63,27 +63,23 @@ class DatabaseConfigCustomizerPlugin extends AbstractConfigCustomizerPlugin
|
|||||||
|
|
||||||
// Select database type
|
// Select database type
|
||||||
$params = [];
|
$params = [];
|
||||||
$databases = array_keys(self::DATABASE_DRIVERS);
|
$databases = \array_keys(self::DATABASE_DRIVERS);
|
||||||
$dbType = $io->choice(
|
$dbType = $io->choice('Select database type', $databases, $databases[0]);
|
||||||
'<question>Select database type (defaults to ' . $databases[0] . '):</question>',
|
|
||||||
$databases,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
$params['DRIVER'] = self::DATABASE_DRIVERS[$dbType];
|
$params['DRIVER'] = self::DATABASE_DRIVERS[$dbType];
|
||||||
|
|
||||||
// Ask for connection params if database is not SQLite
|
// Ask for connection params if database is not SQLite
|
||||||
if ($params['DRIVER'] !== self::DATABASE_DRIVERS['SQLite']) {
|
if ($params['DRIVER'] !== self::DATABASE_DRIVERS['SQLite']) {
|
||||||
$params['NAME'] = $this->ask($io, 'Database name', 'shlink');
|
$params['NAME'] = $io->ask('Database name', 'shlink');
|
||||||
$params['USER'] = $this->ask($io, 'Database username');
|
$params['USER'] = $io->ask('Database username');
|
||||||
$params['PASSWORD'] = $this->ask($io, 'Database password');
|
$params['PASSWORD'] = $io->ask('Database password');
|
||||||
$params['HOST'] = $this->ask($io, 'Database host', 'localhost');
|
$params['HOST'] = $io->ask('Database host', 'localhost');
|
||||||
$params['PORT'] = $this->ask($io, 'Database port', $this->getDefaultDbPort($params['DRIVER']));
|
$params['PORT'] = $io->ask('Database port', $this->getDefaultDbPort($params['DRIVER']));
|
||||||
}
|
}
|
||||||
|
|
||||||
$appConfig->setDatabase($params);
|
$appConfig->setDatabase($params);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getDefaultDbPort($driver)
|
private function getDefaultDbPort(string $driver): string
|
||||||
{
|
{
|
||||||
return $driver === 'pdo_mysql' ? '3306' : '5432';
|
return $driver === 'pdo_mysql' ? '3306' : '5432';
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user