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

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.  * @final
  17.  *
  18.  * @author Fabien Potencier <fabien@symfony.com>
  19.  */
  20. class ChainLoader implements LoaderInterfaceExistsLoaderInterfaceSourceContextLoaderInterface
  21. {
  22.     private $hasSourceCache = [];
  23.     protected $loaders = [];
  24.     /**
  25.      * @param LoaderInterface[] $loaders
  26.      */
  27.     public function __construct(array $loaders = [])
  28.     {
  29.         foreach ($loaders as $loader) {
  30.             $this->addLoader($loader);
  31.         }
  32.     }
  33.     public function addLoader(LoaderInterface $loader)
  34.     {
  35.         $this->loaders[] = $loader;
  36.         $this->hasSourceCache = [];
  37.     }
  38.     /**
  39.      * @return LoaderInterface[]
  40.      */
  41.     public function getLoaders()
  42.     {
  43.         return $this->loaders;
  44.     }
  45.     public function getSource($name)
  46.     {
  47.         @trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since 1.27. Use getSourceContext() instead.', \get_class($this)), E_USER_DEPRECATED);
  48.         $exceptions = [];
  49.         foreach ($this->loaders as $loader) {
  50.             if ($loader instanceof ExistsLoaderInterface && !$loader->exists($name)) {
  51.                 continue;
  52.             }
  53.             try {
  54.                 return $loader->getSource($name);
  55.             } catch (LoaderError $e) {
  56.                 $exceptions[] = $e->getMessage();
  57.             }
  58.         }
  59.         throw new LoaderError(sprintf('Template "%s" is not defined%s.'$name$exceptions ' ('.implode(', '$exceptions).')' ''));
  60.     }
  61.     public function getSourceContext($name)
  62.     {
  63.         $exceptions = [];
  64.         foreach ($this->loaders as $loader) {
  65.             if ($loader instanceof ExistsLoaderInterface && !$loader->exists($name)) {
  66.                 continue;
  67.             }
  68.             try {
  69.                 if ($loader instanceof SourceContextLoaderInterface) {
  70.                     return $loader->getSourceContext($name);
  71.                 }
  72.                 return new Source($loader->getSource($name), $name);
  73.             } catch (LoaderError $e) {
  74.                 $exceptions[] = $e->getMessage();
  75.             }
  76.         }
  77.         throw new LoaderError(sprintf('Template "%s" is not defined%s.'$name$exceptions ' ('.implode(', '$exceptions).')' ''));
  78.     }
  79.     public function exists($name)
  80.     {
  81.         $name = (string) $name;
  82.         if (isset($this->hasSourceCache[$name])) {
  83.             return $this->hasSourceCache[$name];
  84.         }
  85.         foreach ($this->loaders as $loader) {
  86.             if ($loader instanceof ExistsLoaderInterface) {
  87.                 if ($loader->exists($name)) {
  88.                     return $this->hasSourceCache[$name] = true;
  89.                 }
  90.                 continue;
  91.             }
  92.             try {
  93.                 if ($loader instanceof SourceContextLoaderInterface) {
  94.                     $loader->getSourceContext($name);
  95.                 } else {
  96.                     $loader->getSource($name);
  97.                 }
  98.                 return $this->hasSourceCache[$name] = true;
  99.             } catch (LoaderError $e) {
  100.             }
  101.         }
  102.         return $this->hasSourceCache[$name] = false;
  103.     }
  104.     public function getCacheKey($name)
  105.     {
  106.         $exceptions = [];
  107.         foreach ($this->loaders as $loader) {
  108.             if ($loader instanceof ExistsLoaderInterface && !$loader->exists($name)) {
  109.                 continue;
  110.             }
  111.             try {
  112.                 return $loader->getCacheKey($name);
  113.             } catch (LoaderError $e) {
  114.                 $exceptions[] = \get_class($loader).': '.$e->getMessage();
  115.             }
  116.         }
  117.         throw new LoaderError(sprintf('Template "%s" is not defined%s.'$name$exceptions ' ('.implode(', '$exceptions).')' ''));
  118.     }
  119.     public function isFresh($name$time)
  120.     {
  121.         $exceptions = [];
  122.         foreach ($this->loaders as $loader) {
  123.             if ($loader instanceof ExistsLoaderInterface && !$loader->exists($name)) {
  124.                 continue;
  125.             }
  126.             try {
  127.                 return $loader->isFresh($name$time);
  128.             } catch (LoaderError $e) {
  129.                 $exceptions[] = \get_class($loader).': '.$e->getMessage();
  130.             }
  131.         }
  132.         throw new LoaderError(sprintf('Template "%s" is not defined%s.'$name$exceptions ' ('.implode(', '$exceptions).')' ''));
  133.     }
  134. }
  135. class_alias('Twig\Loader\ChainLoader''Twig_Loader_Chain');