4 * This file is part of Twig.
6 * (c) 2011 Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Loads templates from other loaders.
15 * @author Fabien Potencier <fabien@symfony.com>
17 class Twig_Loader_Chain
implements Twig_LoaderInterface
, Twig_ExistsLoaderInterface
19 private $hasSourceCache = array();
25 * @param Twig_LoaderInterface[] $loaders An array of loader instances
27 public function __construct(array $loaders = array())
29 $this->loaders
= array();
30 foreach ($loaders as $loader) {
31 $this->addLoader($loader);
36 * Adds a loader instance.
38 * @param Twig_LoaderInterface $loader A Loader instance
40 public function addLoader(Twig_LoaderInterface
$loader)
42 $this->loaders
[] = $loader;
43 $this->hasSourceCache
= array();
49 public function getSource($name)
51 $exceptions = array();
52 foreach ($this->loaders
as $loader) {
53 if ($loader instanceof Twig_ExistsLoaderInterface
&& !$loader->exists($name)) {
58 return $loader->getSource($name);
59 } catch (Twig_Error_Loader
$e) {
60 $exceptions[] = $e->getMessage();
64 throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(', ', $exceptions)));
70 public function exists($name)
72 $name = (string) $name;
74 if (isset($this->hasSourceCache
[$name])) {
75 return $this->hasSourceCache
[$name];
78 foreach ($this->loaders
as $loader) {
79 if ($loader instanceof Twig_ExistsLoaderInterface
) {
80 if ($loader->exists($name)) {
81 return $this->hasSourceCache
[$name] = true;
88 $loader->getSource($name);
90 return $this->hasSourceCache
[$name] = true;
91 } catch (Twig_Error_Loader
$e) {
95 return $this->hasSourceCache
[$name] = false;
101 public function getCacheKey($name)
103 $exceptions = array();
104 foreach ($this->loaders
as $loader) {
105 if ($loader instanceof Twig_ExistsLoaderInterface
&& !$loader->exists($name)) {
110 return $loader->getCacheKey($name);
111 } catch (Twig_Error_Loader
$e) {
112 $exceptions[] = get_class($loader).': '.$e->getMessage();
116 throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
122 public function isFresh($name, $time)
124 $exceptions = array();
125 foreach ($this->loaders
as $loader) {
126 if ($loader instanceof Twig_ExistsLoaderInterface
&& !$loader->exists($name)) {
131 return $loader->isFresh($name, $time);
132 } catch (Twig_Error_Loader
$e) {
133 $exceptions[] = get_class($loader).': '.$e->getMessage();
137 throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));