-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
69 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,25 @@ | |
/** | ||
* Database Object. | ||
*/ | ||
final class Xeed extends PDO implements ArrayAccess | ||
final class Xeed implements ArrayAccess | ||
{ | ||
/** | ||
* Singleton Instance. | ||
*/ | ||
private static $instance = null; | ||
|
||
/** | ||
* PDO Instance. | ||
*/ | ||
public PDO $pdo; | ||
|
||
/** | ||
* Driver name. | ||
* | ||
* @var string, eg. 'mysql' or 'sqlite' | ||
*/ | ||
public string $driver; | ||
Check failure on line 31 in src/Xeed.php GitHub Actions / publish-pages / publish-pages
|
||
|
||
/** | ||
* Array of available databases. | ||
*/ | ||
|
@@ -30,30 +42,45 @@ final class Xeed extends PDO implements ArrayAccess | |
|
||
private ProviderInterface $provider; | ||
|
||
private function __construct( | ||
public string $driver, | ||
?string $database = null, | ||
?string $host = null, | ||
?string $port = null, | ||
?string $username = null, | ||
?string $password = '', | ||
?array $options = null | ||
) { | ||
$options = $options ?? [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; | ||
/** | ||
* Establish connection | ||
* | ||
* @param array $connection The elements of the connection array.($driver, $database, $host, $port, $username, $password) | ||
* @return static The method returns the current instance that enables method chaining. | ||
*/ | ||
public function addConnection(array $connection): static | ||
{ | ||
// self::$instance = new self($driver, $database, $host, $port, $username, $password); | ||
|
||
$options = $connection['options'] ?? [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]; | ||
|
||
$this->provider = new (__NAMESPACE__.'\\Provider\\'.ucfirst($connection['driver']).'Provider'); | ||
|
||
$this->provider = new (__NAMESPACE__.'\\Provider\\'.ucfirst($driver).'Provider'); | ||
$this->driver = $connection['driver']; | ||
|
||
if ($driver === 'sqlite') { | ||
$dns = $driver.':'.($database ?? Path::database().'database.sqlite'); | ||
if ($connection['driver'] === 'sqlite') { | ||
$dns = $connection['driver'].':'.($database ?? Path::database().'database.sqlite'); | ||
|
||
$this->pdo = new PDO($dns, options: $options); | ||
} | ||
|
||
parent::__construct($dns, options: $options); | ||
if ($connection['driver'] === 'mysql') { | ||
$dns = $connection['driver']. | ||
':host='. | ||
$connection['host']. | ||
((! empty($connection['port'])) ? (';port='.$connection['port']) : '').';dbname='.$connection['database']; | ||
|
||
return; | ||
$this->pdo = new PDO($dns, $connection['username'], $connection['password'], $options); | ||
} | ||
|
||
$dns = $driver.':host='.$host.((! empty($port)) ? (';port='.$port) : '').';dbname='.$database; | ||
return $this; | ||
} | ||
|
||
parent::__construct($dns, $username, $password, $options); | ||
public function addPdo(PDO $pdo): static | ||
{ | ||
$this->pdo = $pdo; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
|
@@ -74,7 +101,14 @@ public static function getInstance(): static | |
$username = $_ENV['DB_USERNAME'] ?? null; | ||
$password = $_ENV['DB_PASSWORD'] ?? null; | ||
|
||
self::$instance = new self($driver, $database, $host, $port, $username, $password); | ||
self::$instance = (new static())->addConnection([ | ||
'driver' => $driver, | ||
'database' => $database, | ||
'host' => $host, | ||
'port' => $port, | ||
'username' => $username, | ||
'password' => $password, | ||
]); | ||
} | ||
|
||
return self::$instance; | ||
|
@@ -107,7 +141,7 @@ public function attach(): static | |
/** | ||
* Get attached tables. | ||
* | ||
* @return array<\Cable8mm\Xeed\Table> The method returns the attached tables | ||
* @return \Cable8mm\Xeed\Table[] The method returns the attached tables | ||
*/ | ||
public function getTables(): array | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters