1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\DoctrineDbServiceProvider\Registry; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ConnectionRegistry; |
8
|
|
|
use Doctrine\DBAL\Connection; |
9
|
|
|
use Pimple\Container; |
10
|
|
|
|
11
|
|
|
final class DoctrineDbalConnectionRegistry implements ConnectionRegistry |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Container |
15
|
|
|
*/ |
16
|
|
|
private $container; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Container|Connection[] |
20
|
|
|
*/ |
21
|
|
|
private $connections; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $defaultConnectionName; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Container|EntityManager[] |
30
|
|
|
*/ |
31
|
|
|
private $originalManagers; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var EntityManager[] |
35
|
|
|
*/ |
36
|
|
|
private $resetedManagers = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $defaultManagerName; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Container $container |
45
|
|
|
*/ |
46
|
5 |
|
public function __construct(Container $container) |
47
|
|
|
{ |
48
|
5 |
|
$this->container = $container; |
49
|
5 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
3 |
|
public function getDefaultConnectionName(): string |
55
|
|
|
{ |
56
|
3 |
|
$this->loadConnections(); |
57
|
|
|
|
58
|
3 |
|
return $this->defaultConnectionName; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string|null $name |
63
|
|
|
* |
64
|
|
|
* @return Connection |
65
|
|
|
* |
66
|
|
|
* @throws \InvalidArgumentException |
67
|
|
|
*/ |
68
|
2 |
View Code Duplication |
public function getConnection($name = null): Connection |
|
|
|
|
69
|
|
|
{ |
70
|
2 |
|
$this->loadConnections(); |
71
|
|
|
|
72
|
2 |
|
$name = $name ?? $this->getDefaultConnectionName(); |
73
|
|
|
|
74
|
2 |
|
if (!isset($this->connections[$name])) { |
75
|
1 |
|
throw new \InvalidArgumentException(sprintf('Missing connection with name "%s".', $name)); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
return $this->connections[$name]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Connection[] |
83
|
|
|
*/ |
84
|
1 |
View Code Duplication |
public function getConnections(): array |
|
|
|
|
85
|
|
|
{ |
86
|
1 |
|
$this->loadConnections(); |
87
|
|
|
|
88
|
1 |
|
$connections = array(); |
89
|
1 |
|
foreach ($this->connections->keys() as $name) { |
90
|
1 |
|
$connections[$name] = $this->connections[$name]; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return $connections; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string[] |
98
|
|
|
*/ |
99
|
1 |
|
public function getConnectionNames(): array |
100
|
|
|
{ |
101
|
1 |
|
$this->loadConnections(); |
102
|
|
|
|
103
|
1 |
|
return $this->connections->keys(); |
104
|
|
|
} |
105
|
|
|
|
106
|
5 |
View Code Duplication |
private function loadConnections() |
|
|
|
|
107
|
|
|
{ |
108
|
5 |
|
if (null === $this->connections) { |
109
|
5 |
|
$this->connections = $this->container['doctrine.dbal.dbs']; |
110
|
5 |
|
$this->defaultConnectionName = $this->container['doctrine.dbal.dbs.default']; |
111
|
|
|
} |
112
|
5 |
|
} |
113
|
|
|
} |
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.