1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Cycle ORM package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Cycle\Database\Config; |
13
|
|
|
|
14
|
|
|
use Cycle\Database\Driver\DriverInterface; |
15
|
|
|
use Cycle\Database\Exception\ConfigException; |
16
|
|
|
use Cycle\Database\NamedInterface; |
17
|
|
|
use Spiral\Core\ConfigsInterface; |
18
|
|
|
use Spiral\Core\Container\InjectableInterface; |
19
|
|
|
use Spiral\Core\Traits\Config\AliasTrait; |
20
|
|
|
|
21
|
|
|
final class DatabaseConfig implements InjectableInterface, \IteratorAggregate, \ArrayAccess |
22
|
|
|
{ |
23
|
|
|
use AliasTrait; |
|
|
|
|
24
|
|
|
|
25
|
|
|
public const INJECTOR = ConfigsInterface::class; |
26
|
|
|
public const CONFIG = 'database'; |
27
|
|
|
public const DEFAULT_DATABASE = 'default'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* At this moment on array based configs can be supported. |
31
|
|
|
*/ |
32
|
|
|
public function __construct( |
33
|
|
|
private array $config = [ |
34
|
|
|
'default' => self::DEFAULT_DATABASE, |
35
|
|
|
'aliases' => [], |
36
|
|
|
'databases' => [], |
37
|
|
|
'connections' => [], |
38
|
|
|
] |
39
|
|
|
) { |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public function getDefaultDatabase(): string |
43
|
|
|
{ |
44
|
2 |
|
return $this->config['default'] ?? 'default'; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get named list of all databases. |
49
|
|
|
* |
50
|
|
|
* @return DatabasePartial[] |
51
|
|
|
*/ |
52
|
6 |
|
public function getDatabases(): array |
53
|
|
|
{ |
54
|
6 |
|
$result = []; |
55
|
6 |
|
foreach (array_keys($this->config['databases'] ?? []) as $database) { |
56
|
6 |
|
$result[$database] = $this->getDatabase($database); |
57
|
|
|
} |
58
|
|
|
|
59
|
6 |
|
return $result; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get names list of all driver connections. |
64
|
|
|
* |
65
|
|
|
* @return DriverInterface[] |
66
|
|
|
*/ |
67
|
6 |
|
public function getDrivers(): array |
68
|
|
|
{ |
69
|
6 |
|
$result = []; |
70
|
6 |
|
foreach (array_keys($this->config['connections'] ?? $this->config['drivers'] ?? []) as $driver) { |
71
|
4 |
|
$result[$driver] = $this->getDriver($driver); |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
return $result; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function hasDatabase(string $database): bool |
78
|
|
|
{ |
79
|
|
|
return isset($this->config['databases'][$database]); |
80
|
|
|
} |
81
|
|
|
|
82
|
26 |
|
/** |
83
|
|
|
* @throws ConfigException |
84
|
26 |
|
*/ |
85
|
|
|
public function getDatabase(string $database): DatabasePartial |
86
|
|
|
{ |
87
|
|
|
if (!$this->hasDatabase($database)) { |
88
|
|
|
throw new ConfigException("Undefined database `{$database}`"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$config = $this->config['databases'][$database]; |
92
|
|
|
|
93
|
|
|
return new DatabasePartial( |
94
|
20 |
|
$database, |
95
|
|
|
$config['tablePrefix'] ?? $config['prefix'] ?? '', |
96
|
20 |
|
$config['connection'] ?? $config['write'] ?? $config['driver'], |
97
|
2 |
|
$config['readConnection'] ?? $config['read'] ?? $config['readDriver'] ?? null |
98
|
|
|
); |
99
|
|
|
} |
100
|
18 |
|
|
101
|
|
|
public function hasDriver(string $driver): bool |
102
|
18 |
|
{ |
103
|
|
|
return isset($this->config['connections'][$driver]) || isset($this->config['drivers'][$driver]); |
104
|
18 |
|
} |
105
|
18 |
|
|
106
|
18 |
|
/** |
107
|
|
|
* @throws ConfigException |
108
|
|
|
*/ |
109
|
|
|
public function getDriver(string $driver): DriverInterface |
110
|
|
|
{ |
111
|
|
|
if (!$this->hasDriver($driver)) { |
112
|
|
|
throw new ConfigException("Undefined driver `{$driver}`"); |
113
|
|
|
} |
114
|
|
|
|
115
|
28 |
|
$config = $this->config['connections'][$driver] ?? $this->config['drivers'][$driver]; |
116
|
|
|
|
117
|
28 |
|
if ($config instanceof DriverConfig) { |
118
|
|
|
$driverObject = $config->driver::create($config); |
119
|
|
|
|
120
|
|
|
if ($driverObject instanceof NamedInterface) { |
121
|
|
|
return $driverObject->withName($driver); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $driverObject; |
125
|
|
|
} |
126
|
|
|
|
127
|
26 |
|
throw new \InvalidArgumentException( |
128
|
|
|
\vsprintf('Driver config must be an instance of %s, but %s passed', [ |
129
|
26 |
|
DriverConfig::class, |
130
|
6 |
|
\get_debug_type($config), |
131
|
|
|
]) |
132
|
|
|
); |
133
|
20 |
|
} |
134
|
|
|
|
135
|
20 |
|
public function toArray(): array |
136
|
18 |
|
{ |
137
|
|
|
return $this->config; |
138
|
18 |
|
} |
139
|
18 |
|
|
140
|
|
|
public function offsetExists($offset) |
141
|
|
|
{ |
142
|
|
|
return array_key_exists($offset, $this->config); |
143
|
|
|
} |
144
|
|
|
|
145
|
2 |
|
public function offsetGet($offset) |
146
|
2 |
|
{ |
147
|
2 |
|
if (!$this->offsetExists($offset)) { |
148
|
2 |
|
throw new \Spiral\Core\Exception\ConfigException("Undefined configuration key '{$offset}'"); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->config[$offset]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @throws ConfigException |
156
|
|
|
*/ |
157
|
|
|
public function offsetSet($offset, $value): void |
158
|
|
|
{ |
159
|
|
|
throw new ConfigException( |
160
|
|
|
'Unable to change configuration data, configs are treated as immutable by default' |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @throws ConfigException |
166
|
|
|
*/ |
167
|
|
|
public function offsetUnset($offset): void |
168
|
|
|
{ |
169
|
|
|
throw new ConfigException( |
170
|
|
|
'Unable to change configuration data, configs are treated as immutable by default' |
171
|
|
|
); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function getIterator() |
175
|
|
|
{ |
176
|
|
|
return new \ArrayIterator($this->config); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
This trait has been deprecated. The supplier of the trait has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.