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