forked from KumbiaPHP/ActiveRecord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Db.php
111 lines (102 loc) · 3.11 KB
/
Db.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* KumbiaPHP web & app Framework.
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://wiki.kumbiaphp.com/Licencia
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@kumbiaphp.com so we can send you a copy immediately.
*
* @category Kumbia
*
* @copyright 2005 - 2020 Kumbia Team (http://www.kumbiaphp.com)
* @license http://wiki.kumbiaphp.com/Licencia New BSD License
*/
namespace Kumbia\ActiveRecord;
use \PDO;
/**
* Manejador de conexiones de base de datos.
*/
abstract class Db
{
/**
* Pool de conexiones.
*
* @var PDO[]
*/
private static $pool = [];
/**
* Config de conexiones.
*
* @var array
*/
private static $config = [];
/**
* Obtiene manejador de conexión a la base de datos.
*
* @param string $database base de datos a conectar
*
* @return PDO
*/
public static function get(string $database = 'default'): PDO
{
return self::$pool[$database] ?? self::$pool[$database] = self::connect(self::getConfig($database));
}
/**
* Conexión a la base de datos.
*
* @param array $config Config base de datos a conectar
*
* @throws \RuntimeException
* @return PDO
*/
private static function connect(array $config): PDO
{
try {
return new PDO($config['dsn'], $config['username'], $config['password'], $config['params']);
} catch (\PDOException $e) {
//TODO: comprobar
$message = $e->getMessage();
throw new \RuntimeException("No se pudo realizar la conexión con '{$config['dsn']}'. {$message}");
}
}
/**
* Obtiene manejador de conexión a la base de datos.
*
* @param string $database base de datos a conectar
*
* @throws \RuntimeException
* @return array
*/
private static function getConfig(string $database): array
{
if (empty(self::$config)) {
// Leer la configuración de conexión
self::$config = require \APP_PATH.'config/databases.php';
}
if ( ! isset(self::$config[$database])) {
throw new \RuntimeException("No existen datos de conexión para la bd '$database' en ".\APP_PATH.'config/databases.php');
}
// Envia y carga los valores por defecto para la conexión, si no existen
return self::$config[$database] + [
'dns' => \null,
'username' => \null,
'password' => \null,
'params' => [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]
];
}
/**
* Permite agregar una base de datos sin leer del archivo de configuracion.
*
* @param array $value Valores de la configuración
*/
public static function setConfig(array $value)
{
self::$config = [] + self::$config + $value; //TODO retornar PDO
}
}