Registry.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /*
  3. * This file is part of the Doctrine Bundle
  4. *
  5. * The code was originally distributed inside the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien@symfony.com>
  8. * (c) Doctrine Project, Benjamin Eberlei <kontakt@beberlei.de>
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. namespace Doctrine\Bundle\DoctrineBundle;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. use Symfony\Bridge\Doctrine\RegistryInterface;
  16. use Symfony\Bridge\Doctrine\ManagerRegistry;
  17. use Doctrine\ORM\ORMException;
  18. use Doctrine\ORM\EntityManager;
  19. /**
  20. * References all Doctrine connections and entity managers in a given Container.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. */
  24. class Registry extends ManagerRegistry implements RegistryInterface
  25. {
  26. /**
  27. * Construct.
  28. *
  29. * @param ContainerInterface $container
  30. * @param array $connections
  31. * @param array $entityManagers
  32. * @param string $defaultConnection
  33. * @param string $defaultEntityManager
  34. */
  35. public function __construct(ContainerInterface $container, array $connections, array $entityManagers, $defaultConnection, $defaultEntityManager)
  36. {
  37. $this->setContainer($container);
  38. parent::__construct('ORM', $connections, $entityManagers, $defaultConnection, $defaultEntityManager, 'Doctrine\ORM\Proxy\Proxy');
  39. }
  40. /**
  41. * Gets the default entity manager name.
  42. *
  43. * @return string The default entity manager name
  44. *
  45. * @deprecated
  46. */
  47. public function getDefaultEntityManagerName()
  48. {
  49. return $this->getDefaultManagerName();
  50. }
  51. /**
  52. * Gets a named entity manager.
  53. *
  54. * @param string $name The entity manager name (null for the default one)
  55. *
  56. * @return EntityManager
  57. *
  58. * @deprecated
  59. */
  60. public function getEntityManager($name = null)
  61. {
  62. return $this->getManager($name);
  63. }
  64. /**
  65. * Gets an array of all registered entity managers
  66. *
  67. * @return EntityManager[] an array of all EntityManager instances
  68. *
  69. * @deprecated
  70. */
  71. public function getEntityManagers()
  72. {
  73. return $this->getManagers();
  74. }
  75. /**
  76. * Resets a named entity manager.
  77. *
  78. * This method is useful when an entity manager has been closed
  79. * because of a rollbacked transaction AND when you think that
  80. * it makes sense to get a new one to replace the closed one.
  81. *
  82. * Be warned that you will get a brand new entity manager as
  83. * the existing one is not useable anymore. This means that any
  84. * other object with a dependency on this entity manager will
  85. * hold an obsolete reference. You can inject the registry instead
  86. * to avoid this problem.
  87. *
  88. * @param string $name The entity manager name (null for the default one)
  89. *
  90. * @return EntityManager
  91. */
  92. public function resetEntityManager($name = null)
  93. {
  94. $this->resetManager($name);
  95. }
  96. /**
  97. * Resolves a registered namespace alias to the full namespace.
  98. *
  99. * This method looks for the alias in all registered entity managers.
  100. *
  101. * @param string $alias The alias
  102. *
  103. * @return string The full namespace
  104. */
  105. public function getEntityNamespace($alias)
  106. {
  107. return $this->getAliasNamespace($alias);
  108. }
  109. /**
  110. * Resolves a registered namespace alias to the full namespace.
  111. *
  112. * This method looks for the alias in all registered entity managers.
  113. *
  114. * @param string $alias The alias
  115. *
  116. * @return string The full namespace
  117. *
  118. * @see Configuration::getEntityNamespace
  119. */
  120. public function getAliasNamespace($alias)
  121. {
  122. foreach (array_keys($this->getManagers()) as $name) {
  123. try {
  124. return $this->getManager($name)->getConfiguration()->getEntityNamespace($alias);
  125. } catch (ORMException $e) {
  126. }
  127. }
  128. throw ORMException::unknownEntityNamespace($alias);
  129. }
  130. /**
  131. * Gets all connection names.
  132. *
  133. * @return array An array of connection names
  134. */
  135. public function getEntityManagerNames()
  136. {
  137. return $this->getManagerNames();
  138. }
  139. /**
  140. * Gets the entity manager associated with a given class.
  141. *
  142. * @param string $class A Doctrine Entity class name
  143. *
  144. * @return EntityManager|null
  145. */
  146. public function getEntityManagerForClass($class)
  147. {
  148. return $this->getManagerForClass($class);
  149. }
  150. }