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