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