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\InjectableConfig; |
18
|
|
|
use Spiral\Core\Traits\Config\AliasTrait; |
19
|
|
|
|
20
|
|
|
final class DatabaseConfig extends InjectableConfig |
21
|
|
|
{ |
22
|
|
|
use AliasTrait; |
|
|
|
|
23
|
|
|
|
24
|
|
|
public const CONFIG = 'database'; |
25
|
|
|
public const DEFAULT_DATABASE = 'default'; |
26
|
|
|
|
27
|
|
|
public function __construct(array $config = []) |
28
|
|
|
{ |
29
|
|
|
parent::__construct(\array_merge([ |
30
|
|
|
'default' => self::DEFAULT_DATABASE, |
31
|
|
|
'aliases' => [], |
32
|
|
|
'databases' => [], |
33
|
|
|
'connections' => [], |
34
|
|
|
], $config)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getDefaultDatabase(): string |
38
|
|
|
{ |
39
|
|
|
return $this->config['default'] ?? 'default'; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
/** |
43
|
|
|
* Get named list of all databases. |
44
|
2 |
|
* |
45
|
|
|
* @return DatabasePartial[] |
46
|
|
|
*/ |
47
|
|
|
public function getDatabases(): array |
48
|
|
|
{ |
49
|
|
|
$result = []; |
50
|
|
|
foreach (\array_keys($this->config['databases'] ?? []) as $database) { |
51
|
|
|
$result[$database] = $this->getDatabase($database); |
52
|
6 |
|
} |
53
|
|
|
|
54
|
6 |
|
return $result; |
55
|
6 |
|
} |
56
|
6 |
|
|
57
|
|
|
/** |
58
|
|
|
* Get names list of all driver connections. |
59
|
6 |
|
* |
60
|
|
|
* @return DriverInterface[] |
61
|
|
|
*/ |
62
|
|
|
public function getDrivers(): array |
63
|
|
|
{ |
64
|
|
|
$result = []; |
65
|
|
|
foreach (\array_keys($this->config['connections'] ?? $this->config['drivers'] ?? []) as $driver) { |
66
|
|
|
$result[$driver] = $this->getDriver($driver); |
67
|
6 |
|
} |
68
|
|
|
|
69
|
6 |
|
return $result; |
70
|
6 |
|
} |
71
|
4 |
|
|
72
|
|
|
public function hasDatabase(string $database): bool |
73
|
|
|
{ |
74
|
6 |
|
return isset($this->config['databases'][$database]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @throws ConfigException |
79
|
|
|
*/ |
80
|
|
|
public function getDatabase(string $database): DatabasePartial |
81
|
|
|
{ |
82
|
26 |
|
if (!$this->hasDatabase($database)) { |
83
|
|
|
throw new ConfigException("Undefined database `{$database}`"); |
84
|
26 |
|
} |
85
|
|
|
|
86
|
|
|
$config = $this->config['databases'][$database]; |
87
|
|
|
|
88
|
|
|
return new DatabasePartial( |
89
|
|
|
$database, |
90
|
|
|
$config['tablePrefix'] ?? $config['prefix'] ?? '', |
91
|
|
|
$config['connection'] ?? $config['write'] ?? $config['driver'], |
92
|
|
|
$config['readConnection'] ?? $config['read'] ?? $config['readDriver'] ?? null, |
93
|
|
|
); |
94
|
20 |
|
} |
95
|
|
|
|
96
|
20 |
|
public function hasDriver(string $driver): bool |
97
|
2 |
|
{ |
98
|
|
|
return isset($this->config['connections'][$driver]) || isset($this->config['drivers'][$driver]); |
99
|
|
|
} |
100
|
18 |
|
|
101
|
|
|
/** |
102
|
18 |
|
* @throws ConfigException |
103
|
|
|
*/ |
104
|
18 |
|
public function getDriver(string $driver): DriverInterface |
105
|
18 |
|
{ |
106
|
18 |
|
if (!$this->hasDriver($driver)) { |
107
|
|
|
throw new ConfigException("Undefined driver `{$driver}`"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$config = $this->config['connections'][$driver] ?? $this->config['drivers'][$driver]; |
111
|
|
|
|
112
|
|
|
if ($config instanceof DriverConfig) { |
113
|
|
|
$driverObject = $config->driver::create($config); |
114
|
|
|
|
115
|
28 |
|
if ($driverObject instanceof NamedInterface) { |
116
|
|
|
return $driverObject->withName($driver); |
|
|
|
|
117
|
28 |
|
} |
118
|
|
|
|
119
|
|
|
return $driverObject; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
throw new \InvalidArgumentException( |
123
|
|
|
\vsprintf('Driver config must be an instance of %s, but %s passed', [ |
124
|
|
|
DriverConfig::class, |
125
|
|
|
\get_debug_type($config), |
126
|
|
|
]), |
127
|
26 |
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
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.