vendor/doctrine/persistence/src/Persistence/Mapping/Driver/MappingDriverChain.php line 73

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Persistence\Mapping\Driver;
  4. use Doctrine\Persistence\Mapping\ClassMetadata;
  5. use Doctrine\Persistence\Mapping\MappingException;
  6. use function array_keys;
  7. use function spl_object_hash;
  8. use function strpos;
  9. /**
  10.  * The DriverChain allows you to add multiple other mapping drivers for
  11.  * certain namespaces.
  12.  */
  13. class MappingDriverChain implements MappingDriver
  14. {
  15.     /**
  16.      * The default driver.
  17.      *
  18.      * @var MappingDriver|null
  19.      */
  20.     private $defaultDriver;
  21.     /** @var array<string, MappingDriver> */
  22.     private $drivers = [];
  23.     /**
  24.      * Gets the default driver.
  25.      *
  26.      * @return MappingDriver|null
  27.      */
  28.     public function getDefaultDriver()
  29.     {
  30.         return $this->defaultDriver;
  31.     }
  32.     /**
  33.      * Set the default driver.
  34.      *
  35.      * @return void
  36.      */
  37.     public function setDefaultDriver(MappingDriver $driver)
  38.     {
  39.         $this->defaultDriver $driver;
  40.     }
  41.     /**
  42.      * Adds a nested driver.
  43.      *
  44.      * @return void
  45.      */
  46.     public function addDriver(MappingDriver $nestedDriverstring $namespace)
  47.     {
  48.         $this->drivers[$namespace] = $nestedDriver;
  49.     }
  50.     /**
  51.      * Gets the array of nested drivers.
  52.      *
  53.      * @return array<string, MappingDriver> $drivers
  54.      */
  55.     public function getDrivers()
  56.     {
  57.         return $this->drivers;
  58.     }
  59.     /**
  60.      * {@inheritDoc}
  61.      */
  62.     public function loadMetadataForClass(string $classNameClassMetadata $metadata)
  63.     {
  64.         foreach ($this->drivers as $namespace => $driver) {
  65.             if (strpos($className$namespace) === 0) {
  66.                 $driver->loadMetadataForClass($className$metadata);
  67.                 return;
  68.             }
  69.         }
  70.         if ($this->defaultDriver !== null) {
  71.             $this->defaultDriver->loadMetadataForClass($className$metadata);
  72.             return;
  73.         }
  74.         throw MappingException::classNotFoundInNamespaces($classNamearray_keys($this->drivers));
  75.     }
  76.     /**
  77.      * {@inheritDoc}
  78.      */
  79.     public function getAllClassNames()
  80.     {
  81.         $classNames    = [];
  82.         $driverClasses = [];
  83.         foreach ($this->drivers as $namespace => $driver) {
  84.             $oid spl_object_hash($driver);
  85.             if (! isset($driverClasses[$oid])) {
  86.                 $driverClasses[$oid] = $driver->getAllClassNames();
  87.             }
  88.             foreach ($driverClasses[$oid] as $className) {
  89.                 if (strpos($className$namespace) !== 0) {
  90.                     continue;
  91.                 }
  92.                 $classNames[$className] = true;
  93.             }
  94.         }
  95.         if ($this->defaultDriver !== null) {
  96.             foreach ($this->defaultDriver->getAllClassNames() as $className) {
  97.                 $classNames[$className] = true;
  98.             }
  99.         }
  100.         return array_keys($classNames);
  101.     }
  102.     /**
  103.      * {@inheritDoc}
  104.      */
  105.     public function isTransient(string $className)
  106.     {
  107.         foreach ($this->drivers as $namespace => $driver) {
  108.             if (strpos($className$namespace) === 0) {
  109.                 return $driver->isTransient($className);
  110.             }
  111.         }
  112.         if ($this->defaultDriver !== null) {
  113.             return $this->defaultDriver->isTransient($className);
  114.         }
  115.         return true;
  116.     }
  117. }