vendor/symfony/src/Symfony/Bundle/DoctrineBundle/Registry.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\DoctrineBundle;
       
    13 
       
    14 use Symfony\Component\DependencyInjection\ContainerInterface;
       
    15 use Symfony\Bridge\Doctrine\RegistryInterface;
       
    16 use Doctrine\DBAL\Connection;
       
    17 use Doctrine\ORM\Configuration;
       
    18 use Doctrine\ORM\ORMException;
       
    19 
       
    20 /**
       
    21  * References all Doctrine connections and entity managers in a given Container.
       
    22  *
       
    23  * @author Fabien Potencier <fabien@symfony.com>
       
    24  */
       
    25 class Registry implements RegistryInterface
       
    26 {
       
    27     private $container;
       
    28     private $connections;
       
    29     private $entityManagers;
       
    30     private $defaultConnection;
       
    31     private $defaultEntityManager;
       
    32 
       
    33     public function __construct(ContainerInterface $container, array $connections, array $entityManagers, $defaultConnection, $defaultEntityManager)
       
    34     {
       
    35         $this->container = $container;
       
    36         $this->connections = $connections;
       
    37         $this->entityManagers = $entityManagers;
       
    38         $this->defaultConnection = $defaultConnection;
       
    39         $this->defaultEntityManager = $defaultEntityManager;
       
    40     }
       
    41 
       
    42     /**
       
    43      * Gets the default connection name.
       
    44      *
       
    45      * @return string The default connection name
       
    46      */
       
    47     public function getDefaultConnectionName()
       
    48     {
       
    49         return $this->defaultConnection;
       
    50     }
       
    51 
       
    52     /**
       
    53      * Gets the named connection.
       
    54      *
       
    55      * @param string $name The connection name (null for the default one)
       
    56      *
       
    57      * @return Connection
       
    58      */
       
    59     public function getConnection($name = null)
       
    60     {
       
    61         if (null === $name) {
       
    62             $name = $this->defaultConnection;
       
    63         }
       
    64 
       
    65         if (!isset($this->connections[$name])) {
       
    66             throw new \InvalidArgumentException(sprintf('Doctrine Connection named "%s" does not exist.', $name));
       
    67         }
       
    68 
       
    69         return $this->container->get($this->connections[$name]);
       
    70     }
       
    71 
       
    72     /**
       
    73      * Gets an array of all registered connections
       
    74      *
       
    75      * @return array An array of Connection instances
       
    76      */
       
    77     public function getConnections()
       
    78     {
       
    79         $connections = array();
       
    80         foreach ($this->connections as $name => $id) {
       
    81             $connections[$name] = $this->container->get($id);
       
    82         }
       
    83 
       
    84         return $connections;
       
    85     }
       
    86 
       
    87     /**
       
    88      * Gets all connection names.
       
    89      *
       
    90      * @return array An array of connection names
       
    91      */
       
    92     public function getConnectionNames()
       
    93     {
       
    94         return $this->connections;
       
    95     }
       
    96 
       
    97     /**
       
    98      * Gets the default entity manager name.
       
    99      *
       
   100      * @return string The default entity manager name
       
   101      */
       
   102     public function getDefaultEntityManagerName()
       
   103     {
       
   104         return $this->defaultEntityManager;
       
   105     }
       
   106 
       
   107     /**
       
   108      * Gets a named entity manager.
       
   109      *
       
   110      * @param string $name The entity manager name (null for the default one)
       
   111      *
       
   112      * @return EntityManager
       
   113      */
       
   114     public function getEntityManager($name = null)
       
   115     {
       
   116         if (null === $name) {
       
   117             $name = $this->defaultEntityManager;
       
   118         }
       
   119 
       
   120         if (!isset($this->entityManagers[$name])) {
       
   121             throw new \InvalidArgumentException(sprintf('Doctrine EntityManager named "%s" does not exist.', $name));
       
   122         }
       
   123 
       
   124         return $this->container->get($this->entityManagers[$name]);
       
   125     }
       
   126 
       
   127     /**
       
   128      * Gets an array of all registered entity managers
       
   129      *
       
   130      * @return array An array of EntityManager instances
       
   131      */
       
   132     public function getEntityManagers()
       
   133     {
       
   134         $ems = array();
       
   135         foreach ($this->entityManagers as $name => $id) {
       
   136             $ems[$name] = $this->container->get($id);
       
   137         }
       
   138 
       
   139         return $ems;
       
   140     }
       
   141 
       
   142     /**
       
   143      * Resets a named entity manager.
       
   144      *
       
   145      * This method is useful when an entity manager has been closed
       
   146      * because of a rollbacked transaction AND when you think that
       
   147      * it makes sense to get a new one to replace the closed one.
       
   148      *
       
   149      * Be warned that you will get a brand new entity manager as
       
   150      * the existing one is not useable anymore. This means that any
       
   151      * other object with a dependency on this entity manager will
       
   152      * hold an obsolete reference. You can inject the registry instead
       
   153      * to avoid this problem.
       
   154      *
       
   155      * @param string $name The entity manager name (null for the default one)
       
   156      *
       
   157      * @return EntityManager
       
   158      */
       
   159     public function resetEntityManager($name = null)
       
   160     {
       
   161         if (null === $name) {
       
   162             $name = $this->defaultEntityManager;
       
   163         }
       
   164 
       
   165         if (!isset($this->entityManagers[$name])) {
       
   166             throw new \InvalidArgumentException(sprintf('Doctrine EntityManager named "%s" does not exist.', $name));
       
   167         }
       
   168 
       
   169         // force the creation of a new entity manager
       
   170         // if the current one is closed
       
   171         $this->container->set($this->entityManagers[$name], null);
       
   172     }
       
   173 
       
   174     /**
       
   175      * Resolves a registered namespace alias to the full namespace.
       
   176      *
       
   177      * This method looks for the alias in all registered entity managers.
       
   178      *
       
   179      * @param string $alias The alias
       
   180      *
       
   181      * @return string The full namespace
       
   182      *
       
   183      * @see Configuration::getEntityNamespace
       
   184      */
       
   185     public function getEntityNamespace($alias)
       
   186     {
       
   187         foreach (array_keys($this->entityManagers) as $name) {
       
   188             try {
       
   189                 return $this->getEntityManager($name)->getConfiguration()->getEntityNamespace($alias);
       
   190             } catch (ORMException $e) {
       
   191             }
       
   192         }
       
   193 
       
   194         throw ORMException::unknownEntityNamespace($alias);
       
   195     }
       
   196 
       
   197     /**
       
   198      * Gets all connection names.
       
   199      *
       
   200      * @return array An array of connection names
       
   201      */
       
   202     public function getEntityManagerNames()
       
   203     {
       
   204         return $this->entityManagers;
       
   205     }
       
   206 
       
   207     /**
       
   208      * Gets the EntityRepository for an entity.
       
   209      *
       
   210      * @param string $entityName        The name of the entity.
       
   211      * @param string $entityManagerNAme The entity manager name (null for the default one)
       
   212      *
       
   213      * @return Doctrine\ORM\EntityRepository
       
   214      */
       
   215     public function getRepository($entityName, $entityManagerName = null)
       
   216     {
       
   217         return $this->getEntityManager($entityManagerName)->getRepository($entityName);
       
   218     }
       
   219 
       
   220     /**
       
   221      * Gets the entity manager associated with a given class.
       
   222      *
       
   223      * @param string $class A Doctrine Entity class name
       
   224      *
       
   225      * @return EntityManager|null
       
   226      */
       
   227     public function getEntityManagerForClass($class)
       
   228     {
       
   229         $proxyClass = new \ReflectionClass($class);
       
   230         if ($proxyClass->implementsInterface('Doctrine\ORM\Proxy\Proxy')) {
       
   231             $class = $proxyClass->getParentClass()->getName();
       
   232         }
       
   233 
       
   234         foreach ($this->entityManagers as $id) {
       
   235             $em = $this->container->get($id);
       
   236 
       
   237             if (!$em->getConfiguration()->getMetadataDriverImpl()->isTransient($class)) {
       
   238                 return $em;
       
   239             }
       
   240         }
       
   241     }
       
   242 }