Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@0
|
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
Chris@0
|
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
Chris@0
|
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
Chris@0
|
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
Chris@0
|
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
Chris@0
|
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
Chris@0
|
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
Chris@0
|
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
Chris@0
|
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
Chris@0
|
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
Chris@0
|
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
Chris@0
|
14 *
|
Chris@0
|
15 * This software consists of voluntary contributions made by many individuals
|
Chris@0
|
16 * and is licensed under the MIT license. For more information, see
|
Chris@0
|
17 * <http://www.doctrine-project.org>.
|
Chris@0
|
18 */
|
Chris@0
|
19
|
Chris@0
|
20 namespace Doctrine\Common\Persistence;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * Abstract implementation of the ManagerRegistry contract.
|
Chris@0
|
24 *
|
Chris@0
|
25 * @link www.doctrine-project.org
|
Chris@0
|
26 * @since 2.2
|
Chris@0
|
27 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
28 * @author Benjamin Eberlei <kontakt@beberlei.de>
|
Chris@0
|
29 * @author Lukas Kahwe Smith <smith@pooteeweet.org>
|
Chris@0
|
30 */
|
Chris@0
|
31 abstract class AbstractManagerRegistry implements ManagerRegistry
|
Chris@0
|
32 {
|
Chris@0
|
33 /**
|
Chris@0
|
34 * @var string
|
Chris@0
|
35 */
|
Chris@0
|
36 private $name;
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * @var array
|
Chris@0
|
40 */
|
Chris@0
|
41 private $connections;
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * @var array
|
Chris@0
|
45 */
|
Chris@0
|
46 private $managers;
|
Chris@0
|
47
|
Chris@0
|
48 /**
|
Chris@0
|
49 * @var string
|
Chris@0
|
50 */
|
Chris@0
|
51 private $defaultConnection;
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * @var string
|
Chris@0
|
55 */
|
Chris@0
|
56 private $defaultManager;
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * @var string
|
Chris@0
|
60 */
|
Chris@0
|
61 private $proxyInterfaceName;
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * Constructor.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @param string $name
|
Chris@0
|
67 * @param array $connections
|
Chris@0
|
68 * @param array $managers
|
Chris@0
|
69 * @param string $defaultConnection
|
Chris@0
|
70 * @param string $defaultManager
|
Chris@0
|
71 * @param string $proxyInterfaceName
|
Chris@0
|
72 */
|
Chris@0
|
73 public function __construct($name, array $connections, array $managers, $defaultConnection, $defaultManager, $proxyInterfaceName)
|
Chris@0
|
74 {
|
Chris@0
|
75 $this->name = $name;
|
Chris@0
|
76 $this->connections = $connections;
|
Chris@0
|
77 $this->managers = $managers;
|
Chris@0
|
78 $this->defaultConnection = $defaultConnection;
|
Chris@0
|
79 $this->defaultManager = $defaultManager;
|
Chris@0
|
80 $this->proxyInterfaceName = $proxyInterfaceName;
|
Chris@0
|
81 }
|
Chris@0
|
82
|
Chris@0
|
83 /**
|
Chris@0
|
84 * Fetches/creates the given services.
|
Chris@0
|
85 *
|
Chris@0
|
86 * A service in this context is connection or a manager instance.
|
Chris@0
|
87 *
|
Chris@0
|
88 * @param string $name The name of the service.
|
Chris@0
|
89 *
|
Chris@0
|
90 * @return object The instance of the given service.
|
Chris@0
|
91 */
|
Chris@0
|
92 abstract protected function getService($name);
|
Chris@0
|
93
|
Chris@0
|
94 /**
|
Chris@0
|
95 * Resets the given services.
|
Chris@0
|
96 *
|
Chris@0
|
97 * A service in this context is connection or a manager instance.
|
Chris@0
|
98 *
|
Chris@0
|
99 * @param string $name The name of the service.
|
Chris@0
|
100 *
|
Chris@0
|
101 * @return void
|
Chris@0
|
102 */
|
Chris@0
|
103 abstract protected function resetService($name);
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * Gets the name of the registry.
|
Chris@0
|
107 *
|
Chris@0
|
108 * @return string
|
Chris@0
|
109 */
|
Chris@0
|
110 public function getName()
|
Chris@0
|
111 {
|
Chris@0
|
112 return $this->name;
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * {@inheritdoc}
|
Chris@0
|
117 */
|
Chris@0
|
118 public function getConnection($name = null)
|
Chris@0
|
119 {
|
Chris@0
|
120 if (null === $name) {
|
Chris@0
|
121 $name = $this->defaultConnection;
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 if (!isset($this->connections[$name])) {
|
Chris@0
|
125 throw new \InvalidArgumentException(sprintf('Doctrine %s Connection named "%s" does not exist.', $this->name, $name));
|
Chris@0
|
126 }
|
Chris@0
|
127
|
Chris@0
|
128 return $this->getService($this->connections[$name]);
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 /**
|
Chris@0
|
132 * {@inheritdoc}
|
Chris@0
|
133 */
|
Chris@0
|
134 public function getConnectionNames()
|
Chris@0
|
135 {
|
Chris@0
|
136 return $this->connections;
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 /**
|
Chris@0
|
140 * {@inheritdoc}
|
Chris@0
|
141 */
|
Chris@0
|
142 public function getConnections()
|
Chris@0
|
143 {
|
Chris@0
|
144 $connections = [];
|
Chris@0
|
145 foreach ($this->connections as $name => $id) {
|
Chris@0
|
146 $connections[$name] = $this->getService($id);
|
Chris@0
|
147 }
|
Chris@0
|
148
|
Chris@0
|
149 return $connections;
|
Chris@0
|
150 }
|
Chris@0
|
151
|
Chris@0
|
152 /**
|
Chris@0
|
153 * {@inheritdoc}
|
Chris@0
|
154 */
|
Chris@0
|
155 public function getDefaultConnectionName()
|
Chris@0
|
156 {
|
Chris@0
|
157 return $this->defaultConnection;
|
Chris@0
|
158 }
|
Chris@0
|
159
|
Chris@0
|
160 /**
|
Chris@0
|
161 * {@inheritdoc}
|
Chris@0
|
162 */
|
Chris@0
|
163 public function getDefaultManagerName()
|
Chris@0
|
164 {
|
Chris@0
|
165 return $this->defaultManager;
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 /**
|
Chris@0
|
169 * {@inheritdoc}
|
Chris@0
|
170 *
|
Chris@0
|
171 * @throws \InvalidArgumentException
|
Chris@0
|
172 */
|
Chris@0
|
173 public function getManager($name = null)
|
Chris@0
|
174 {
|
Chris@0
|
175 if (null === $name) {
|
Chris@0
|
176 $name = $this->defaultManager;
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 if (!isset($this->managers[$name])) {
|
Chris@0
|
180 throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 return $this->getService($this->managers[$name]);
|
Chris@0
|
184 }
|
Chris@0
|
185
|
Chris@0
|
186 /**
|
Chris@0
|
187 * {@inheritdoc}
|
Chris@0
|
188 */
|
Chris@0
|
189 public function getManagerForClass($class)
|
Chris@0
|
190 {
|
Chris@0
|
191 // Check for namespace alias
|
Chris@0
|
192 if (strpos($class, ':') !== false) {
|
Chris@0
|
193 list($namespaceAlias, $simpleClassName) = explode(':', $class, 2);
|
Chris@0
|
194 $class = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 $proxyClass = new \ReflectionClass($class);
|
Chris@0
|
198
|
Chris@0
|
199 if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
|
Chris@0
|
200 if (! $parentClass = $proxyClass->getParentClass()) {
|
Chris@0
|
201 return null;
|
Chris@0
|
202 }
|
Chris@0
|
203
|
Chris@0
|
204 $class = $parentClass->getName();
|
Chris@0
|
205 }
|
Chris@0
|
206
|
Chris@0
|
207 foreach ($this->managers as $id) {
|
Chris@0
|
208 $manager = $this->getService($id);
|
Chris@0
|
209
|
Chris@0
|
210 if (!$manager->getMetadataFactory()->isTransient($class)) {
|
Chris@0
|
211 return $manager;
|
Chris@0
|
212 }
|
Chris@0
|
213 }
|
Chris@0
|
214 }
|
Chris@0
|
215
|
Chris@0
|
216 /**
|
Chris@0
|
217 * {@inheritdoc}
|
Chris@0
|
218 */
|
Chris@0
|
219 public function getManagerNames()
|
Chris@0
|
220 {
|
Chris@0
|
221 return $this->managers;
|
Chris@0
|
222 }
|
Chris@0
|
223
|
Chris@0
|
224 /**
|
Chris@0
|
225 * {@inheritdoc}
|
Chris@0
|
226 */
|
Chris@0
|
227 public function getManagers()
|
Chris@0
|
228 {
|
Chris@0
|
229 $dms = [];
|
Chris@0
|
230 foreach ($this->managers as $name => $id) {
|
Chris@0
|
231 $dms[$name] = $this->getService($id);
|
Chris@0
|
232 }
|
Chris@0
|
233
|
Chris@0
|
234 return $dms;
|
Chris@0
|
235 }
|
Chris@0
|
236
|
Chris@0
|
237 /**
|
Chris@0
|
238 * {@inheritdoc}
|
Chris@0
|
239 */
|
Chris@0
|
240 public function getRepository($persistentObjectName, $persistentManagerName = null)
|
Chris@0
|
241 {
|
Chris@0
|
242 return $this->getManager($persistentManagerName)->getRepository($persistentObjectName);
|
Chris@0
|
243 }
|
Chris@0
|
244
|
Chris@0
|
245 /**
|
Chris@0
|
246 * {@inheritdoc}
|
Chris@0
|
247 */
|
Chris@0
|
248 public function resetManager($name = null)
|
Chris@0
|
249 {
|
Chris@0
|
250 if (null === $name) {
|
Chris@0
|
251 $name = $this->defaultManager;
|
Chris@0
|
252 }
|
Chris@0
|
253
|
Chris@0
|
254 if (!isset($this->managers[$name])) {
|
Chris@0
|
255 throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
|
Chris@0
|
256 }
|
Chris@0
|
257
|
Chris@0
|
258 // force the creation of a new document manager
|
Chris@0
|
259 // if the current one is closed
|
Chris@0
|
260 $this->resetService($this->managers[$name]);
|
Chris@0
|
261
|
Chris@0
|
262 return $this->getManager($name);
|
Chris@0
|
263 }
|
Chris@0
|
264 }
|