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