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