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