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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Persistence;
  4. use InvalidArgumentException;
  5. use ReflectionClass;
  6. use function assert;
  7. use function sprintf;
  8. /**
  9. * Abstract implementation of the ManagerRegistry contract.
  10. */
  11. abstract class AbstractManagerRegistry implements ManagerRegistry
  12. {
  13. /** @var string */
  14. private $name;
  15. /** @var array<string, string> */
  16. private $connections;
  17. /** @var array<string, string> */
  18. private $managers;
  19. /** @var string */
  20. private $defaultConnection;
  21. /** @var string */
  22. private $defaultManager;
  23. /**
  24. * @var string
  25. * @phpstan-var class-string
  26. */
  27. private $proxyInterfaceName;
  28. /**
  29. * @param array<string, string> $connections
  30. * @param array<string, string> $managers
  31. * @phpstan-param class-string $proxyInterfaceName
  32. */
  33. public function __construct(
  34. string $name,
  35. array $connections,
  36. array $managers,
  37. string $defaultConnection,
  38. string $defaultManager,
  39. string $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 object The instance of the given service.
  56. */
  57. abstract protected function getService(string $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(string $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(?string $name = null)
  81. {
  82. if ($name === null) {
  83. $name = $this->defaultConnection;
  84. }
  85. if (! isset($this->connections[$name])) {
  86. throw new InvalidArgumentException(
  87. sprintf('Doctrine %s Connection named "%s" does not exist.', $this->name, $name)
  88. );
  89. }
  90. return $this->getService($this->connections[$name]);
  91. }
  92. /**
  93. * {@inheritDoc}
  94. */
  95. public function getConnectionNames()
  96. {
  97. return $this->connections;
  98. }
  99. /**
  100. * {@inheritDoc}
  101. */
  102. public function getConnections()
  103. {
  104. $connections = [];
  105. foreach ($this->connections as $name => $id) {
  106. $connections[$name] = $this->getService($id);
  107. }
  108. return $connections;
  109. }
  110. /**
  111. * {@inheritDoc}
  112. */
  113. public function getDefaultConnectionName()
  114. {
  115. return $this->defaultConnection;
  116. }
  117. /**
  118. * {@inheritDoc}
  119. */
  120. public function getDefaultManagerName()
  121. {
  122. return $this->defaultManager;
  123. }
  124. /**
  125. * {@inheritDoc}
  126. *
  127. * @throws InvalidArgumentException
  128. */
  129. public function getManager(?string $name = null)
  130. {
  131. if ($name === null) {
  132. $name = $this->defaultManager;
  133. }
  134. if (! isset($this->managers[$name])) {
  135. throw new InvalidArgumentException(
  136. sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name)
  137. );
  138. }
  139. $service = $this->getService($this->managers[$name]);
  140. assert($service instanceof ObjectManager);
  141. return $service;
  142. }
  143. /**
  144. * {@inheritDoc}
  145. */
  146. public function getManagerForClass(string $class)
  147. {
  148. $proxyClass = new ReflectionClass($class);
  149. if ($proxyClass->isAnonymous()) {
  150. return null;
  151. }
  152. if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
  153. $parentClass = $proxyClass->getParentClass();
  154. if ($parentClass === false) {
  155. return null;
  156. }
  157. $class = $parentClass->getName();
  158. }
  159. foreach ($this->managers as $id) {
  160. $manager = $this->getService($id);
  161. assert($manager instanceof ObjectManager);
  162. if (! $manager->getMetadataFactory()->isTransient($class)) {
  163. return $manager;
  164. }
  165. }
  166. return null;
  167. }
  168. /**
  169. * {@inheritDoc}
  170. */
  171. public function getManagerNames()
  172. {
  173. return $this->managers;
  174. }
  175. /**
  176. * {@inheritDoc}
  177. */
  178. public function getManagers()
  179. {
  180. $managers = [];
  181. foreach ($this->managers as $name => $id) {
  182. $manager = $this->getService($id);
  183. assert($manager instanceof ObjectManager);
  184. $managers[$name] = $manager;
  185. }
  186. return $managers;
  187. }
  188. /**
  189. * {@inheritDoc}
  190. */
  191. public function getRepository(
  192. string $persistentObject,
  193. ?string $persistentManagerName = null
  194. ) {
  195. return $this
  196. ->selectManager($persistentObject, $persistentManagerName)
  197. ->getRepository($persistentObject);
  198. }
  199. /**
  200. * {@inheritDoc}
  201. */
  202. public function resetManager(?string $name = null)
  203. {
  204. if ($name === null) {
  205. $name = $this->defaultManager;
  206. }
  207. if (! isset($this->managers[$name])) {
  208. throw new InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.', $this->name, $name));
  209. }
  210. // force the creation of a new document manager
  211. // if the current one is closed
  212. $this->resetService($this->managers[$name]);
  213. return $this->getManager($name);
  214. }
  215. /** @phpstan-param class-string $persistentObject */
  216. private function selectManager(
  217. string $persistentObject,
  218. ?string $persistentManagerName = null
  219. ): ObjectManager {
  220. if ($persistentManagerName !== null) {
  221. return $this->getManager($persistentManagerName);
  222. }
  223. return $this->getManagerForClass($persistentObject) ?? $this->getManager();
  224. }
  225. }