Registry.php 6.8 KB

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