silviooosilva /
CacheerPHP
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Silviooosilva\CacheerPhp\Core; |
||
| 4 | |||
| 5 | use PDO; |
||
| 6 | use PDOException; |
||
| 7 | use Silviooosilva\CacheerPhp\Enums\DatabaseDriver; |
||
| 8 | use Silviooosilva\CacheerPhp\Exceptions\ConnectionException; |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Class Connect |
||
| 12 | * @author Sílvio Silva <https://github.com/silviooosilva> |
||
| 13 | * @package Silviooosilva\CacheerPhp |
||
| 14 | */ |
||
| 15 | class Connect |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * Active database driver for new connections. |
||
| 19 | */ |
||
| 20 | public static DatabaseDriver $connection = DatabaseDriver::SQLITE; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Holds the last error encountered during connection attempts. |
||
| 24 | * |
||
| 25 | * @var PDOException|null |
||
| 26 | */ |
||
| 27 | private static ?PDOException $error = null; |
||
| 28 | |||
| 29 | |||
| 30 | /** |
||
| 31 | * Creates a new PDO instance based on the specified database configuration. |
||
| 32 | * |
||
| 33 | * @param array|null $database |
||
| 34 | * @return PDO|null |
||
| 35 | * @throws ConnectionException |
||
| 36 | */ |
||
| 37 | public static function getInstance(?array $database = null): ?PDO |
||
| 38 | { |
||
| 39 | $pdo = ConnectionFactory::createConnection($database); |
||
| 40 | if ($pdo) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 41 | MigrationManager::migrate($pdo); |
||
| 42 | } |
||
| 43 | return $pdo; |
||
| 44 | } |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Sets the connection type for the database. |
||
| 48 | * |
||
| 49 | * @param DatabaseDriver|string $connection |
||
| 50 | * @return void |
||
| 51 | * @throws ConnectionException |
||
| 52 | */ |
||
| 53 | public static function setConnection(DatabaseDriver|string $connection): void |
||
| 54 | { |
||
| 55 | $driver = $connection instanceof DatabaseDriver |
||
| 56 | ? $connection |
||
| 57 | : DatabaseDriver::tryFrom(strtolower($connection)); |
||
| 58 | |||
| 59 | if ($driver === null) { |
||
| 60 | $labels = DatabaseDriver::labels(); |
||
| 61 | throw ConnectionException::create('Only [' . implode(', ', $labels) . '] are available at the moment...'); |
||
| 62 | } |
||
| 63 | |||
| 64 | self::$connection = $driver; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Gets the current connection type. |
||
| 69 | * |
||
| 70 | * @return DatabaseDriver |
||
| 71 | */ |
||
| 72 | public static function getConnection(): DatabaseDriver |
||
| 73 | { |
||
| 74 | return self::$connection; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Returns the last error encountered during connection attempts.\ |
||
| 79 | * |
||
| 80 | * @return PDOException|null |
||
| 81 | */ |
||
| 82 | public static function getError(): ?PDOException |
||
| 83 | { |
||
| 84 | return self::$error; |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Prevents instantiation of the Connect class. |
||
| 89 | * This class is designed to be used statically, so it cannot be instantiated. |
||
| 90 | * |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | private function __construct() {} |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Prevents cloning of the Connect instance. |
||
| 97 | * |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | private function __clone() {} |
||
| 101 | } |
||
| 102 |