Registry.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Doctrine\ORM\ORMException;
  5. use ProxyManager\Proxy\LazyLoadingInterface;
  6. use Psr\Container\ContainerInterface;
  7. use Symfony\Bridge\Doctrine\ManagerRegistry;
  8. use Symfony\Contracts\Service\ResetInterface;
  9. use function array_keys;
  10. use function assert;
  11. /**
  12. * References all Doctrine connections and entity managers in a given Container.
  13. */
  14. class Registry extends ManagerRegistry implements ResetInterface
  15. {
  16. /**
  17. * @param string[] $connections
  18. * @param string[] $entityManagers
  19. */
  20. public function __construct(ContainerInterface $container, array $connections, array $entityManagers, string $defaultConnection, string $defaultEntityManager)
  21. {
  22. $this->container = $container;
  23. parent::__construct('ORM', $connections, $entityManagers, $defaultConnection, $defaultEntityManager, 'Doctrine\ORM\Proxy\Proxy');
  24. }
  25. /**
  26. * Resolves a registered namespace alias to the full namespace.
  27. *
  28. * This method looks for the alias in all registered entity managers.
  29. *
  30. * @see Configuration::getEntityNamespace
  31. *
  32. * @param string $alias The alias
  33. *
  34. * @return string The full namespace
  35. */
  36. public function getAliasNamespace($alias)
  37. {
  38. foreach (array_keys($this->getManagers()) as $name) {
  39. $objectManager = $this->getManager($name);
  40. if (! $objectManager instanceof EntityManagerInterface) {
  41. continue;
  42. }
  43. try {
  44. return $objectManager->getConfiguration()->getEntityNamespace($alias);
  45. } catch (ORMException $e) {
  46. }
  47. }
  48. throw ORMException::unknownEntityNamespace($alias);
  49. }
  50. public function reset(): void
  51. {
  52. foreach ($this->getManagerNames() as $managerName => $serviceId) {
  53. $this->resetOrClearManager($managerName, $serviceId);
  54. }
  55. }
  56. private function resetOrClearManager(string $managerName, string $serviceId): void
  57. {
  58. if (! $this->container->initialized($serviceId)) {
  59. return;
  60. }
  61. $manager = $this->container->get($serviceId);
  62. assert($manager instanceof EntityManagerInterface);
  63. if (! $manager instanceof LazyLoadingInterface || $manager->isOpen()) {
  64. $manager->clear();
  65. return;
  66. }
  67. $this->resetManager($managerName);
  68. }
  69. }