Skip to content

Commit

Permalink
Allow to not define a configuration and add setConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
williamdes committed Feb 17, 2022
1 parent f8202dc commit 406a122
Showing 1 changed file with 46 additions and 7 deletions.
53 changes: 46 additions & 7 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SimplePhpModelSystem;

use Exception;
use LogicException;
use PDO;
use PDOException;
use PDOStatement;
Expand All @@ -26,14 +27,24 @@ class Database
{

/**
* @var array
* @var array<string,string|int>
*
* @phpstan-var array{
* adapter: string,
* name: string,
* host: string,
* user: string,
* pass: string,
* port: int,
* charset: int
* }
*/
private $dbConfig;
private $dbConfig = null;

/**
* @var PDO|null
*/
private $connection;
private $connection = null;

/** @var self[] $instances */
protected static $instances = [];
Expand All @@ -45,16 +56,32 @@ class Database
/**
* Build a Database object
*
* @param array $config
* @param array|null $config The config block or null if you will use the setConnection method
* @param int $connection Database::MAIN_CONNECTION, Database::SECOND_CONNECTION, Database::THIRD_CONNECTION
* @phpstan-param array{
* currentDatabaseEnv: string,
* database: array<string, array{
* adapter: string,
* name: string,
* host: string,
* user: string,
* pass: string,
* port: int,
* charset: int
* }>
* }|null $config
* @phpstan-param Database::*_CONNECTION $connection
*/
public function __construct(array $config, int $connection = self::MAIN_CONNECTION)
public function __construct(?array $config, int $connection = self::MAIN_CONNECTION)
{
if (! isset($config['database']) || ! isset($config['currentDatabaseEnv'])) {
/** @var array{currentDatabaseEnv?: string, database?: array<string, array{adapter: string, name: string, host: string, user: string, pass: string, port: int, charset: int}>}|null $config */
if ((! isset($config['database']) || ! isset($config['currentDatabaseEnv'])) && $config !== null) {
throw new Exception('Invalid config to create the Database');
}

$this->dbConfig = $config['database'][$config['currentDatabaseEnv']];
if ($config !== null) {
$this->dbConfig = $config['database'][$config['currentDatabaseEnv']];
}
self::$instances[$connection] = $this;
}

Expand Down Expand Up @@ -83,6 +110,10 @@ public function __destruct()
*/
public function connect(): void
{
if ($this->dbConfig === null) {
throw new LogicException('You need to pass the config when creating the Database instance to use the connect method.');
}

$dsn = sprintf(
'%s:dbname=%s;host=%s;port=%d;charset=%s',
$this->dbConfig['adapter'],
Expand All @@ -105,6 +136,14 @@ public function connect(): void
}
}

/**
* @since 1.2.0
*/
public function setConnection(PDO $connection): void
{
$this->connection = $connection;
}

public function getConnection(): PDO
{
if ($this->connection === null) {
Expand Down

0 comments on commit 406a122

Please sign in to comment.