Add DatabaseCredentials class and implement database picker question
This commit is contained in:
parent
d78f52b965
commit
0ff9021d6e
@ -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)%'
|
||||||
|
42
src/Console/BackupDatabase.php
Normal file
42
src/Console/BackupDatabase.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
29
src/Service/DatabaseCredentials.php
Normal file
29
src/Service/DatabaseCredentials.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
}
|
}
|
||||||
|
22
src/Service/Traits/SelectDatabaseQuestion.php
Normal file
22
src/Service/Traits/SelectDatabaseQuestion.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user