1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrine\Service\Connection; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasDoctrine\Config\DoctrineConfig; |
8
|
|
|
use Arp\LaminasDoctrine\Service\Connection\Exception\ConnectionFactoryException; |
9
|
|
|
use Arp\LaminasDoctrine\Service\Connection\Exception\ConnectionManagerException; |
10
|
|
|
use Doctrine\DBAL\Connection; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Alex Patterson <[email protected]> |
14
|
|
|
* @package Arp\LaminasDoctrine\Service\Connection |
15
|
|
|
*/ |
16
|
|
|
final class ConnectionManager implements ConnectionManagerInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var DoctrineConfig |
20
|
|
|
*/ |
21
|
|
|
private DoctrineConfig $config; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Connection[] |
25
|
|
|
*/ |
26
|
|
|
private array $connections = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ConnectionFactoryInterface |
30
|
|
|
*/ |
31
|
|
|
private ConnectionFactoryInterface $factory; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param DoctrineConfig $config |
35
|
|
|
* @param ConnectionFactoryInterface $factory |
36
|
|
|
* @param Connection[] $connections |
37
|
|
|
*/ |
38
|
|
|
public function __construct( |
39
|
|
|
DoctrineConfig $config, |
40
|
|
|
ConnectionFactoryInterface $factory, |
41
|
|
|
array $connections |
42
|
|
|
) { |
43
|
|
|
$this->config = $config; |
44
|
|
|
$this->factory = $factory; |
45
|
|
|
|
46
|
|
|
$this->setConnections($connections); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $name |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function hasConnection(string $name): bool |
55
|
|
|
{ |
56
|
|
|
return isset($this->connections[$name]) || $this->config->hasConnectionConfig($name); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $name |
61
|
|
|
* |
62
|
|
|
* @return Connection |
63
|
|
|
* |
64
|
|
|
* @throws ConnectionManagerException |
65
|
|
|
*/ |
66
|
|
|
public function getConnection(string $name): Connection |
67
|
|
|
{ |
68
|
|
|
if (!isset($this->connections[$name]) && $this->config->hasConnectionConfig($name)) { |
69
|
|
|
$this->connections[$name] = $this->create($name, $this->config->getConnectionConfig($name)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (isset($this->connections[$name])) { |
73
|
|
|
return $this->connections[$name]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
throw new ConnectionManagerException( |
77
|
|
|
sprintf('Failed to establish connection \'%s\': Failed to find a the required configuration', $name) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $name |
83
|
|
|
* @param array $config |
84
|
|
|
* |
85
|
|
|
* @return Connection |
86
|
|
|
* |
87
|
|
|
* @throws ConnectionManagerException |
88
|
|
|
*/ |
89
|
|
|
private function create(string $name, array $config): Connection |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
return $this->factory->create($config); |
93
|
|
|
} catch (ConnectionFactoryException $e) { |
94
|
|
|
throw new ConnectionManagerException( |
95
|
|
|
sprintf('Failed to establish connection \'%s\': %s', $name, $e->getMessage()), |
96
|
|
|
$e->getMessage(), |
|
|
|
|
97
|
|
|
$e |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Connection[]|array[] $connections |
104
|
|
|
*/ |
105
|
|
|
public function setConnections(array $connections): void |
106
|
|
|
{ |
107
|
|
|
$this->connections = []; |
108
|
|
|
|
109
|
|
|
foreach ($connections as $name => $connection) { |
110
|
|
|
if (is_array($connection)) { |
111
|
|
|
$this->addConnectionConfig($name, $connection); |
112
|
|
|
continue; |
113
|
|
|
} |
114
|
|
|
$this->setConnection($name, $connection); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $name |
120
|
|
|
* @param Connection $connection |
121
|
|
|
*/ |
122
|
|
|
public function setConnection(string $name, Connection $connection): void |
123
|
|
|
{ |
124
|
|
|
$this->connections[$name] = $connection; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $name |
129
|
|
|
* @param array $config |
130
|
|
|
*/ |
131
|
|
|
public function addConnectionConfig(string $name, array $config): void |
132
|
|
|
{ |
133
|
|
|
$this->config->setConnectionConfig($name, $config); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|