4 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 * This software consists of voluntary contributions made by many individuals
17 * and is licensed under the MIT license. For more information, see
18 * <http://www.doctrine-project.org>.
21 namespace Doctrine\Common\Persistence;
23 use Doctrine\Common\Persistence\ManagerRegistry;
26 * Abstract implementation of the ManagerRegistry contract.
28 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
29 * @link www.doctrine-project.org
31 * @author Fabien Potencier <fabien@symfony.com>
32 * @author Benjamin Eberlei <kontakt@beberlei.de>
33 * @author Lukas Kahwe Smith <smith@pooteeweet.org>
35 abstract class AbstractManagerRegistry implements ManagerRegistry
55 private $defaultConnection;
60 private $defaultManager;
65 private $proxyInterfaceName;
71 * @param array $connections
72 * @param array $managers
73 * @param string $defaultConnection
74 * @param string $defaultManager
75 * @param string $proxyInterfaceName
77 public function __construct($name, array $connections, array $managers, $defaultConnection, $defaultManager, $proxyInterfaceName)
80 $this->connections = $connections;
81 $this->managers = $managers;
82 $this->defaultConnection = $defaultConnection;
83 $this->defaultManager = $defaultManager;
84 $this->proxyInterfaceName = $proxyInterfaceName;
88 * Fetches/creates the given services
90 * A service in this context is connection or a manager instance
92 * @param string $name name of the service
93 * @return object instance of the given service
95 abstract protected function getService($name);
98 * Resets the given services
100 * A service in this context is connection or a manager instance
102 * @param string $name name of the service
105 abstract protected function resetService($name);
108 * Get the name of the registry
112 public function getName()
120 public function getConnection($name = null)
122 if (null === $name) {
123 $name = $this->defaultConnection;
126 if (!isset($this->connections[$name])) {
127 throw new \InvalidArgumentException(sprintf('Doctrine %s Connection named "%s" does not exist.', $this->name, $name));
130 return $this->getService($this->connections[$name]);
136 public function getConnectionNames()
138 return $this->connections;
144 public function getConnections()
146 $connections = array();
147 foreach ($this->connections as $name => $id) {
148 $connections[$name] = $this->getService($id);
157 public function getDefaultConnectionName()
159 return $this->defaultConnection;
165 public function getDefaultManagerName()
167 return $this->defaultManager;
173 * @throws \InvalidArgumentException
175 public function getManager($name = null)
177 if (null === $name) {
178 $name = $this->defaultManager;
181 if (!isset($this->managers[$name])) {
182 throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
185 return $this->getService($this->managers[$name]);
191 public function getManagerForClass($class)
193 // Check for namespace alias
194 if (strpos($class, ':') !== false) {
195 list($namespaceAlias, $simpleClassName) = explode(':', $class);
196 $class = $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
199 $proxyClass = new \ReflectionClass($class);
200 if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
201 $class = $proxyClass->getParentClass()->getName();
204 foreach ($this->managers as $id) {
205 $manager = $this->getService($id);
207 if (!$manager->getMetadataFactory()->isTransient($class)) {
216 public function getManagerNames()
218 return $this->managers;
224 public function getManagers()
227 foreach ($this->managers as $name => $id) {
228 $dms[$name] = $this->getService($id);
237 public function getRepository($persistentObjectName, $persistentManagerName = null)
239 return $this->getManager($persistentManagerName)->getRepository($persistentObjectName);
245 public function resetManager($name = null)
247 if (null === $name) {
248 $name = $this->defaultManager;
251 if (!isset($this->managers[$name])) {
252 throw new \InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
255 // force the creation of a new document manager
256 // if the current one is closed
257 $this->resetService($this->managers[$name]);