1 | <?php |
||||
2 | |||||
3 | namespace Bdf\Prime; |
||||
4 | |||||
5 | use Bdf\Prime\Connection\ConnectionInterface; |
||||
6 | use Bdf\Prime\Connection\ConnectionRegistry; |
||||
7 | use Bdf\Prime\Connection\SubConnectionManagerInterface; |
||||
8 | use Bdf\Prime\Exception\DBALException; |
||||
9 | use LogicException; |
||||
10 | |||||
11 | /** |
||||
12 | * ConnectionManager |
||||
13 | * |
||||
14 | * doctrine dbal connection registry |
||||
15 | */ |
||||
16 | class ConnectionManager implements ConnectionRegistryInterface |
||||
17 | { |
||||
18 | /** |
||||
19 | * The connection registry |
||||
20 | * |
||||
21 | * @var ConnectionRegistryInterface |
||||
22 | */ |
||||
23 | private $registry; |
||||
24 | |||||
25 | /** |
||||
26 | * Connections list |
||||
27 | * |
||||
28 | * @var ConnectionInterface[] |
||||
29 | */ |
||||
30 | private $connections = []; |
||||
31 | |||||
32 | /** |
||||
33 | * Default connection to use |
||||
34 | * |
||||
35 | * @var string|null |
||||
36 | */ |
||||
37 | private $defaultConnection; |
||||
38 | |||||
39 | |||||
40 | /** |
||||
41 | * Set default configuration |
||||
42 | * |
||||
43 | * @param ConnectionRegistryInterface $registry |
||||
44 | */ |
||||
45 | 466 | public function __construct(ConnectionRegistryInterface $registry = null) |
|||
46 | { |
||||
47 | 466 | $this->registry = $registry ?: new ConnectionRegistry(); |
|||
48 | } |
||||
49 | |||||
50 | /** |
||||
51 | * Add database connection |
||||
52 | * |
||||
53 | * @param ConnectionInterface $connection Unique name for the connection |
||||
54 | * @param boolean $default Use this connection as the default? The first connection added is automatically set as the default, even if this flag is false. |
||||
55 | * |
||||
56 | * @throws LogicException if connection exists |
||||
57 | * |
||||
58 | * @return void |
||||
59 | */ |
||||
60 | 400 | public function addConnection(ConnectionInterface $connection, bool $default = false): void |
|||
61 | { |
||||
62 | // Connection name must be unique |
||||
63 | 400 | if (isset($this->connections[$connection->getName()])) { |
|||
64 | 1 | throw new LogicException('Connection for "'.$connection->getName().'" already exists. Connection name must be unique.'); |
|||
65 | } |
||||
66 | |||||
67 | // Set as default connection? |
||||
68 | 400 | if (true === $default || null === $this->defaultConnection) { |
|||
69 | 383 | $this->defaultConnection = $connection->getName(); |
|||
70 | } |
||||
71 | |||||
72 | 400 | $this->connections[$connection->getName()] = $connection; |
|||
73 | } |
||||
74 | |||||
75 | /** |
||||
76 | * Remove a connection by its name |
||||
77 | * |
||||
78 | * @param string $name |
||||
79 | * |
||||
80 | * @return void |
||||
81 | */ |
||||
82 | 40 | public function removeConnection(string $name): void |
|||
83 | { |
||||
84 | 40 | if (!isset($this->connections[$name])) { |
|||
85 | 26 | return; |
|||
86 | } |
||||
87 | |||||
88 | 14 | $this->connections[$name]->close(); |
|||
89 | 14 | unset($this->connections[$name]); |
|||
90 | } |
||||
91 | |||||
92 | /** |
||||
93 | * {@inheritDoc} |
||||
94 | */ |
||||
95 | 1328 | public function getConnection(string $name = null): ConnectionInterface |
|||
96 | { |
||||
97 | 1328 | if ($name === null) { |
|||
98 | 5 | $name = $this->defaultConnection; |
|||
99 | } |
||||
100 | |||||
101 | // Connection name must be unique |
||||
102 | 1328 | if (!isset($this->connections[$name]) && !$this->loadSubConnection($name)) { |
|||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
103 | 395 | $this->addConnection($this->registry->getConnection($name)); |
|||
0 ignored issues
–
show
It seems like
$name can also be of type null ; however, parameter $name of Bdf\Prime\ConnectionRegi...erface::getConnection() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
104 | } |
||||
105 | |||||
106 | 1327 | return $this->connections[$name]; |
|||
107 | } |
||||
108 | |||||
109 | /** |
||||
110 | * Associate configuration to connection |
||||
111 | * |
||||
112 | * @param string $connectionName |
||||
113 | * @param string|array $parameters |
||||
114 | * |
||||
115 | * @return void |
||||
116 | */ |
||||
117 | 42 | public function declareConnection(string $connectionName, $parameters): void |
|||
118 | { |
||||
119 | 42 | if ($this->registry instanceof ConnectionRegistry) { |
|||
120 | 42 | $this->registry->declareConnection($connectionName, $parameters); |
|||
121 | } |
||||
122 | } |
||||
123 | |||||
124 | /** |
||||
125 | * Get all connections |
||||
126 | * |
||||
127 | * @return ConnectionInterface[] Array of connection objects |
||||
128 | */ |
||||
129 | 1445 | public function connections(): array |
|||
130 | { |
||||
131 | 1445 | return $this->connections; |
|||
132 | } |
||||
133 | |||||
134 | /** |
||||
135 | * {@inheritDoc} |
||||
136 | */ |
||||
137 | 3 | public function getConnectionNames(): array |
|||
138 | { |
||||
139 | 3 | return array_unique(array_merge($this->getCurrentConnectionNames(), $this->registry->getConnectionNames())); |
|||
140 | } |
||||
141 | |||||
142 | /** |
||||
143 | * Gets the name of connections in progress |
||||
144 | */ |
||||
145 | 4 | public function getCurrentConnectionNames(): array |
|||
146 | { |
||||
147 | 4 | return array_keys($this->connections); |
|||
148 | } |
||||
149 | |||||
150 | /** |
||||
151 | * Set the default connection name |
||||
152 | * |
||||
153 | * @param string $name |
||||
154 | * |
||||
155 | * @return void |
||||
156 | */ |
||||
157 | 1 | public function setDefaultConnection(string $name): void |
|||
158 | { |
||||
159 | 1 | $this->defaultConnection = $name; |
|||
160 | } |
||||
161 | |||||
162 | /** |
||||
163 | * Get the default connection name |
||||
164 | * |
||||
165 | * @return string|null The default connection, or null if there is no available connections |
||||
166 | */ |
||||
167 | 4 | public function getDefaultConnection(): ?string |
|||
168 | { |
||||
169 | 4 | return $this->defaultConnection; |
|||
170 | } |
||||
171 | |||||
172 | /** |
||||
173 | * Try to load a sub connection |
||||
174 | * |
||||
175 | * This method allows connection as "name.otherName". |
||||
176 | * Works only if connection "name" is a SubConnectionManagerInterface. |
||||
177 | * |
||||
178 | * @param string $connectionName |
||||
179 | * |
||||
180 | * @return bool The connection has been loaded |
||||
181 | */ |
||||
182 | 395 | private function loadSubConnection(string $connectionName): bool |
|||
183 | { |
||||
184 | 395 | $names = explode('.', $connectionName, 2); |
|||
185 | |||||
186 | 395 | if (!isset($names[1])) { |
|||
187 | 395 | return false; |
|||
188 | } |
||||
189 | |||||
190 | 2 | $connection = $this->getConnection($names[0]); |
|||
191 | |||||
192 | 2 | if ($connection instanceof SubConnectionManagerInterface) { |
|||
193 | //TODO doit on concerver une reference sur la sous connection ? |
||||
194 | 2 | $this->connections[$connectionName] = $connection->getConnection($names[1]); |
|||
195 | 2 | return true; |
|||
196 | } |
||||
197 | |||||
198 | return false; |
||||
199 | } |
||||
200 | } |
||||
201 |