composer package updates
[openemr.git] / vendor / doctrine / common / lib / Doctrine / Common / Persistence / AbstractManagerRegistry.php
blob6da5422366fffa65403859c49af6470debd99dbb
1 <?php
2 /*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common\Persistence;
22 /**
23 * Abstract implementation of the ManagerRegistry contract.
25 * @link www.doctrine-project.org
26 * @since 2.2
27 * @author Fabien Potencier <fabien@symfony.com>
28 * @author Benjamin Eberlei <kontakt@beberlei.de>
29 * @author Lukas Kahwe Smith <smith@pooteeweet.org>
31 abstract class AbstractManagerRegistry implements ManagerRegistry
33 /**
34 * @var string
36 private $name;
38 /**
39 * @var array
41 private $connections;
43 /**
44 * @var array
46 private $managers;
48 /**
49 * @var string
51 private $defaultConnection;
53 /**
54 * @var string
56 private $defaultManager;
58 /**
59 * @var string
61 private $proxyInterfaceName;
63 /**
64 * Constructor.
66 * @param string $name
67 * @param array $connections
68 * @param array $managers
69 * @param string $defaultConnection
70 * @param string $defaultManager
71 * @param string $proxyInterfaceName
73 public function __construct($name, array $connections, array $managers, $defaultConnection, $defaultManager, $proxyInterfaceName)
75 $this->name = $name;
76 $this->connections = $connections;
77 $this->managers = $managers;
78 $this->defaultConnection = $defaultConnection;
79 $this->defaultManager = $defaultManager;
80 $this->proxyInterfaceName = $proxyInterfaceName;
83 /**
84 * Fetches/creates the given services.
86 * A service in this context is connection or a manager instance.
88 * @param string $name The name of the service.
90 * @return object The instance of the given service.
92 abstract protected function getService($name);
94 /**
95 * Resets the given services.
97 * A service in this context is connection or a manager instance.
99 * @param string $name The name of the service.
101 * @return void
103 abstract protected function resetService($name);
106 * Gets the name of the registry.
108 * @return string
110 public function getName()
112 return $this->name;
116 * {@inheritdoc}
118 public function getConnection($name = null)
120 if (null === $name) {
121 $name = $this->defaultConnection;
124 if (!isset($this->connections[$name])) {
125 throw new \InvalidArgumentException(sprintf('Doctrine %s Connection named "%s" does not exist.', $this->name, $name));
128 return $this->getService($this->connections[$name]);
132 * {@inheritdoc}
134 public function getConnectionNames()
136 return $this->connections;
140 * {@inheritdoc}
142 public function getConnections()
144 $connections = [];
145 foreach ($this->connections as $name => $id) {
146 $connections[$name] = $this->getService($id);
149 return $connections;
153 * {@inheritdoc}
155 public function getDefaultConnectionName()
157 return $this->defaultConnection;
161 * {@inheritdoc}
163 public function getDefaultManagerName()
165 return $this->defaultManager;
169 * {@inheritdoc}
171 * @throws \InvalidArgumentException
173 public function getManager($name = null)
175 if (null === $name) {
176 $name = $this->defaultManager;
179 if (!isset($this->managers[$name])) {
180 throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
183 return $this->getService($this->managers[$name]);
187 * {@inheritdoc}
189 public function getManagerForClass($class)
191 // Check for namespace alias
192 if (strpos($class, ':') !== false) {
193 list($namespaceAlias, $simpleClassName) = explode(':', $class, 2);
194 $class = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
197 $proxyClass = new \ReflectionClass($class);
199 if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
200 if (! $parentClass = $proxyClass->getParentClass()) {
201 return null;
204 $class = $parentClass->getName();
207 foreach ($this->managers as $id) {
208 $manager = $this->getService($id);
210 if (!$manager->getMetadataFactory()->isTransient($class)) {
211 return $manager;
217 * {@inheritdoc}
219 public function getManagerNames()
221 return $this->managers;
225 * {@inheritdoc}
227 public function getManagers()
229 $dms = [];
230 foreach ($this->managers as $name => $id) {
231 $dms[$name] = $this->getService($id);
234 return $dms;
238 * {@inheritdoc}
240 public function getRepository($persistentObjectName, $persistentManagerName = null)
242 return $this->getManager($persistentManagerName)->getRepository($persistentObjectName);
246 * {@inheritdoc}
248 public function resetManager($name = null)
250 if (null === $name) {
251 $name = $this->defaultManager;
254 if (!isset($this->managers[$name])) {
255 throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
258 // force the creation of a new document manager
259 // if the current one is closed
260 $this->resetService($this->managers[$name]);
262 return $this->getManager($name);