1<?php 2 3namespace Doctrine\Bundle\DoctrineBundle; 4 5use Doctrine\ORM\EntityManager; 6use Doctrine\ORM\EntityManagerInterface; 7use Doctrine\ORM\ORMException; 8use ProxyManager\Proxy\LazyLoadingInterface; 9use Psr\Container\ContainerInterface; 10use Symfony\Bridge\Doctrine\ManagerRegistry; 11use Symfony\Bridge\Doctrine\RegistryInterface; 12use Symfony\Contracts\Service\ResetInterface; 13 14/** 15 * References all Doctrine connections and entity managers in a given Container. 16 */ 17class Registry extends ManagerRegistry implements RegistryInterface, ResetInterface 18{ 19 /** 20 * @param string[] $connections 21 * @param string[] $entityManagers 22 * @param string $defaultConnection 23 * @param string $defaultEntityManager 24 */ 25 public function __construct(ContainerInterface $container, array $connections, array $entityManagers, $defaultConnection, $defaultEntityManager) 26 { 27 $this->container = $container; 28 29 parent::__construct('ORM', $connections, $entityManagers, $defaultConnection, $defaultEntityManager, 'Doctrine\ORM\Proxy\Proxy'); 30 } 31 32 /** 33 * Gets the default entity manager name. 34 * 35 * @deprecated 36 * 37 * @return string The default entity manager name 38 */ 39 public function getDefaultEntityManagerName() 40 { 41 @trigger_error('getDefaultEntityManagerName is deprecated since Symfony 2.1. Use getDefaultManagerName instead', E_USER_DEPRECATED); 42 43 return $this->getDefaultManagerName(); 44 } 45 46 /** 47 * Gets a named entity manager. 48 * 49 * @deprecated 50 * 51 * @param string $name The entity manager name (null for the default one) 52 * 53 * @return EntityManager 54 */ 55 public function getEntityManager($name = null) 56 { 57 @trigger_error('getEntityManager is deprecated since Symfony 2.1. Use getManager instead', E_USER_DEPRECATED); 58 59 return $this->getManager($name); 60 } 61 62 /** 63 * Gets an array of all registered entity managers 64 * 65 * @deprecated 66 * 67 * @return EntityManager[] an array of all EntityManager instances 68 */ 69 public function getEntityManagers() 70 { 71 @trigger_error('getEntityManagers is deprecated since Symfony 2.1. Use getManagers instead', E_USER_DEPRECATED); 72 73 return $this->getManagers(); 74 } 75 76 /** 77 * Resets a named entity manager. 78 * 79 * This method is useful when an entity manager has been closed 80 * because of a rollbacked transaction AND when you think that 81 * it makes sense to get a new one to replace the closed one. 82 * 83 * Be warned that you will get a brand new entity manager as 84 * the existing one is not usable anymore. This means that any 85 * other object with a dependency on this entity manager will 86 * hold an obsolete reference. You can inject the registry instead 87 * to avoid this problem. 88 * 89 * @deprecated 90 * 91 * @param string $name The entity manager name (null for the default one) 92 */ 93 public function resetEntityManager($name = null) : EntityManager 94 { 95 @trigger_error('resetEntityManager is deprecated since Symfony 2.1. Use resetManager instead', E_USER_DEPRECATED); 96 97 return $this->resetManager($name); 98 } 99 100 /** 101 * Resolves a registered namespace alias to the full namespace. 102 * 103 * This method looks for the alias in all registered entity managers. 104 * 105 * @deprecated 106 * 107 * @param string $alias The alias 108 * 109 * @return string The full namespace 110 */ 111 public function getEntityNamespace($alias) 112 { 113 @trigger_error('getEntityNamespace is deprecated since Symfony 2.1. Use getAliasNamespace instead', E_USER_DEPRECATED); 114 115 return $this->getAliasNamespace($alias); 116 } 117 118 /** 119 * Resolves a registered namespace alias to the full namespace. 120 * 121 * This method looks for the alias in all registered entity managers. 122 * 123 * @see Configuration::getEntityNamespace 124 * 125 * @param string $alias The alias 126 * 127 * @return string The full namespace 128 */ 129 public function getAliasNamespace($alias) 130 { 131 foreach (array_keys($this->getManagers()) as $name) { 132 try { 133 return $this->getManager($name)->getConfiguration()->getEntityNamespace($alias); 134 } catch (ORMException $e) { 135 } 136 } 137 138 throw ORMException::unknownEntityNamespace($alias); 139 } 140 141 /** 142 * Gets all connection names. 143 * 144 * @deprecated 145 * 146 * @return string[] An array of connection names 147 */ 148 public function getEntityManagerNames() 149 { 150 @trigger_error('getEntityManagerNames is deprecated since Symfony 2.1. Use getManagerNames instead', E_USER_DEPRECATED); 151 152 return $this->getManagerNames(); 153 } 154 155 /** 156 * Gets the entity manager associated with a given class. 157 * 158 * @deprecated 159 * 160 * @param string $class A Doctrine Entity class name 161 * 162 * @return EntityManager|null 163 */ 164 public function getEntityManagerForClass($class) 165 { 166 @trigger_error('getEntityManagerForClass is deprecated since Symfony 2.1. Use getManagerForClass instead', E_USER_DEPRECATED); 167 168 return $this->getManagerForClass($class); 169 } 170 171 public function reset() : void 172 { 173 foreach ($this->getManagerNames() as $managerName => $serviceId) { 174 $this->resetOrClearManager($managerName, $serviceId); 175 } 176 } 177 178 private function resetOrClearManager(string $managerName, string $serviceId) : void 179 { 180 if (! $this->container->initialized($serviceId)) { 181 return; 182 } 183 184 $manager = $this->container->get($serviceId); 185 186 assert($manager instanceof EntityManagerInterface); 187 188 if (! $manager instanceof LazyLoadingInterface || $manager->isOpen()) { 189 $manager->clear(); 190 191 return; 192 } 193 194 $this->resetManager($managerName); 195 } 196} 197