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

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. /**
  13.  * Loads templates from other loaders.
  14.  *
  15.  * @author Fabien Potencier <fabien@symfony.com>
  16.  */
  17. final class ChainLoader implements LoaderInterfaceExistsLoaderInterfaceSourceContextLoaderInterface
  18. {
  19.     private $hasSourceCache = [];
  20.     private $loaders = [];
  21.     /**
  22.      * @param LoaderInterface[] $loaders
  23.      */
  24.     public function __construct(array $loaders = [])
  25.     {
  26.         foreach ($loaders as $loader) {
  27.             $this->addLoader($loader);
  28.         }
  29.     }
  30.     public function addLoader(LoaderInterface $loader)
  31.     {
  32.         $this->loaders[] = $loader;
  33.         $this->hasSourceCache = [];
  34.     }
  35.     /**
  36.      * @return LoaderInterface[]
  37.      */
  38.     public function getLoaders()
  39.     {
  40.         return $this->loaders;
  41.     }
  42.     public function getSourceContext($name)
  43.     {
  44.         $exceptions = [];
  45.         foreach ($this->loaders as $loader) {
  46.             if (!$loader->exists($name)) {
  47.                 continue;
  48.             }
  49.             try {
  50.                 return $loader->getSourceContext($name);
  51.             } catch (LoaderError $e) {
  52.                 $exceptions[] = $e->getMessage();
  53.             }
  54.         }
  55.         throw new LoaderError(sprintf('Template "%s" is not defined%s.'$name$exceptions ' ('.implode(', '$exceptions).')' ''));
  56.     }
  57.     public function exists($name)
  58.     {
  59.         if (isset($this->hasSourceCache[$name])) {
  60.             return $this->hasSourceCache[$name];
  61.         }
  62.         foreach ($this->loaders as $loader) {
  63.             if ($loader->exists($name)) {
  64.                 return $this->hasSourceCache[$name] = true;
  65.             }
  66.         }
  67.         return $this->hasSourceCache[$name] = false;
  68.     }
  69.     public function getCacheKey($name)
  70.     {
  71.         $exceptions = [];
  72.         foreach ($this->loaders as $loader) {
  73.             if (!$loader->exists($name)) {
  74.                 continue;
  75.             }
  76.             try {
  77.                 return $loader->getCacheKey($name);
  78.             } catch (LoaderError $e) {
  79.                 $exceptions[] = \get_class($loader).': '.$e->getMessage();
  80.             }
  81.         }
  82.         throw new LoaderError(sprintf('Template "%s" is not defined%s.'$name$exceptions ' ('.implode(', '$exceptions).')' ''));
  83.     }
  84.     public function isFresh($name$time)
  85.     {
  86.         $exceptions = [];
  87.         foreach ($this->loaders as $loader) {
  88.             if (!$loader->exists($name)) {
  89.                 continue;
  90.             }
  91.             try {
  92.                 return $loader->isFresh($name$time);
  93.             } catch (LoaderError $e) {
  94.                 $exceptions[] = \get_class($loader).': '.$e->getMessage();
  95.             }
  96.         }
  97.         throw new LoaderError(sprintf('Template "%s" is not defined%s.'$name$exceptions ' ('.implode(', '$exceptions).')' ''));
  98.     }
  99. }
  100. class_alias('Twig\Loader\ChainLoader''Twig_Loader_Chain');