1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.9.6 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\Exceptions\DatabaseException; |
18
|
|
|
use Quantum\Libraries\Database\Adapters\Idiorm\IdiormDbal; |
19
|
|
|
use Quantum\Libraries\Database\Adapters\Sleekdb\SleekDbal; |
20
|
|
|
use Quantum\Libraries\Config\Exceptions\ConfigException; |
21
|
|
|
use Quantum\Libraries\Database\Contracts\DbalInterface; |
22
|
|
|
use Quantum\Libraries\Database\Traits\RelationalTrait; |
23
|
|
|
use Quantum\Di\Exceptions\DiException; |
24
|
|
|
use Quantum\Exceptions\BaseException; |
25
|
|
|
use Quantum\Loader\Setup; |
26
|
|
|
use ReflectionException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class Database |
30
|
|
|
* @package Quantum\Libraries\Database |
31
|
|
|
*/ |
32
|
|
|
class Database |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
use RelationalTrait; |
36
|
|
|
|
37
|
|
|
const ADAPTERS = [ |
38
|
|
|
'sleekdb' => SleekDbal::class, |
39
|
|
|
'mysql' => IdiormDbal::class, |
40
|
|
|
'sqlite' => IdiormDbal::class, |
41
|
|
|
'pgsql' => IdiormDbal::class, |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Database configurations |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private $configs = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Database instance |
52
|
|
|
* @var Database|null |
53
|
|
|
*/ |
54
|
|
|
private static $instance = null; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $ormClass; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @throws BaseException |
63
|
|
|
* @throws ConfigException |
64
|
|
|
* @throws DiException |
65
|
|
|
* @throws ReflectionException |
66
|
|
|
*/ |
67
|
|
|
private function __construct() |
68
|
|
|
{ |
69
|
|
|
if (!config()->has('database')) { |
70
|
|
|
config()->import(new Setup('config', 'database')); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$adapterName = config()->get('database.current'); |
74
|
|
|
|
75
|
|
|
if (!array_key_exists($adapterName, self::ADAPTERS)) { |
76
|
|
|
throw DatabaseException::adapterNotSupported($adapterName); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->ormClass = self::ADAPTERS[$adapterName]; |
80
|
|
|
|
81
|
|
|
if (!class_exists($this->ormClass)) { |
82
|
|
|
throw DatabaseException::ormClassNotFound($this->ormClass); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (!$this->ormClass::getConnection()) { |
86
|
|
|
$this->ormClass::connect(config()->get('database.' . $adapterName)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get Instance |
92
|
|
|
* @return Database |
93
|
|
|
*/ |
94
|
|
|
public static function getInstance(): Database |
95
|
|
|
{ |
96
|
|
|
if (self::$instance === null) { |
97
|
|
|
self::$instance = new self(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return self::$instance; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets the ORM |
105
|
|
|
* @param string $table |
106
|
|
|
* @param string $idColumn |
107
|
|
|
* @param array $foreignKeys |
108
|
|
|
* @param array $hidden |
109
|
|
|
* @return DbalInterface |
110
|
|
|
*/ |
111
|
|
|
public function getOrm(string $table, string $idColumn = 'id', array $foreignKeys = [], array $hidden = []): DbalInterface |
112
|
|
|
{ |
113
|
|
|
return new $this->ormClass($table, $idColumn, $foreignKeys, $hidden); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Gets the ORM class |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getOrmClass(): string |
121
|
|
|
{ |
122
|
|
|
return $this->ormClass; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Gets the DB configurations |
127
|
|
|
* @return array|null |
128
|
|
|
*/ |
129
|
|
|
public function getConfigs(): ?array |
130
|
|
|
{ |
131
|
|
|
return $this->configs; |
132
|
|
|
} |
133
|
|
|
} |