Registry.php 6.8KB

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