4 * This file is part of Twig.
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.
17 * @author Fabien Potencier <fabien@symfony.com>
19 class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterface, Twig_SourceContextLoaderInterface
21 private $hasSourceCache = array();
22 protected $loaders = array();
25 * @param Twig_LoaderInterface[] $loaders
27 public function __construct(array $loaders = array())
29 foreach ($loaders as $loader) {
30 $this->addLoader($loader);
34 public function addLoader(Twig_LoaderInterface $loader)
36 $this->loaders[] = $loader;
37 $this->hasSourceCache = array();
40 public function getSource($name)
42 @trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since 1.27. Use getSourceContext() instead.', get_class($this)), E_USER_DEPRECATED);
44 $exceptions = array();
45 foreach ($this->loaders as $loader) {
46 if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
51 return $loader->getSource($name);
52 } catch (Twig_Error_Loader $e) {
53 $exceptions[] = $e->getMessage();
57 throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
60 public function getSourceContext($name)
62 $exceptions = array();
63 foreach ($this->loaders as $loader) {
64 if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
69 if ($loader instanceof Twig_SourceContextLoaderInterface) {
70 return $loader->getSourceContext($name);
73 return new Twig_Source($loader->getSource($name), $name);
74 } catch (Twig_Error_Loader $e) {
75 $exceptions[] = $e->getMessage();
79 throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
82 public function exists($name)
84 $name = (string) $name;
86 if (isset($this->hasSourceCache[$name])) {
87 return $this->hasSourceCache[$name];
90 foreach ($this->loaders as $loader) {
91 if ($loader instanceof Twig_ExistsLoaderInterface) {
92 if ($loader->exists($name)) {
93 return $this->hasSourceCache[$name] = true;
100 if ($loader instanceof Twig_SourceContextLoaderInterface) {
101 $loader->getSourceContext($name);
103 $loader->getSource($name);
106 return $this->hasSourceCache[$name] = true;
107 } catch (Twig_Error_Loader $e) {
111 return $this->hasSourceCache[$name] = false;
114 public function getCacheKey($name)
116 $exceptions = array();
117 foreach ($this->loaders as $loader) {
118 if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
123 return $loader->getCacheKey($name);
124 } catch (Twig_Error_Loader $e) {
125 $exceptions[] = get_class($loader).': '.$e->getMessage();
129 throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
132 public function isFresh($name, $time)
134 $exceptions = array();
135 foreach ($this->loaders as $loader) {
136 if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
141 return $loader->isFresh($name, $time);
142 } catch (Twig_Error_Loader $e) {
143 $exceptions[] = get_class($loader).': '.$e->getMessage();
147 throw new Twig_Error_Loader(sprintf('Template "%s" is not defined%s.', $name, $exceptions ? ' ('.implode(', ', $exceptions).')' : ''));
151 class_alias('Twig_Loader_Chain', 'Twig\Loader\ChainLoader', false);