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

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