Modernize the code, make the mysqliConnection easier to use and change mysql command to mariadb
This commit is contained in:
parent
bdf4910694
commit
ca0d357629
4
.env
4
.env
@ -20,5 +20,5 @@ APP_SECRET=
|
|||||||
###< symfony/framework-bundle ###
|
###< symfony/framework-bundle ###
|
||||||
|
|
||||||
DATABASE_HOSTNAME=localhost
|
DATABASE_HOSTNAME=localhost
|
||||||
DATABASE_USER=user
|
DATABASE_USER=admin
|
||||||
DATABASE_PASSWORD=password
|
DATABASE_PASSWORD=admin
|
@ -22,9 +22,3 @@ 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\DatabaseCredentials:
|
|
||||||
arguments:
|
|
||||||
$hostname: '%env(string:DATABASE_HOSTNAME)%'
|
|
||||||
$user: '%env(string:DATABASE_USER)%'
|
|
||||||
$password: '%env(DATABASE_PASSWORD)%'
|
|
@ -4,10 +4,13 @@ namespace App\Service;
|
|||||||
|
|
||||||
use Symfony\Component\Process\Process;
|
use Symfony\Component\Process\Process;
|
||||||
|
|
||||||
class BackupService
|
readonly class BackupService
|
||||||
{
|
{
|
||||||
|
private const COMMAND_DUMP = 'mariadb-dump';
|
||||||
|
private const COMMAND_DB = 'mariadb';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly DatabaseCredentials $credentials
|
private DatabaseCredentials $credentials
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function backup(string $db, ?string $fileName = null): string
|
public function backup(string $db, ?string $fileName = null): string
|
||||||
@ -18,11 +21,11 @@ class BackupService
|
|||||||
}
|
}
|
||||||
|
|
||||||
$process = new Process([
|
$process = new Process([
|
||||||
'mysqldump',
|
self::COMMAND_DUMP,
|
||||||
'-u', $this->credentials->getUser(),
|
'-u', $this->credentials->user,
|
||||||
$db,
|
$db,
|
||||||
]);
|
]);
|
||||||
$process->setEnv(['MYSQL_PWD' => $this->credentials->getPassword()]);
|
$process->setEnv(['MYSQL_PWD' => $this->credentials->password]);
|
||||||
$process->start();
|
$process->start();
|
||||||
|
|
||||||
$stream = fopen($fileName, 'w+');
|
$stream = fopen($fileName, 'w+');
|
||||||
@ -40,14 +43,14 @@ class BackupService
|
|||||||
public function restore(string $db, string $fileName): void
|
public function restore(string $db, string $fileName): void
|
||||||
{
|
{
|
||||||
$process = new Process([
|
$process = new Process([
|
||||||
'mysql',
|
self::COMMAND_DB,
|
||||||
'-u', $this->credentials->getUser(),
|
'-u', $this->credentials->user,
|
||||||
$db,
|
$db,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$stream = fopen($fileName, 'r');
|
$stream = fopen($fileName, 'r');
|
||||||
|
|
||||||
$process->setEnv(['MYSQL_PWD' => $this->credentials->getPassword()]);
|
$process->setEnv(['MYSQL_PWD' => $this->credentials->password]);
|
||||||
$process->setInput($stream);
|
$process->setInput($stream);
|
||||||
$process->run();
|
$process->run();
|
||||||
}
|
}
|
||||||
|
@ -2,26 +2,13 @@
|
|||||||
|
|
||||||
namespace App\Service;
|
namespace App\Service;
|
||||||
|
|
||||||
class DatabaseCredentials
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
|
|
||||||
|
readonly class DatabaseCredentials
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly string $hostname,
|
#[Autowire('%env(string:DATABASE_HOSTNAME)%')] public string $hostname,
|
||||||
private readonly string $user,
|
#[Autowire('%env(string:DATABASE_USER)%')] public string $user,
|
||||||
private readonly string $password,
|
#[Autowire('%env(string:DATABASE_PASSWORD)%')] public string $password,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function getHostname(): string
|
|
||||||
{
|
|
||||||
return $this->hostname;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getUser(): string
|
|
||||||
{
|
|
||||||
return $this->user;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getPassword(): string
|
|
||||||
{
|
|
||||||
return $this->password;
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -5,15 +5,12 @@ namespace App\Service;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use mysqli;
|
use mysqli;
|
||||||
|
|
||||||
class DatabaseService
|
readonly class DatabaseService
|
||||||
{
|
{
|
||||||
private mysqli $conn;
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private readonly MysqliConnection $service
|
private MysqliConnection $conn
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
$this->conn = $this->service->getConnection();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function listDatabases(): array
|
public function listDatabases(): array
|
||||||
|
@ -5,20 +5,13 @@ namespace App\Service;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use mysqli;
|
use mysqli;
|
||||||
|
|
||||||
class MysqliConnection
|
class MysqliConnection extends mysqli
|
||||||
{
|
{
|
||||||
private mysqli $connection;
|
|
||||||
|
|
||||||
public function __construct(DatabaseCredentials $credentials)
|
public function __construct(DatabaseCredentials $credentials)
|
||||||
{
|
{
|
||||||
$this->connection = new mysqli($credentials->getHostname(), $credentials->getUser(), $credentials->getPassword());
|
parent::__construct($credentials->hostname, $credentials->user, $credentials->password);
|
||||||
if ($this->connection->connect_error) {
|
if ($this->connect_error) {
|
||||||
throw new Exception("Connection failed: " . $this->connection->connect_error);
|
throw new Exception("Connection failed: " . $this->connect_error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getConnection(): mysqli
|
|
||||||
{
|
|
||||||
return $this->connection;
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user