vendor/doctrine/persistence/lib/Doctrine/Persistence/AbstractManagerRegistry.php line 144

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