1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-22 20:35:54 +01:00
vichan/inc/lib/Twig/Loader/Chain.php

152 lines
4.5 KiB
PHP
Raw Normal View History

2011-10-05 06:22:53 +02:00
<?php
/*
* This file is part of Twig.
*
2018-05-10 12:24:53 +02:00
* (c) Fabien Potencier
2011-10-05 06:22:53 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Loads templates from other loaders.
*
2018-05-10 12:24:53 +02:00
* @final
*
2013-08-01 21:20:12 +02:00
* @author Fabien Potencier <fabien@symfony.com>
2011-10-05 06:22:53 +02:00
*/
2018-05-10 12:24:53 +02:00
class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterface, Twig_SourceContextLoaderInterface
2011-10-05 06:22:53 +02:00
{
2013-08-01 21:20:12 +02:00
private $hasSourceCache = array();
2013-09-19 08:08:25 +02:00
protected $loaders = array();
2011-10-05 06:22:53 +02:00
/**
2018-05-10 12:24:53 +02:00
* @param Twig_LoaderInterface[] $loaders
2011-10-05 06:22:53 +02:00
*/
public function __construct(array $loaders = array())
{
foreach ($loaders as $loader) {
$this->addLoader($loader);
}
}
public function addLoader(Twig_LoaderInterface $loader)
{
$this->loaders[] = $loader;
2013-08-01 21:20:12 +02:00
$this->hasSourceCache = array();
2011-10-05 06:22:53 +02:00
}
public function getSource($name)
{
2018-05-10 12:24:53 +02:00
@trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since 1.27. Use getSourceContext() instead.', get_class($this)), E_USER_DEPRECATED);
2013-08-01 21:20:12 +02:00
$exceptions = array();
2011-10-05 06:22:53 +02:00
foreach ($this->loaders as $loader) {
2013-08-01 21:20:12 +02:00
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
continue;
}
2011-10-05 06:22:53 +02:00
try {
return $loader->getSource($name);
} catch (Twig_Error_Loader $e) {
2013-08-01 21:20:12 +02:00
$exceptions[] = $e->getMessage();
2011-10-05 06:22:53 +02:00
}
}
2018-05-10 12:24:53 +02:00
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
}
public function getSourceContext($name)
{
$exceptions = array();
foreach ($this->loaders as $loader) {
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
continue;
}
try {
if ($loader instanceof Twig_SourceContextLoaderInterface) {
return $loader->getSourceContext($name);
}
return new Twig_Source($loader->getSource($name), $name);
} catch (Twig_Error_Loader $e) {
$exceptions[] = $e->getMessage();
}
}
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
2011-10-05 06:22:53 +02:00
}
2013-08-01 21:20:12 +02:00
public function exists($name)
{
$name = (string) $name;
if (isset($this->hasSourceCache[$name])) {
return $this->hasSourceCache[$name];
}
foreach ($this->loaders as $loader) {
if ($loader instanceof Twig_ExistsLoaderInterface) {
if ($loader->exists($name)) {
return $this->hasSourceCache[$name] = true;
}
continue;
}
try {
2018-05-10 12:24:53 +02:00
if ($loader instanceof Twig_SourceContextLoaderInterface) {
$loader->getSourceContext($name);
} else {
$loader->getSource($name);
}
2013-08-01 21:20:12 +02:00
return $this->hasSourceCache[$name] = true;
} catch (Twig_Error_Loader $e) {
}
}
return $this->hasSourceCache[$name] = false;
}
2011-10-05 06:22:53 +02:00
public function getCacheKey($name)
{
2013-08-01 21:20:12 +02:00
$exceptions = array();
2011-10-05 06:22:53 +02:00
foreach ($this->loaders as $loader) {
2013-08-01 21:20:12 +02:00
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
continue;
}
2011-10-05 06:22:53 +02:00
try {
return $loader->getCacheKey($name);
} catch (Twig_Error_Loader $e) {
2013-08-01 21:20:12 +02:00
$exceptions[] = get_class($loader).': '.$e->getMessage();
2011-10-05 06:22:53 +02:00
}
}
2018-05-10 12:24:53 +02:00
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
2011-10-05 06:22:53 +02:00
}
public function isFresh($name, $time)
{
2013-08-01 21:20:12 +02:00
$exceptions = array();
2011-10-05 06:22:53 +02:00
foreach ($this->loaders as $loader) {
2013-08-01 21:20:12 +02:00
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
continue;
}
2011-10-05 06:22:53 +02:00
try {
return $loader->isFresh($name, $time);
} catch (Twig_Error_Loader $e) {
2013-08-01 21:20:12 +02:00
$exceptions[] = get_class($loader).': '.$e->getMessage();
2011-10-05 06:22:53 +02:00
}
}
2018-05-10 12:24:53 +02:00
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
2011-10-05 06:22:53 +02:00
}
}
2018-05-10 12:24:53 +02:00
class_alias('Twig_Loader_Chain', 'Twig\Loader\ChainLoader', false);