44 lines
1.0 KiB
PHP
44 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace Database\Base;
|
|
|
|
class PDO extends \PDO
|
|
{
|
|
|
|
readonly public string $driver;
|
|
|
|
/**
|
|
* @param string $database
|
|
* @param string $host
|
|
* @param string $username
|
|
* @param string $password
|
|
* @param array $options
|
|
* @param string $driver
|
|
*/
|
|
public function __construct(
|
|
readonly public string $database,
|
|
readonly public string $host,
|
|
readonly public string $username,
|
|
readonly public string $password,
|
|
readonly public array $options,
|
|
string $driver = 'mysql',
|
|
)
|
|
{
|
|
$this->driver = strtolower($driver);
|
|
$dsn = $this->buildDsn();
|
|
parent::__construct($dsn, $this->username, $this->password, $this->options);
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
private function buildDsn(): string
|
|
{
|
|
return match ($this->driver) {
|
|
'pgsql', 'postgresql' => 'pgsql:host=' . $this->host . ';dbname=' . $this->database,
|
|
default => 'mysql:dbname=' . $this->database . ';host=' . $this->host,
|
|
};
|
|
}
|
|
|
|
}
|