2024-06-20 14:10:42 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Twig.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Twig\Loader;
|
|
|
|
|
|
|
|
use Twig\Error\LoaderError;
|
|
|
|
use Twig\Source;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads templates from other loaders.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
*/
|
|
|
|
final class ChainLoader implements LoaderInterface
|
|
|
|
{
|
2025-01-13 09:56:01 +00:00
|
|
|
/**
|
|
|
|
* @var array<string, bool>
|
|
|
|
*/
|
2024-06-20 14:10:42 +00:00
|
|
|
private $hasSourceCache = [];
|
|
|
|
|
|
|
|
/**
|
2025-01-13 09:56:01 +00:00
|
|
|
* @param iterable<LoaderInterface> $loaders
|
2024-06-20 14:10:42 +00:00
|
|
|
*/
|
2025-01-13 09:56:01 +00:00
|
|
|
public function __construct(
|
|
|
|
private iterable $loaders = [],
|
|
|
|
) {
|
2024-06-20 14:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addLoader(LoaderInterface $loader): void
|
|
|
|
{
|
2025-01-13 09:56:01 +00:00
|
|
|
$current = $this->loaders;
|
|
|
|
|
|
|
|
$this->loaders = (static function () use ($current, $loader): \Generator {
|
|
|
|
yield from $current;
|
|
|
|
yield $loader;
|
|
|
|
})();
|
|
|
|
|
2024-06-20 14:10:42 +00:00
|
|
|
$this->hasSourceCache = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return LoaderInterface[]
|
|
|
|
*/
|
|
|
|
public function getLoaders(): array
|
|
|
|
{
|
2025-01-13 09:56:01 +00:00
|
|
|
if (!\is_array($this->loaders)) {
|
|
|
|
$this->loaders = iterator_to_array($this->loaders, false);
|
|
|
|
}
|
|
|
|
|
2024-06-20 14:10:42 +00:00
|
|
|
return $this->loaders;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSourceContext(string $name): Source
|
|
|
|
{
|
|
|
|
$exceptions = [];
|
2025-01-13 09:56:01 +00:00
|
|
|
|
|
|
|
foreach ($this->getLoaders() as $loader) {
|
2024-06-20 14:10:42 +00:00
|
|
|
if (!$loader->exists($name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return $loader->getSourceContext($name);
|
|
|
|
} catch (LoaderError $e) {
|
|
|
|
$exceptions[] = $e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 17:51:48 +00:00
|
|
|
throw new LoaderError(\sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
|
2024-06-20 14:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function exists(string $name): bool
|
|
|
|
{
|
|
|
|
if (isset($this->hasSourceCache[$name])) {
|
|
|
|
return $this->hasSourceCache[$name];
|
|
|
|
}
|
|
|
|
|
2025-01-13 09:56:01 +00:00
|
|
|
foreach ($this->getLoaders() as $loader) {
|
2024-06-20 14:10:42 +00:00
|
|
|
if ($loader->exists($name)) {
|
|
|
|
return $this->hasSourceCache[$name] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->hasSourceCache[$name] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCacheKey(string $name): string
|
|
|
|
{
|
|
|
|
$exceptions = [];
|
2025-01-13 09:56:01 +00:00
|
|
|
|
|
|
|
foreach ($this->getLoaders() as $loader) {
|
2024-06-20 14:10:42 +00:00
|
|
|
if (!$loader->exists($name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return $loader->getCacheKey($name);
|
|
|
|
} catch (LoaderError $e) {
|
|
|
|
$exceptions[] = \get_class($loader).': '.$e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 17:51:48 +00:00
|
|
|
throw new LoaderError(\sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
|
2024-06-20 14:10:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isFresh(string $name, int $time): bool
|
|
|
|
{
|
|
|
|
$exceptions = [];
|
2025-01-13 09:56:01 +00:00
|
|
|
|
|
|
|
foreach ($this->getLoaders() as $loader) {
|
2024-06-20 14:10:42 +00:00
|
|
|
if (!$loader->exists($name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return $loader->isFresh($name, $time);
|
|
|
|
} catch (LoaderError $e) {
|
|
|
|
$exceptions[] = \get_class($loader).': '.$e->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-05 17:51:48 +00:00
|
|
|
throw new LoaderError(\sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
|
2024-06-20 14:10:42 +00:00
|
|
|
}
|
|
|
|
}
|