ManagerRegistryEntityManager.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Configuration\EntityManager;
  4. use Doctrine\Migrations\Configuration\EntityManager\Exception\InvalidConfiguration;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. final class ManagerRegistryEntityManager implements EntityManagerLoader
  8. {
  9. /** @var ManagerRegistry */
  10. private $registry;
  11. /** @var string|null */
  12. private $defaultManagerName;
  13. public static function withSimpleDefault(ManagerRegistry $registry, ?string $managerName = null): self
  14. {
  15. $that = new self();
  16. $that->registry = $registry;
  17. $that->defaultManagerName = $managerName;
  18. return $that;
  19. }
  20. private function __construct()
  21. {
  22. }
  23. public function getEntityManager(?string $name = null): EntityManagerInterface
  24. {
  25. $managerName = $name ?? $this->defaultManagerName;
  26. $em = $this->registry->getManager($managerName);
  27. if (! $em instanceof EntityManagerInterface) {
  28. throw InvalidConfiguration::invalidManagerType($em);
  29. }
  30. return $em;
  31. }
  32. }