1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-30 18:24:29 +01:00
vichan/inc/lib/Twig/Loader/Chain.php

139 lines
3.6 KiB
PHP
Raw Normal View History

2011-10-05 06:22:53 +02:00
<?php
/*
* This file is part of Twig.
*
* (c) 2011 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Loads templates from other loaders.
*
2013-08-01 21:20:12 +02:00
* @author Fabien Potencier <fabien@symfony.com>
2011-10-05 06:22:53 +02:00
*/
2013-08-01 21:20:12 +02:00
class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
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
/**
* Constructor.
*
* @param Twig_LoaderInterface[] $loaders An array of loader instances
*/
public function __construct(array $loaders = array())
{
foreach ($loaders as $loader) {
$this->addLoader($loader);
}
}
/**
* Adds a loader instance.
*
* @param Twig_LoaderInterface $loader A Loader instance
*/
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
}
/**
2013-08-01 21:20:12 +02:00
* {@inheritdoc}
2011-10-05 06:22:53 +02:00
*/
public function getSource($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->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
}
}
2013-08-01 21:20:12 +02:00
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(', ', $exceptions)));
2011-10-05 06:22:53 +02:00
}
/**
2013-08-01 21:20:12 +02:00
* {@inheritdoc}
*/
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 {
$loader->getSource($name);
return $this->hasSourceCache[$name] = true;
} catch (Twig_Error_Loader $e) {
}
}
return $this->hasSourceCache[$name] = false;
}
/**
* {@inheritdoc}
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
}
}
2013-08-01 21:20:12 +02:00
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
2011-10-05 06:22:53 +02:00
}
/**
2013-08-01 21:20:12 +02:00
* {@inheritdoc}
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
}
}
2013-08-01 21:20:12 +02:00
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
2011-10-05 06:22:53 +02:00
}
}