vendor/doctrine/persistence/lib/Doctrine/Persistence/Mapping/Driver/MappingDriverChain.php line 97

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