Skip to content

Commit

Permalink
use modern php features
Browse files Browse the repository at this point in the history
  • Loading branch information
LordSimal committed Aug 13, 2023
1 parent da26b03 commit 9a64d20
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 40 deletions.
12 changes: 5 additions & 7 deletions src/Sql/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ public function dump(): string
throw new BinaryNotFoundException($this->command . ' was not found');
}

$config = $this->getConfig();

$command = [
$this->command,
'--user="' . ($config['username'] ?? '') . '"',
'--password="' . ($config['password'] ?? '') . '"',
'--default-character-set=' . ($config['encoding'] ?? 'utf8mb4'),
'--host=' . ($config['host'] ?? 'localhost'),
'--user="' . ($this->config['username'] ?? '') . '"',
'--password="' . ($this->config['password'] ?? '') . '"',
'--default-character-set=' . ($this->config['encoding'] ?? 'utf8mb4'),
'--host=' . ($this->config['host'] ?? 'localhost'),
'--databases',
$config['database'],
$this->config['database'],
'--no-create-db',
];
if ($this->isDataOnly()) {
Expand Down
19 changes: 8 additions & 11 deletions src/Sql/PostgreSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ public function dump(): string
throw new BinaryNotFoundException($this->command . ' was not found');
}

$config = $this->getConfig();
$passFile = $this->writePassFile();

$command = [
'PGPASSFILE=' . $passFile,
$this->command,
'--host=' . ($config['host'] ?? 'localhost'),
'--username=' . ($config['username'] ?? ''),
'--dbname="' . ($config['database'] ?? '') . '"',
'--host=' . ($this->config['host'] ?? 'localhost'),
'--username=' . ($this->config['username'] ?? ''),
'--dbname="' . ($this->config['database'] ?? '') . '"',
];
if ($this->isDataOnly()) {
$command[] = '--data-only';
Expand All @@ -42,7 +41,7 @@ public function dump(): string
$output = $process->getOutput();
$error = $process->getErrorOutput();

if (strpos($error, 'server version mismatch') !== false) {
if (str_contains($error, 'server version mismatch')) {
throw new VersionMismatchException();
}

Expand All @@ -60,15 +59,13 @@ public function dump(): string
*/
private function writePassFile(): string
{
$config = $this->getConfig();

$passwordParts = [
empty($config['host']) ? 'localhost' : $config['host'],
empty($config['port']) ? '5432' : $config['port'],
empty($this->config['host']) ? 'localhost' : $this->config['host'],
empty($this->config['port']) ? '5432' : $this->config['port'],
// Database
'*',
$config['username'],
$config['password'],
$this->config['username'],
$this->config['password'],
];

// Escape colon and backslash characters in entries.
Expand Down
18 changes: 2 additions & 16 deletions src/Sql/SqlBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ abstract class SqlBase
*/
protected string $command;

/**
* @var array The config array from the connection object
*/
protected array $config;

/**
* @var bool Indicated if only data should be exported or not
*/
Expand All @@ -30,17 +25,8 @@ abstract class SqlBase
/**
* @param array $config The config array from the connection object
*/
public function __construct(array $config)
{
$this->config = $config;
}

/**
* @return array
*/
public function getConfig(): array
public function __construct(public readonly array $config)
{
return $this->config;
}

/**
Expand Down Expand Up @@ -77,7 +63,7 @@ public function setIo(ConsoleIo $io): void
*/
protected function checkBinary(string $command): bool
{
$windows = strpos(PHP_OS, 'WIN') === 0;
$windows = str_starts_with(PHP_OS, 'WIN');
$test = $windows ? 'where' : 'command -v';

return is_executable(trim(shell_exec("$test $command")));
Expand Down
8 changes: 2 additions & 6 deletions src/Sql/Sqlite.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,9 @@ public function dump(): string
*/
private function getSchema(): string
{
$config = $this->getConfig();

$schemaCommand = [
$this->command,
$config['database'],
$this->config['database'],
'.schema',
];

Expand All @@ -79,11 +77,9 @@ private function getSchema(): string
*/
private function getDump(): string
{
$config = $this->getConfig();

$schemaCommand = [
$this->command,
$config['database'],
$this->config['database'],
'.dump',
];

Expand Down

0 comments on commit 9a64d20

Please sign in to comment.