vendor/doctrine/persistence/src/Persistence/AbstractManagerRegistry.php line 173

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Persistence;
  3. use Doctrine\Deprecations\Deprecation;
  4. use InvalidArgumentException;
  5. use ReflectionClass;
  6. use function class_exists;
  7. use function explode;
  8. use function sprintf;
  9. use function strpos;
  10. /**
  11. * Abstract implementation of the ManagerRegistry contract.
  12. */
  13. abstract class AbstractManagerRegistry implements ManagerRegistry
  14. {
  15. /** @var string */
  16. private $name;
  17. /** @var string[] */
  18. private $connections;
  19. /** @var string[] */
  20. private $managers;
  21. /** @var string */
  22. private $defaultConnection;
  23. /** @var string */
  24. private $defaultManager;
  25. /**
  26. * @var string
  27. * @psalm-var class-string
  28. */
  29. private $proxyInterfaceName;
  30. /**
  31. * @param string $name
  32. * @param string[] $connections
  33. * @param string[] $managers
  34. * @param string $defaultConnection
  35. * @param string $defaultManager
  36. * @param string $proxyInterfaceName
  37. * @psalm-param class-string $proxyInterfaceName
  38. */
  39. public function __construct($name, array $connections, array $managers, $defaultConnection, $defaultManager, $proxyInterfaceName)
  40. {
  41. $this->name = $name;
  42. $this->connections = $connections;
  43. $this->managers = $managers;
  44. $this->defaultConnection = $defaultConnection;
  45. $this->defaultManager = $defaultManager;
  46. $this->proxyInterfaceName = $proxyInterfaceName;
  47. }
  48. /**
  49. * Fetches/creates the given services.
  50. *
  51. * A service in this context is connection or a manager instance.
  52. *
  53. * @param string $name The name of the service.
  54. *
  55. * @return ObjectManager The instance of the given service.
  56. */
  57. abstract protected function getService($name);
  58. /**
  59. * Resets the given services.
  60. *
  61. * A service in this context is connection or a manager instance.
  62. *
  63. * @param string $name The name of the service.
  64. *
  65. * @return void
  66. */
  67. abstract protected function resetService($name);
  68. /**
  69. * Gets the name of the registry.
  70. *
  71. * @return string
  72. */
  73. public function getName()
  74. {
  75. return $this->name;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getConnection($name = null)
  81. {
  82. if ($name === null) {
  83. $name = $this->defaultConnection;
  84. }
  85. if (! isset($this->connections[$name])) {
  86. throw new InvalidArgumentException(sprintf('Doctrine %s Connection named "%s" does not exist.', $this->name, $name));
  87. }
  88. return $this->getService($this->connections[$name]);
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function getConnectionNames()
  94. {
  95. return $this->connections;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function getConnections()
  101. {
  102. $connections = [];
  103. foreach ($this->connections as $name => $id) {
  104. $connections[$name] = $this->getService($id);
  105. }
  106. return $connections;
  107. }
  108. /**
  109. * {@inheritdoc}
  110. */
  111. public function getDefaultConnectionName()
  112. {
  113. return $this->defaultConnection;
  114. }
  115. /**
  116. * {@inheritdoc}
  117. */
  118. public function getDefaultManagerName()
  119. {
  120. return $this->defaultManager;
  121. }
  122. /**
  123. * {@inheritdoc}
  124. *
  125. * @throws InvalidArgumentException
  126. */
  127. public function getManager($name = null)
  128. {
  129. if ($name === null) {
  130. $name = $this->defaultManager;
  131. }
  132. if (! isset($this->managers[$name])) {
  133. throw new InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
  134. }
  135. return $this->getService($this->managers[$name]);
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function getManagerForClass($class)
  141. {
  142. if (class_exists($class, false) && (new ReflectionClass($class))->isAnonymous()) {
  143. return null;
  144. }
  145. $className = $this->getRealClassName($class);
  146. $reflection = new ReflectionClass($className);
  147. if ($reflection->implementsInterface($this->proxyInterfaceName)) {
  148. $parentClass = $reflection->getParentClass();
  149. if (! $parentClass) {
  150. return null;
  151. }
  152. $className = $parentClass->getName();
  153. }
  154. foreach ($this->managers as $id) {
  155. $manager = $this->getService($id);
  156. if (! $manager->getMetadataFactory()->isTransient($className)) {
  157. return $manager;
  158. }
  159. }
  160. return null;
  161. }
  162. /**
  163. * {@inheritdoc}
  164. */
  165. public function getManagerNames()
  166. {
  167. return $this->managers;
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function getManagers()
  173. {
  174. $dms = [];
  175. foreach ($this->managers as $name => $id) {
  176. $dms[$name] = $this->getService($id);
  177. }
  178. return $dms;
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function getRepository($persistentObject, $persistentManagerName = null)
  184. {
  185. return $this
  186. ->selectManager($persistentObject, $persistentManagerName)
  187. ->getRepository($persistentObject);
  188. }
  189. /**
  190. * {@inheritdoc}
  191. */
  192. public function resetManager($name = null)
  193. {
  194. if ($name === null) {
  195. $name = $this->defaultManager;
  196. }
  197. if (! isset($this->managers[$name])) {
  198. throw new InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
  199. }
  200. // force the creation of a new document manager
  201. // if the current one is closed
  202. $this->resetService($this->managers[$name]);
  203. return $this->getManager($name);
  204. }
  205. /** @psalm-param class-string $persistentObjectName */
  206. private function selectManager(string $persistentObjectName, ?string $persistentManagerName = null): ObjectManager
  207. {
  208. if ($persistentManagerName !== null) {
  209. return $this->getManager($persistentManagerName);
  210. }
  211. return $this->getManagerForClass($persistentObjectName) ?? $this->getManager();
  212. }
  213. /** @psalm-return class-string */
  214. private function getRealClassName(string $classNameOrAlias): string
  215. {
  216. // Check for namespace alias
  217. if (strpos($classNameOrAlias, ':') !== false) {
  218. Deprecation::trigger(
  219. 'doctrine/persistence',
  220. 'https://github.com/doctrine/persistence/issues/204',
  221. 'Short namespace aliases such as "%s" are deprecated, use ::class constant instead.',
  222. $classNameOrAlias
  223. );
  224. [$namespaceAlias, $simpleClassName] = explode(':', $classNameOrAlias, 2);
  225. /** @psalm-var class-string */
  226. return $this->getAliasNamespace($namespaceAlias) . '\\' . $simpleClassName;
  227. }
  228. /** @psalm-var class-string */
  229. return $classNameOrAlias;
  230. }
  231. }