Add DatabaseCredentials class and implement database picker question

This commit is contained in:
Tim 2021-12-16 00:06:38 +01:00
parent d78f52b965
commit 0ff9021d6e
5 changed files with 96 additions and 3 deletions

View File

@ -24,7 +24,7 @@ services:
# add more service definitions when explicit configuration is needed # add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones # please note that last definitions always *replace* previous ones
App\Service\MysqliConnection: App\Service\DatabaseCredentials:
arguments: arguments:
$hostname: '%env(string:DATABASE_HOSTNAME)%' $hostname: '%env(string:DATABASE_HOSTNAME)%'
$user: '%env(string:DATABASE_USER)%' $user: '%env(string:DATABASE_USER)%'

View File

@ -0,0 +1,42 @@
<?php
namespace App\Console;
use App\Service\DatabaseManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
class BackupDatabase extends Command
{
protected static $defaultName = 'app:db:create';
public function __construct(
private DatabaseManager $db,
)
{
parent::__construct();
}
protected function configure(): void
{
$this->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;
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Service;
class DatabaseCredentials
{
public function __construct(
private string $hostname,
private string $user,
private string $password,
)
{
}
public function getHostname(): string
{
return $this->hostname;
}
public function getUser(): string
{
return $this->user;
}
public function getPassword(): string
{
return $this->password;
}
}

View File

@ -8,9 +8,9 @@ class MysqliConnection
{ {
private mysqli $connection; 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) { if ($this->connection->connect_error) {
throw new \Exception("Connection failed: " . $this->connection->connect_error); throw new \Exception("Connection failed: " . $this->connection->connect_error);
} }

View File

@ -0,0 +1,22 @@
<?php
namespace App\Service\Traits;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
trait SelectDatabaseQuestion
{
private function getDatabaseName(InputInterface $input, OutputInterface $output): string
{
$question = $this->getHelper('question');
if (!$name = $input->getArgument('name')) {
$selectQuestion = new ChoiceQuestion('Database name: ', $this->db->listDatabases());
$name = $question->ask($input, $output, $selectQuestion);
}
return $name;
}
}