1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\ServiceProvider\Registry; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
8
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
9
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
10
|
|
|
use Doctrine\Common\Persistence\Proxy; |
11
|
|
|
use Doctrine\DBAL\Connection; |
12
|
|
|
use Doctrine\ORM\EntityManager; |
13
|
|
|
use Doctrine\ORM\EntityRepository; |
14
|
|
|
use Doctrine\ORM\ORMException; |
15
|
|
|
use Pimple\Container; |
16
|
|
|
|
17
|
|
|
final class DoctrineOrmManagerRegistry implements ManagerRegistry |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var Container |
21
|
|
|
*/ |
22
|
|
|
private $container; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Container|Connection[] |
26
|
|
|
*/ |
27
|
|
|
private $connections; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $defaultConnectionName; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Container|EntityManager[] |
36
|
|
|
*/ |
37
|
|
|
private $originalManagers; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var EntityManager[] |
41
|
|
|
*/ |
42
|
|
|
private $resetedManagers = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $defaultManagerName; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Container $container |
51
|
|
|
*/ |
52
|
17 |
|
public function __construct(Container $container) |
53
|
|
|
{ |
54
|
17 |
|
$this->container = $container; |
55
|
17 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
3 |
|
public function getDefaultConnectionName(): string |
61
|
|
|
{ |
62
|
3 |
|
$this->loadConnections(); |
63
|
|
|
|
64
|
3 |
|
return $this->defaultConnectionName; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string|null $name |
69
|
|
|
* |
70
|
|
|
* @return Connection |
71
|
|
|
* |
72
|
|
|
* @throws \InvalidArgumentException |
73
|
|
|
*/ |
74
|
2 |
|
public function getConnection($name = null): Connection |
75
|
|
|
{ |
76
|
2 |
|
$this->loadConnections(); |
77
|
|
|
|
78
|
2 |
|
$name = $name ?? $this->getDefaultConnectionName(); |
79
|
|
|
|
80
|
2 |
|
if (!isset($this->connections[$name])) { |
81
|
1 |
|
throw new \InvalidArgumentException(sprintf('Missing connection with name "%s".', $name)); |
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $this->connections[$name]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return Connection[] |
89
|
|
|
*/ |
90
|
1 |
|
public function getConnections(): array |
91
|
|
|
{ |
92
|
1 |
|
$this->loadConnections(); |
93
|
|
|
|
94
|
1 |
|
$connections = array(); |
95
|
1 |
|
foreach ($this->connections->keys() as $name) { |
96
|
1 |
|
$connections[$name] = $this->connections[$name]; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
return $connections; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return string[] |
104
|
|
|
*/ |
105
|
1 |
|
public function getConnectionNames(): array |
106
|
|
|
{ |
107
|
1 |
|
$this->loadConnections(); |
108
|
|
|
|
109
|
1 |
|
return $this->connections->keys(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
6 |
|
public function getDefaultManagerName(): string |
116
|
|
|
{ |
117
|
6 |
|
$this->loadManagers(); |
118
|
|
|
|
119
|
6 |
|
return $this->defaultManagerName; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string|null $name |
124
|
|
|
* |
125
|
|
|
* @return EntityManager|ObjectManager |
126
|
|
|
*/ |
127
|
6 |
|
public function getManager($name = null): ObjectManager |
128
|
|
|
{ |
129
|
6 |
|
$this->loadManagers(); |
130
|
|
|
|
131
|
6 |
|
$name = $name ?? $this->getDefaultManagerName(); |
132
|
|
|
|
133
|
6 |
|
if (!isset($this->originalManagers[$name])) { |
134
|
1 |
|
throw new \InvalidArgumentException(sprintf('Missing manager with name "%s".', $name)); |
135
|
|
|
} |
136
|
|
|
|
137
|
5 |
|
if (isset($this->resetedManagers[$name])) { |
138
|
1 |
|
return $this->resetedManagers[$name]; |
139
|
|
|
} |
140
|
|
|
|
141
|
4 |
|
return $this->originalManagers[$name]; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return EntityManager[]|ObjectManager[] |
146
|
|
|
*/ |
147
|
2 |
|
public function getManagers(): array |
148
|
|
|
{ |
149
|
2 |
|
$this->loadManagers(); |
150
|
|
|
|
151
|
2 |
|
$managers = array(); |
152
|
2 |
|
foreach ($this->originalManagers->keys() as $name) { |
153
|
2 |
|
if (isset($this->resetedManagers[$name])) { |
154
|
1 |
|
$manager = $this->resetedManagers[$name]; |
155
|
|
|
} else { |
156
|
1 |
|
$manager = $this->originalManagers[$name]; |
157
|
|
|
} |
158
|
|
|
|
159
|
2 |
|
$managers[$name] = $manager; |
160
|
|
|
} |
161
|
|
|
|
162
|
2 |
|
return $managers; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
3 |
|
public function getManagerNames(): array |
169
|
|
|
{ |
170
|
3 |
|
$this->loadManagers(); |
171
|
|
|
|
172
|
3 |
|
return $this->originalManagers->keys(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string|null $name |
177
|
|
|
* |
178
|
|
|
* @return EntityManager|ObjectManager |
179
|
|
|
*/ |
180
|
2 |
|
public function resetManager($name = null) |
181
|
|
|
{ |
182
|
2 |
|
$this->loadManagers(); |
183
|
|
|
|
184
|
2 |
|
$name = $name ?? $this->getDefaultManagerName(); |
185
|
|
|
|
186
|
2 |
|
if (!isset($this->originalManagers[$name])) { |
187
|
1 |
|
throw new \InvalidArgumentException(sprintf('Missing manager with name "%s".', $name)); |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
$originalManager = $this->originalManagers[$name]; |
191
|
|
|
|
192
|
1 |
|
$this->resetedManagers[$name] = EntityManager::create( |
193
|
1 |
|
$originalManager->getConnection(), |
194
|
1 |
|
$originalManager->getConfiguration(), |
195
|
1 |
|
$originalManager->getEventManager() |
196
|
|
|
); |
197
|
|
|
|
198
|
1 |
|
return $this->resetedManagers[$name]; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $alias |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
* |
206
|
|
|
* @throws ORMException |
207
|
|
|
*/ |
208
|
2 |
|
public function getAliasNamespace($alias): string |
209
|
|
|
{ |
210
|
2 |
|
foreach ($this->getManagerNames() as $name) { |
211
|
|
|
try { |
212
|
2 |
|
return $this->getManager($name)->getConfiguration()->getEntityNamespace($alias); |
|
|
|
|
213
|
1 |
|
} catch (ORMException $e) { |
214
|
|
|
// throw the exception only if no manager can solve it |
215
|
|
|
} |
216
|
|
|
} |
217
|
1 |
|
throw ORMException::unknownEntityNamespace($alias); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @param string $persistentObject |
222
|
|
|
* @param null $persistentManagerName |
223
|
|
|
* |
224
|
|
|
* @return EntityRepository|ObjectRepository |
225
|
|
|
*/ |
226
|
1 |
|
public function getRepository($persistentObject, $persistentManagerName = null): ObjectRepository |
227
|
|
|
{ |
228
|
1 |
|
return $this->getManager($persistentManagerName)->getRepository($persistentObject); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param string $class |
233
|
|
|
* |
234
|
|
|
* @return EntityManager|ObjectManager|null |
235
|
|
|
*/ |
236
|
|
|
public function getManagerForClass($class) |
237
|
|
|
{ |
238
|
|
|
$proxyClass = new \ReflectionClass($class); |
239
|
|
|
if ($proxyClass->implementsInterface(Proxy::class)) { |
240
|
|
|
$class = $proxyClass->getParentClass()->getName(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
foreach ($this->getManagerNames() as $name) { |
244
|
|
|
if (!$this->getManager($name)->getMetadataFactory()->isTransient($class)) { |
245
|
|
|
return $this->getManager($name); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
} |
249
|
|
|
|
250
|
5 |
|
private function loadConnections() |
251
|
|
|
{ |
252
|
5 |
|
if (null === $this->connections) { |
253
|
5 |
|
$this->connections = $this->container['doctrine.dbal.dbs']; |
254
|
5 |
|
$this->defaultConnectionName = $this->container['doctrine.dbal.dbs.default']; |
255
|
|
|
} |
256
|
5 |
|
} |
257
|
|
|
|
258
|
12 |
|
private function loadManagers() |
259
|
|
|
{ |
260
|
12 |
|
if (null === $this->originalManagers) { |
261
|
12 |
|
$this->originalManagers = $this->container['doctrine.orm.ems']; |
262
|
12 |
|
$this->defaultManagerName = $this->container['doctrine.orm.ems.default']; |
263
|
|
|
} |
264
|
12 |
|
} |
265
|
|
|
} |
266
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: