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

Open in your IDE?
  1. <?php
  2. namespace Doctrine\Persistence;
  3. use Doctrine\Deprecations\Deprecation;
  4. use InvalidArgumentException;
  5. use ReflectionClass;
  6. use function explode;
  7. use function sprintf;
  8. use function strpos;
  9. /**
  10.  * Abstract implementation of the ManagerRegistry contract.
  11.  */
  12. abstract class AbstractManagerRegistry implements ManagerRegistry
  13. {
  14.     /** @var string */
  15.     private $name;
  16.     /** @var string[] */
  17.     private $connections;
  18.     /** @var string[] */
  19.     private $managers;
  20.     /** @var string */
  21.     private $defaultConnection;
  22.     /** @var string */
  23.     private $defaultManager;
  24.     /**
  25.      * @var string
  26.      * @psalm-var class-string
  27.      */
  28.     private $proxyInterfaceName;
  29.     /**
  30.      * @param string   $name
  31.      * @param string[] $connections
  32.      * @param string[] $managers
  33.      * @param string   $defaultConnection
  34.      * @param string   $defaultManager
  35.      * @param string   $proxyInterfaceName
  36.      * @psalm-param class-string $proxyInterfaceName
  37.      */
  38.     public function __construct($name, array $connections, array $managers$defaultConnection$defaultManager$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($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($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($name null)
  80.     {
  81.         if ($name === null) {
  82.             $name $this->defaultConnection;
  83.         }
  84.         if (! isset($this->connections[$name])) {
  85.             throw new InvalidArgumentException(sprintf('Doctrine %s Connection named "%s" does not exist.'$this->name$name));
  86.         }
  87.         return $this->getService($this->connections[$name]);
  88.     }
  89.     /**
  90.      * {@inheritdoc}
  91.      */
  92.     public function getConnectionNames()
  93.     {
  94.         return $this->connections;
  95.     }
  96.     /**
  97.      * {@inheritdoc}
  98.      */
  99.     public function getConnections()
  100.     {
  101.         $connections = [];
  102.         foreach ($this->connections as $name => $id) {
  103.             $connections[$name] = $this->getService($id);
  104.         }
  105.         return $connections;
  106.     }
  107.     /**
  108.      * {@inheritdoc}
  109.      */
  110.     public function getDefaultConnectionName()
  111.     {
  112.         return $this->defaultConnection;
  113.     }
  114.     /**
  115.      * {@inheritdoc}
  116.      */
  117.     public function getDefaultManagerName()
  118.     {
  119.         return $this->defaultManager;
  120.     }
  121.     /**
  122.      * {@inheritdoc}
  123.      *
  124.      * @throws InvalidArgumentException
  125.      */
  126.     public function getManager($name null)
  127.     {
  128.         if ($name === null) {
  129.             $name $this->defaultManager;
  130.         }
  131.         if (! isset($this->managers[$name])) {
  132.             throw new InvalidArgumentException(sprintf('Doctrine %s Manager named "%s" does not exist.'$this->name$name));
  133.         }
  134.         return $this->getService($this->managers[$name]);
  135.     }
  136.     /**
  137.      * {@inheritdoc}
  138.      */
  139.     public function getManagerForClass($class)
  140.     {
  141.         $className $this->getRealClassName($class);
  142.         $proxyClass = new ReflectionClass($className);
  143.         if ($proxyClass->implementsInterface($this->proxyInterfaceName)) {
  144.             $parentClass $proxyClass->getParentClass();
  145.             if (! $parentClass) {
  146.                 return null;
  147.             }
  148.             $className $parentClass->getName();
  149.         }
  150.         foreach ($this->managers as $id) {
  151.             $manager $this->getService($id);
  152.             if (! $manager->getMetadataFactory()->isTransient($className)) {
  153.                 return $manager;
  154.             }
  155.         }
  156.         return null;
  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($persistentObject$persistentManagerName null)
  180.     {
  181.         return $this
  182.             ->selectManager($persistentObject$persistentManagerName)
  183.             ->getRepository($persistentObject);
  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.     /**
  202.      * @psalm-param class-string $persistentObjectName
  203.      */
  204.     private function selectManager(string $persistentObjectName, ?string $persistentManagerName null): ObjectManager
  205.     {
  206.         if ($persistentManagerName !== null) {
  207.             return $this->getManager($persistentManagerName);
  208.         }
  209.         return $this->getManagerForClass($persistentObjectName) ?? $this->getManager();
  210.     }
  211.     /**
  212.      * @psalm-return class-string
  213.      */
  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(':'$classNameOrAlias2);
  225.             /** @psalm-var class-string */
  226.             return $this->getAliasNamespace($namespaceAlias) . '\\' $simpleClassName;
  227.         }
  228.         /** @psalm-var class-string */
  229.         return $classNameOrAlias;
  230.     }
  231. }