vendor/twig/twig/src/Loader/ChainLoader.php line 99

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Twig.
  4.  *
  5.  * (c) Fabien Potencier
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Twig\Loader;
  11. use Twig\Error\LoaderError;
  12. use Twig\Source;
  13. /**
  14.  * Loads templates from other loaders.
  15.  *
  16.  * @author Fabien Potencier <fabien@symfony.com>
  17.  */
  18. final class ChainLoader implements LoaderInterface
  19. {
  20.     private $hasSourceCache = [];
  21.     private $loaders = [];
  22.     /**
  23.      * @param LoaderInterface[] $loaders
  24.      */
  25.     public function __construct(array $loaders = [])
  26.     {
  27.         foreach ($loaders as $loader) {
  28.             $this->addLoader($loader);
  29.         }
  30.     }
  31.     public function addLoader(LoaderInterface $loader): void
  32.     {
  33.         $this->loaders[] = $loader;
  34.         $this->hasSourceCache = [];
  35.     }
  36.     /**
  37.      * @return LoaderInterface[]
  38.      */
  39.     public function getLoaders(): array
  40.     {
  41.         return $this->loaders;
  42.     }
  43.     public function getSourceContext(string $name): Source
  44.     {
  45.         $exceptions = [];
  46.         foreach ($this->loaders as $loader) {
  47.             if (!$loader->exists($name)) {
  48.                 continue;
  49.             }
  50.             try {
  51.                 return $loader->getSourceContext($name);
  52.             } catch (LoaderError $e) {
  53.                 $exceptions[] = $e->getMessage();
  54.             }
  55.         }
  56.         throw new LoaderError(sprintf('Template "%s" is not defined%s.'$name$exceptions ' ('.implode(', '$exceptions).')' ''));
  57.     }
  58.     public function exists(string $name): bool
  59.     {
  60.         if (isset($this->hasSourceCache[$name])) {
  61.             return $this->hasSourceCache[$name];
  62.         }
  63.         foreach ($this->loaders as $loader) {
  64.             if ($loader->exists($name)) {
  65.                 return $this->hasSourceCache[$name] = true;
  66.             }
  67.         }
  68.         return $this->hasSourceCache[$name] = false;
  69.     }
  70.     public function getCacheKey(string $name): string
  71.     {
  72.         $exceptions = [];
  73.         foreach ($this->loaders as $loader) {
  74.             if (!$loader->exists($name)) {
  75.                 continue;
  76.             }
  77.             try {
  78.                 return $loader->getCacheKey($name);
  79.             } catch (LoaderError $e) {
  80.                 $exceptions[] = \get_class($loader).': '.$e->getMessage();
  81.             }
  82.         }
  83.         throw new LoaderError(sprintf('Template "%s" is not defined%s.'$name$exceptions ' ('.implode(', '$exceptions).')' ''));
  84.     }
  85.     public function isFresh(string $nameint $time): bool
  86.     {
  87.         $exceptions = [];
  88.         foreach ($this->loaders as $loader) {
  89.             if (!$loader->exists($name)) {
  90.                 continue;
  91.             }
  92.             try {
  93.                 return $loader->isFresh($name$time);
  94.             } catch (LoaderError $e) {
  95.                 $exceptions[] = \get_class($loader).': '.$e->getMessage();
  96.             }
  97.         }
  98.         throw new LoaderError(sprintf('Template "%s" is not defined%s.'$name$exceptions ' ('.implode(', '$exceptions).')' ''));
  99.     }
  100. }