From 0ff9021d6e871b278e8faaadcc57862e15f0aac4 Mon Sep 17 00:00:00 2001 From: tim Date: Thu, 16 Dec 2021 00:06:38 +0100 Subject: [PATCH] Add DatabaseCredentials class and implement database picker question --- config/services.yaml | 2 +- src/Console/BackupDatabase.php | 42 +++++++++++++++++++ src/Service/DatabaseCredentials.php | 29 +++++++++++++ src/Service/MysqliConnection.php | 4 +- src/Service/Traits/SelectDatabaseQuestion.php | 22 ++++++++++ 5 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 src/Console/BackupDatabase.php create mode 100644 src/Service/DatabaseCredentials.php create mode 100644 src/Service/Traits/SelectDatabaseQuestion.php diff --git a/config/services.yaml b/config/services.yaml index 77c960f..08b41b6 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -24,7 +24,7 @@ services: # add more service definitions when explicit configuration is needed # please note that last definitions always *replace* previous ones - App\Service\MysqliConnection: + App\Service\DatabaseCredentials: arguments: $hostname: '%env(string:DATABASE_HOSTNAME)%' $user: '%env(string:DATABASE_USER)%' diff --git a/src/Console/BackupDatabase.php b/src/Console/BackupDatabase.php new file mode 100644 index 0000000..ef52b24 --- /dev/null +++ b/src/Console/BackupDatabase.php @@ -0,0 +1,42 @@ +addArgument('name', InputArgument::OPTIONAL, 'Database name'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $question = $this->getHelper('question'); + + if (!$name = $input->getArgument('name')) { + $name = $question->ask($input, $output, new Question('Database name: ')); + } + + $this->db->createDatabase($name); + + $output->writeln(sprintf('Database "%s" successfully created', $name)); + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Service/DatabaseCredentials.php b/src/Service/DatabaseCredentials.php new file mode 100644 index 0000000..5db8834 --- /dev/null +++ b/src/Service/DatabaseCredentials.php @@ -0,0 +1,29 @@ +hostname; + } + + public function getUser(): string + { + return $this->user; + } + + public function getPassword(): string + { + return $this->password; + } +} \ No newline at end of file diff --git a/src/Service/MysqliConnection.php b/src/Service/MysqliConnection.php index fc0189b..25952d0 100644 --- a/src/Service/MysqliConnection.php +++ b/src/Service/MysqliConnection.php @@ -8,9 +8,9 @@ class MysqliConnection { private mysqli $connection; - public function __construct($hostname, $user, $password) + public function __construct(DatabaseCredentials $credentials) { - $this->connection = new mysqli($hostname, $user, $password); + $this->connection = new mysqli($credentials->getHostname(), $credentials->getUser(), $credentials->getPassword()); if ($this->connection->connect_error) { throw new \Exception("Connection failed: " . $this->connection->connect_error); } diff --git a/src/Service/Traits/SelectDatabaseQuestion.php b/src/Service/Traits/SelectDatabaseQuestion.php new file mode 100644 index 0000000..92ab9f3 --- /dev/null +++ b/src/Service/Traits/SelectDatabaseQuestion.php @@ -0,0 +1,22 @@ +getHelper('question'); + + if (!$name = $input->getArgument('name')) { + $selectQuestion = new ChoiceQuestion('Database name: ', $this->db->listDatabases()); + $name = $question->ask($input, $output, $selectQuestion); + } + + return $name; + } +} \ No newline at end of file