1
|
<?php
|
2
|
|
3
|
/*
|
4
|
* This file is part of Twig.
|
5
|
*
|
6
|
* (c) 2011 Fabien Potencier
|
7
|
*
|
8
|
* For the full copyright and license information, please view the LICENSE
|
9
|
* file that was distributed with this source code.
|
10
|
*/
|
11
|
|
12
|
/**
|
13
|
* Loads templates from other loaders.
|
14
|
*
|
15
|
* @package twig
|
16
|
* @author Fabien Potencier <fabien@symfony.com>
|
17
|
*/
|
18
|
class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
|
19
|
{
|
20
|
private $hasSourceCache = array();
|
21
|
protected $loaders;
|
22
|
|
23
|
/**
|
24
|
* Constructor.
|
25
|
*
|
26
|
* @param Twig_LoaderInterface[] $loaders An array of loader instances
|
27
|
*/
|
28
|
public function __construct(array $loaders = array())
|
29
|
{
|
30
|
$this->loaders = array();
|
31
|
foreach ($loaders as $loader) {
|
32
|
$this->addLoader($loader);
|
33
|
}
|
34
|
}
|
35
|
|
36
|
/**
|
37
|
* Adds a loader instance.
|
38
|
*
|
39
|
* @param Twig_LoaderInterface $loader A Loader instance
|
40
|
*/
|
41
|
public function addLoader(Twig_LoaderInterface $loader)
|
42
|
{
|
43
|
$this->loaders[] = $loader;
|
44
|
$this->hasSourceCache = array();
|
45
|
}
|
46
|
|
47
|
/**
|
48
|
* {@inheritdoc}
|
49
|
*/
|
50
|
public function getSource($name)
|
51
|
{
|
52
|
$exceptions = array();
|
53
|
foreach ($this->loaders as $loader) {
|
54
|
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
|
55
|
continue;
|
56
|
}
|
57
|
|
58
|
try {
|
59
|
return $loader->getSource($name);
|
60
|
} catch (Twig_Error_Loader $e) {
|
61
|
$exceptions[] = $e->getMessage();
|
62
|
}
|
63
|
}
|
64
|
|
65
|
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(', ', $exceptions)));
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* {@inheritdoc}
|
70
|
*/
|
71
|
public function exists($name)
|
72
|
{
|
73
|
$name = (string) $name;
|
74
|
|
75
|
if (isset($this->hasSourceCache[$name])) {
|
76
|
return $this->hasSourceCache[$name];
|
77
|
}
|
78
|
|
79
|
foreach ($this->loaders as $loader) {
|
80
|
if ($loader instanceof Twig_ExistsLoaderInterface && $loader->exists($name)) {
|
81
|
return $this->hasSourceCache[$name] = true;
|
82
|
}
|
83
|
|
84
|
try {
|
85
|
$loader->getSource($name);
|
86
|
|
87
|
return $this->hasSourceCache[$name] = true;
|
88
|
} catch (Twig_Error_Loader $e) {
|
89
|
}
|
90
|
}
|
91
|
|
92
|
return $this->hasSourceCache[$name] = false;
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* {@inheritdoc}
|
97
|
*/
|
98
|
public function getCacheKey($name)
|
99
|
{
|
100
|
$exceptions = array();
|
101
|
foreach ($this->loaders as $loader) {
|
102
|
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
|
103
|
continue;
|
104
|
}
|
105
|
|
106
|
try {
|
107
|
return $loader->getCacheKey($name);
|
108
|
} catch (Twig_Error_Loader $e) {
|
109
|
$exceptions[] = get_class($loader).': '.$e->getMessage();
|
110
|
}
|
111
|
}
|
112
|
|
113
|
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* {@inheritdoc}
|
118
|
*/
|
119
|
public function isFresh($name, $time)
|
120
|
{
|
121
|
$exceptions = array();
|
122
|
foreach ($this->loaders as $loader) {
|
123
|
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) {
|
124
|
continue;
|
125
|
}
|
126
|
|
127
|
try {
|
128
|
return $loader->isFresh($name, $time);
|
129
|
} catch (Twig_Error_Loader $e) {
|
130
|
$exceptions[] = get_class($loader).': '.$e->getMessage();
|
131
|
}
|
132
|
}
|
133
|
|
134
|
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
|
135
|
}
|
136
|
}
|