vendor/pimcore/pimcore/lib/Loader/ImplementationLoader/ImplementationLoader.php line 110

Open in your IDE?
  1. <?php
  2. declare(strict_types 1);
  3. /**
  4.  * Pimcore
  5.  *
  6.  * This source file is available under two different licenses:
  7.  * - GNU General Public License version 3 (GPLv3)
  8.  * - Pimcore Commercial License (PCL)
  9.  * Full copyright and license information is available in
  10.  * LICENSE.md which is distributed with this source code.
  11.  *
  12.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  13.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  14.  */
  15. namespace Pimcore\Loader\ImplementationLoader;
  16. use Pimcore\Loader\ImplementationLoader\Exception\UnsupportedException;
  17. /**
  18.  * Core implementation loader delegating to a list of registered loaders
  19.  *
  20.  * @internal
  21.  */
  22. class ImplementationLoader implements LoaderInterfaceClassNameLoaderInterface
  23. {
  24.     /**
  25.      * @var LoaderInterface[]
  26.      */
  27.     protected $loaders;
  28.     /**
  29.      * @var array
  30.      */
  31.     private $loaderCache = [];
  32.     /**
  33.      * @param LoaderInterface[] $loaders
  34.      */
  35.     public function __construct(array $loaders = [])
  36.     {
  37.         $this->setLoaders($loaders);
  38.     }
  39.     /**
  40.      * @param LoaderInterface[] $loaders
  41.      */
  42.     private function setLoaders(array $loaders)
  43.     {
  44.         $this->loaders = [];
  45.         $this->loaderCache = [];
  46.         foreach ($loaders as $loader) {
  47.             $this->addLoader($loader);
  48.         }
  49.     }
  50.     /**
  51.      * @param LoaderInterface $loader
  52.      */
  53.     public function addLoader(LoaderInterface $loader)
  54.     {
  55.         $this->loaders[] = $loader;
  56.     }
  57.     /**
  58.      * @param string $name
  59.      *
  60.      * @return LoaderInterface|null
  61.      */
  62.     private function getLoader(string $name)
  63.     {
  64.         // loader cache contains index of loader previously found for given name
  65.         if (isset($this->loaderCache[$name])) {
  66.             return $this->loaders[$this->loaderCache[$name]];
  67.         }
  68.         /** @var LoaderInterface $loader */
  69.         foreach (array_reverse($this->loaderstrue) as $idx => $loader) {
  70.             if ($loader->supports($name)) {
  71.                 $this->loaderCache[$name] = $idx;
  72.                 return $loader;
  73.             }
  74.         }
  75.         return null;
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function supports(string $name): bool
  81.     {
  82.         return null !== $this->getLoader($name);
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public function build(string $name, array $params = [])
  88.     {
  89.         $loader $this->getLoader($name);
  90.         if (null === $loader) {
  91.             throw new UnsupportedException(sprintf('Loader for "%s" was not found'$name));
  92.         }
  93.         return $loader->build($name$params);
  94.     }
  95.     /**
  96.      * {@inheritdoc}
  97.      */
  98.     public function supportsClassName(string $name): bool
  99.     {
  100.         $loader $this->getLoader($name);
  101.         if (null === $loader || !$loader instanceof ClassNameLoaderInterface) {
  102.             return false;
  103.         }
  104.         return $loader->supportsClassName($name);
  105.     }
  106.     /**
  107.      * {@inheritdoc}
  108.      */
  109.     public function getClassNameFor(string $name): string
  110.     {
  111.         $loader $this->getLoader($name);
  112.         if (null === $loader) {
  113.             throw new UnsupportedException(sprintf('Loader for "%s" was not found'$name));
  114.         }
  115.         if (!$loader instanceof ClassNameLoaderInterface) {
  116.             throw new UnsupportedException(sprintf(
  117.                 'Loader "%s" for "%s" does not support building a class name',
  118.                 get_class($loader),
  119.                 $name
  120.             ));
  121.         }
  122.         if (!$loader->supportsClassName($name)) {
  123.             throw new UnsupportedException(sprintf(
  124.                 'Building a class name for "%s" from loader "%s" is not supported',
  125.                 $name,
  126.                 get_class($loader)
  127.             ));
  128.         }
  129.         return $loader->getClassNameFor($name);
  130.     }
  131. }