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