1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saxulum\DoctrineOrmManagerRegistry\Doctrine; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry as ManagerRegistryInterface; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Doctrine\ORM\EntityRepository; |
9
|
|
|
use Doctrine\ORM\ORMException; |
10
|
|
|
use Pimple\Container; |
11
|
|
|
|
12
|
|
|
class ManagerRegistry implements ManagerRegistryInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Container |
16
|
|
|
*/ |
17
|
|
|
protected $container; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Connection[] |
21
|
|
|
*/ |
22
|
|
|
protected $connections; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $defaultConnectionName; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var EntityManager[] |
31
|
|
|
*/ |
32
|
|
|
protected $originalManagers; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var EntityManager[] |
36
|
|
|
*/ |
37
|
|
|
protected $resetManagers = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $defaultManagerName; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $proxyInterfaceName; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Container $container |
51
|
|
|
* @param string $proxyInterfaceName |
52
|
|
|
*/ |
53
|
|
|
public function __construct(Container $container, $proxyInterfaceName = 'Doctrine\ORM\Proxy\Proxy') |
54
|
|
|
{ |
55
|
|
|
$this->container = $container; |
56
|
|
|
$this->proxyInterfaceName = $proxyInterfaceName; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function getDefaultConnectionName() |
63
|
|
|
{ |
64
|
|
|
$this->loadConnections(); |
65
|
|
|
|
66
|
|
|
return $this->defaultConnectionName; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string|null $name |
71
|
|
|
* @return Connection |
72
|
|
|
* @throws \InvalidArgumentException |
73
|
|
|
*/ |
74
|
|
|
public function getConnection($name = null) |
75
|
|
|
{ |
76
|
|
|
$this->loadConnections(); |
77
|
|
|
|
78
|
|
|
$name = $this->validateName( |
79
|
|
|
$this->connections, |
80
|
|
|
$name, |
81
|
|
|
$this->getDefaultConnectionName()) |
82
|
|
|
; |
83
|
|
|
|
84
|
|
|
return $this->connections[$name]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return Connection[] |
89
|
|
|
*/ |
90
|
|
|
public function getConnections() |
91
|
|
|
{ |
92
|
|
|
$this->loadConnections(); |
93
|
|
|
|
94
|
|
|
if ($this->connections instanceof Container) { |
95
|
|
|
$connections = array(); |
96
|
|
|
foreach ($this->getConnectionNames() as $name) { |
97
|
|
|
$connections[$name] = $this->connections[$name]; |
98
|
|
|
} |
99
|
|
|
$this->connections = $connections; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->connections; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function getConnectionNames() |
109
|
|
|
{ |
110
|
|
|
$this->loadConnections(); |
111
|
|
|
|
112
|
|
|
if ($this->connections instanceof Container) { |
113
|
|
|
return $this->connections->keys(); |
114
|
|
|
} else { |
115
|
|
|
return array_keys($this->connections); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function loadConnections() |
120
|
|
|
{ |
121
|
|
|
if (is_null($this->connections)) { |
122
|
|
|
$this->connections = $this->container['dbs']; |
123
|
|
|
$this->defaultConnectionName = $this->container['dbs.default']; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
public function getDefaultManagerName() |
131
|
|
|
{ |
132
|
|
|
$this->loadManagers(); |
133
|
|
|
|
134
|
|
|
return $this->defaultManagerName; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string|null $name |
139
|
|
|
* @return EntityManager |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
public function getManager($name = null) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$this->loadManagers(); |
144
|
|
|
$name = $this->validateManagerName($name); |
145
|
|
|
|
146
|
|
|
return isset($this->resetManagers[$name]) ? $this->resetManagers[$name] : $this->originalManagers[$name]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string|null $name |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
protected function validateManagerName($name) |
154
|
|
|
{ |
155
|
|
|
return $this->validateName( |
156
|
|
|
$this->originalManagers, |
157
|
|
|
$name, |
158
|
|
|
$this->getDefaultManagerName()) |
159
|
|
|
; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param array $data |
164
|
|
|
* @param string $default |
165
|
|
|
* @param string|null $name |
166
|
|
|
* @return string |
167
|
|
|
* @throws \InvalidArgumentException |
168
|
|
|
*/ |
169
|
|
|
protected function validateName($data, $name, $default) |
170
|
|
|
{ |
171
|
|
|
if ($name === null) { |
172
|
|
|
$name = $default; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
if (!isset($data[$name])) { |
176
|
|
|
throw new \InvalidArgumentException(sprintf('Element named "%s" does not exist.', $name)); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return $name; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return EntityManager[] |
184
|
|
|
*/ |
185
|
|
|
public function getManagers() |
186
|
|
|
{ |
187
|
|
|
$this->loadManagers(); |
188
|
|
|
|
189
|
|
|
if ($this->originalManagers instanceof Container) { |
190
|
|
|
$managers = array(); |
191
|
|
|
foreach ($this->getManagerNames() as $name) { |
192
|
|
|
$managers[$name] = $this->originalManagers[$name]; |
193
|
|
|
} |
194
|
|
|
$this->originalManagers = $managers; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return array_replace($this->originalManagers, $this->resetManagers); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
public function getManagerNames() |
204
|
|
|
{ |
205
|
|
|
$this->loadManagers(); |
206
|
|
|
|
207
|
|
|
if ($this->originalManagers instanceof Container) { |
208
|
|
|
return $this->originalManagers->keys(); |
209
|
|
|
} else { |
210
|
|
|
return array_keys($this->originalManagers); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param string|null $name |
216
|
|
|
* @return void |
217
|
|
|
*/ |
218
|
|
View Code Duplication |
public function resetManager($name = null) |
|
|
|
|
219
|
|
|
{ |
220
|
|
|
$this->loadManagers(); |
221
|
|
|
$name = $this->validateManagerName($name); |
222
|
|
|
|
223
|
|
|
$this->resetManagers[$name] = $this->container['orm.ems.factory'][$name](); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
protected function loadManagers() |
227
|
|
|
{ |
228
|
|
|
if (is_null($this->originalManagers)) { |
229
|
|
|
$this->originalManagers = $this->container['orm.ems']; |
230
|
|
|
$this->defaultManagerName = $this->container['orm.ems.default']; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @param string $alias |
236
|
|
|
* @return string |
237
|
|
|
* @throws ORMException |
238
|
|
|
*/ |
239
|
|
|
public function getAliasNamespace($alias) |
240
|
|
|
{ |
241
|
|
|
foreach ($this->getManagerNames() as $name) { |
242
|
|
|
try { |
243
|
|
|
return $this->getManager($name)->getConfiguration()->getEntityNamespace($alias); |
244
|
|
|
} catch (ORMException $e) { |
245
|
|
|
// throw the exception only if no manager can solve it |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
throw ORMException::unknownEntityNamespace($alias); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string $persistentObject |
253
|
|
|
* @param null $persistentManagerName |
254
|
|
|
* @return EntityRepository |
255
|
|
|
*/ |
256
|
|
|
public function getRepository($persistentObject, $persistentManagerName = null) |
257
|
|
|
{ |
258
|
|
|
return $this->getManager($persistentManagerName)->getRepository($persistentObject); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param string $class |
263
|
|
|
* @return EntityManager|null |
264
|
|
|
*/ |
265
|
|
|
public function getManagerForClass($class) |
266
|
|
|
{ |
267
|
|
|
$proxyClass = new \ReflectionClass($class); |
268
|
|
|
if ($proxyClass->implementsInterface($this->proxyInterfaceName)) { |
269
|
|
|
$class = $proxyClass->getParentClass()->getName(); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
foreach ($this->getManagerNames() as $managerName) { |
273
|
|
|
if (!$this->getManager($managerName)->getMetadataFactory()->isTransient($class)) { |
274
|
|
|
return $this->getManager($managerName); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
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.