lainchan/inc/lib/Twig/Loader/Chain.php

139 lines
3.6 KiB
PHP
Raw Normal View History

2011-10-05 00:22:53 -04: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 15:20:12 -04:00
* @author Fabien Potencier <fabien@symfony.com>
2011-10-05 00:22:53 -04:00
*/
2013-08-01 15:20:12 -04:00
class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
2011-10-05 00:22:53 -04:00
{
2013-08-01 15:20:12 -04:00
private $hasSourceCache = array();
2013-09-19 02:08:25 -04:00
protected $loaders = array();
2011-10-05 00:22:53 -04: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 15:20:12 -04:00
$this->hasSourceCache = array();
2011-10-05 00:22:53 -04:00
}
/**
2013-08-01 15:20:12 -04:00
* {@inheritdoc}
2011-10-05 00:22:53 -04:00
*/
public function getSource($name)
{
2013-08-01 15:20:12 -04:00
$exceptions = array();
2011-10-05 00:22:53 -04:00
foreach ($this->loaders as $loader) {
2013-08-01 15:20:12 -04:00
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
continue;
}
2011-10-05 00:22:53 -04:00
try {
return $loader->getSource($name);
} catch (Twig_Error_Loader $e) {
2013-08-01 15:20:12 -04:00
$exceptions[] = $e->getMessage();
2011-10-05 00:22:53 -04:00
}
}
2013-08-01 15:20:12 -04:00
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(', ', $exceptions)));
2011-10-05 00:22:53 -04:00
}
/**
2013-08-01 15:20:12 -04: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 00:22:53 -04:00
*/
public function getCacheKey($name)
{
2013-08-01 15:20:12 -04:00
$exceptions = array();
2011-10-05 00:22:53 -04:00
foreach ($this->loaders as $loader) {
2013-08-01 15:20:12 -04:00
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
continue;
}
2011-10-05 00:22:53 -04:00
try {
return $loader->getCacheKey($name);
} catch (Twig_Error_Loader $e) {
2013-08-01 15:20:12 -04:00
$exceptions[] = get_class($loader).': '.$e->getMessage();
2011-10-05 00:22:53 -04:00
}
}
2013-08-01 15:20:12 -04:00
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
2011-10-05 00:22:53 -04:00
}
/**
2013-08-01 15:20:12 -04:00
* {@inheritdoc}
2011-10-05 00:22:53 -04:00
*/
public function isFresh($name, $time)
{
2013-08-01 15:20:12 -04:00
$exceptions = array();
2011-10-05 00:22:53 -04:00
foreach ($this->loaders as $loader) {
2013-08-01 15:20:12 -04:00
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
continue;
}
2011-10-05 00:22:53 -04:00
try {
return $loader->isFresh($name, $time);
} catch (Twig_Error_Loader $e) {
2013-08-01 15:20:12 -04:00
$exceptions[] = get_class($loader).': '.$e->getMessage();
2011-10-05 00:22:53 -04:00
}
}
2013-08-01 15:20:12 -04:00
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
2011-10-05 00:22:53 -04:00
}
}