1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Persistence; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use ReflectionClass; |
9
|
|
|
use function explode; |
10
|
|
|
use function sprintf; |
11
|
|
|
use function strpos; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Abstract implementation of the ManagerRegistry contract. |
15
|
|
|
*/ |
16
|
|
|
abstract class AbstractManagerRegistry implements ManagerRegistry |
17
|
|
|
{ |
18
|
|
|
/** @var string */ |
19
|
|
|
private $name; |
20
|
|
|
|
21
|
|
|
/** @var array<string, string> */ |
22
|
|
|
private $connections; |
23
|
|
|
|
24
|
|
|
/** @var array<string, string> */ |
25
|
|
|
private $managers; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $defaultConnection; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
private $defaultManager; |
32
|
|
|
|
33
|
|
|
/** @var string */ |
34
|
|
|
private $proxyInterfaceName; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array<string, string> $connections |
38
|
|
|
* @param array<string, string> $managers |
39
|
|
|
*/ |
40
|
9 |
|
public function __construct( |
41
|
|
|
string $name, |
42
|
|
|
array $connections, |
43
|
|
|
array $managers, |
44
|
|
|
string $defaultConnection, |
45
|
|
|
string $defaultManager, |
46
|
|
|
string $proxyInterfaceName |
47
|
|
|
) { |
48
|
9 |
|
$this->name = $name; |
49
|
9 |
|
$this->connections = $connections; |
50
|
9 |
|
$this->managers = $managers; |
51
|
9 |
|
$this->defaultConnection = $defaultConnection; |
52
|
9 |
|
$this->defaultManager = $defaultManager; |
53
|
9 |
|
$this->proxyInterfaceName = $proxyInterfaceName; |
54
|
9 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Fetches/creates the given services. |
58
|
|
|
* |
59
|
|
|
* A service in this context is connection or a manager instance. |
60
|
|
|
* |
61
|
|
|
* @param string $name The name of the service. |
62
|
|
|
* |
63
|
|
|
* @return object The instance of the given service. |
64
|
|
|
*/ |
65
|
|
|
abstract protected function getService(string $name) : object; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Resets the given services. |
69
|
|
|
* |
70
|
|
|
* A service in this context is connection or a manager instance. |
71
|
|
|
* |
72
|
|
|
* @param string $name The name of the service. |
73
|
|
|
*/ |
74
|
|
|
abstract protected function resetService(string $name) : void; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets the name of the registry. |
78
|
|
|
*/ |
79
|
|
|
public function getName() : string |
80
|
|
|
{ |
81
|
|
|
return $this->name; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
|
|
public function getConnection(?string $name = null) : object |
88
|
|
|
{ |
89
|
|
|
if ($name === null) { |
90
|
|
|
$name = $this->defaultConnection; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (! isset($this->connections[$name])) { |
94
|
|
|
throw new InvalidArgumentException( |
95
|
|
|
sprintf('Doctrine %s Connection named "%s" does not exist.', $this->name, $name) |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $this->getService($this->connections[$name]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
|
|
public function getConnectionNames() : array |
106
|
|
|
{ |
107
|
|
|
return $this->connections; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function getConnections() : array |
114
|
|
|
{ |
115
|
|
|
$connections = []; |
116
|
|
|
foreach ($this->connections as $name => $id) { |
117
|
|
|
$connections[$name] = $this->getService($id); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $connections; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
public function getDefaultConnectionName() : string |
127
|
|
|
{ |
128
|
|
|
return $this->defaultConnection; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
*/ |
134
|
|
|
public function getDefaultManagerName() : string |
135
|
|
|
{ |
136
|
|
|
return $this->defaultManager; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
* |
142
|
|
|
* @throws InvalidArgumentException |
143
|
|
|
*/ |
144
|
7 |
|
public function getManager(?string $name = null) : ObjectManager |
145
|
|
|
{ |
146
|
7 |
|
if ($name === null) { |
147
|
4 |
|
$name = $this->defaultManager; |
148
|
|
|
} |
149
|
|
|
|
150
|
7 |
|
if (! isset($this->managers[$name])) { |
151
|
|
|
throw new InvalidArgumentException( |
152
|
|
|
sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name) |
153
|
|
|
); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** @var ObjectManager $objectManager */ |
157
|
7 |
|
$objectManager = $this->getService($this->managers[$name]); |
158
|
|
|
|
159
|
7 |
|
return $objectManager; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
7 |
|
public function getManagerForClass(string $class) : ?ObjectManager |
166
|
|
|
{ |
167
|
|
|
// Check for namespace alias |
168
|
7 |
|
if (strpos($class, ':') !== false) { |
169
|
3 |
|
[$namespaceAlias, $simpleClassName] = explode(':', $class, 2); |
170
|
|
|
|
171
|
3 |
|
$class = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName; |
172
|
|
|
} |
173
|
|
|
|
174
|
7 |
|
$proxyClass = new ReflectionClass($class); |
175
|
|
|
|
176
|
5 |
|
if ($proxyClass->implementsInterface($this->proxyInterfaceName)) { |
177
|
|
|
$parentClass = $proxyClass->getParentClass(); |
178
|
|
|
|
179
|
|
|
if ($parentClass === false) { |
180
|
|
|
return null; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
$class = $parentClass->getName(); |
184
|
|
|
} |
185
|
|
|
|
186
|
5 |
|
if($this->defaultManager) { |
|
|
|
|
187
|
|
|
/** @var ObjectManager $manager */ |
188
|
5 |
|
$manager = $this->getManager($this->defaultManager); |
189
|
|
|
|
190
|
5 |
|
if (! $manager->getMetadataFactory()->isTransient($class)) { |
191
|
3 |
|
return $manager; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
2 |
|
foreach ($this->managers as $id) { |
196
|
|
|
/** @var ObjectManager $manager */ |
197
|
2 |
|
$manager = $this->getService($id); |
198
|
|
|
|
199
|
2 |
|
if (! $manager->getMetadataFactory()->isTransient($class)) { |
200
|
1 |
|
return $manager; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
1 |
|
return null; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* {@inheritdoc} |
209
|
|
|
*/ |
210
|
|
|
public function getManagerNames() : array |
211
|
|
|
{ |
212
|
|
|
return $this->managers; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* {@inheritdoc} |
217
|
|
|
*/ |
218
|
|
|
public function getManagers() : array |
219
|
|
|
{ |
220
|
|
|
$managers = []; |
221
|
|
|
|
222
|
|
|
foreach ($this->managers as $name => $id) { |
223
|
|
|
/** @var ObjectManager $manager */ |
224
|
|
|
$manager = $this->getService($id); |
225
|
|
|
|
226
|
|
|
$managers[$name] = $manager; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $managers; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
*/ |
235
|
3 |
|
public function getRepository( |
236
|
|
|
string $persistentObjectName, |
237
|
|
|
?string $persistentManagerName = null |
238
|
|
|
) : ObjectRepository { |
239
|
|
|
return $this |
240
|
3 |
|
->selectManager($persistentObjectName, $persistentManagerName) |
241
|
3 |
|
->getRepository($persistentObjectName); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* {@inheritdoc} |
246
|
|
|
*/ |
247
|
1 |
|
public function resetManager(?string $name = null) : ObjectManager |
248
|
|
|
{ |
249
|
1 |
|
if ($name === null) { |
250
|
1 |
|
$name = $this->defaultManager; |
251
|
|
|
} |
252
|
|
|
|
253
|
1 |
|
if (! isset($this->managers[$name])) { |
254
|
|
|
throw new InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name)); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
// force the creation of a new document manager |
258
|
|
|
// if the current one is closed |
259
|
1 |
|
$this->resetService($this->managers[$name]); |
260
|
|
|
|
261
|
1 |
|
return $this->getManager($name); |
262
|
|
|
} |
263
|
|
|
|
264
|
3 |
|
private function selectManager( |
265
|
|
|
string $persistentObjectName, |
266
|
|
|
?string $persistentManagerName = null |
267
|
|
|
) : ObjectManager { |
268
|
3 |
|
if ($persistentManagerName !== null) { |
269
|
1 |
|
return $this->getManager($persistentManagerName); |
270
|
|
|
} |
271
|
|
|
|
272
|
2 |
|
return $this->getManagerForClass($persistentObjectName) ?? $this->getManager(); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|