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