diff options
Diffstat (limited to 'inc/Twig/Loader/Chain.php')
-rw-r--r-- | inc/Twig/Loader/Chain.php | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/inc/Twig/Loader/Chain.php b/inc/Twig/Loader/Chain.php new file mode 100644 index 00000000..1f1cf065 --- /dev/null +++ b/inc/Twig/Loader/Chain.php | |||
@@ -0,0 +1,139 @@ | |||
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; | ||
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 | $this->loaders = array(); | ||
30 | foreach ($loaders as $loader) { | ||
31 | $this->addLoader($loader); | ||
32 | } | ||
33 | } | ||
34 | |||
35 | /** | ||
36 | * Adds a loader instance. | ||
37 | * | ||
38 | * @param Twig_LoaderInterface $loader A Loader instance | ||
39 | */ | ||
40 | public function addLoader(Twig_LoaderInterface $loader) | ||
41 | { | ||
42 | $this->loaders[] = $loader; | ||
43 | $this->hasSourceCache = array(); | ||
44 | } | ||
45 | |||
46 | /** | ||
47 | * {@inheritdoc} | ||
48 | */ | ||
49 | public function getSource($name) | ||
50 | { | ||
51 | $exceptions = array(); | ||
52 | foreach ($this->loaders as $loader) { | ||
53 | if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) { | ||
54 | continue; | ||
55 | } | ||
56 | |||
57 | try { | ||
58 | return $loader->getSource($name); | ||
59 | } catch (Twig_Error_Loader $e) { | ||
60 | $exceptions[] = $e->getMessage(); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(', ', $exceptions))); | ||
65 | } | ||
66 | |||
67 | /** | ||
68 | * {@inheritdoc} | ||
69 | */ | ||
70 | public function exists($name) | ||
71 | { | ||
72 | $name = (string) $name; | ||
73 | |||
74 | if (isset($this->hasSourceCache[$name])) { | ||
75 | return $this->hasSourceCache[$name]; | ||
76 | } | ||
77 | |||
78 | foreach ($this->loaders as $loader) { | ||
79 | if ($loader instanceof Twig_ExistsLoaderInterface) { | ||
80 | if ($loader->exists($name)) { | ||
81 | return $this->hasSourceCache[$name] = true; | ||
82 | } | ||
83 | |||
84 | continue; | ||
85 | } | ||
86 | |||
87 | try { | ||
88 | $loader->getSource($name); | ||
89 | |||
90 | return $this->hasSourceCache[$name] = true; | ||
91 | } catch (Twig_Error_Loader $e) { | ||
92 | } | ||
93 | } | ||
94 | |||
95 | return $this->hasSourceCache[$name] = false; | ||
96 | } | ||
97 | |||
98 | /** | ||
99 | * {@inheritdoc} | ||
100 | */ | ||
101 | public function getCacheKey($name) | ||
102 | { | ||
103 | $exceptions = array(); | ||
104 | foreach ($this->loaders as $loader) { | ||
105 | if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) { | ||
106 | continue; | ||
107 | } | ||
108 | |||
109 | try { | ||
110 | return $loader->getCacheKey($name); | ||
111 | } catch (Twig_Error_Loader $e) { | ||
112 | $exceptions[] = get_class($loader).': '.$e->getMessage(); | ||
113 | } | ||
114 | } | ||
115 | |||
116 | throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions))); | ||
117 | } | ||
118 | |||
119 | /** | ||
120 | * {@inheritdoc} | ||
121 | */ | ||
122 | public function isFresh($name, $time) | ||
123 | { | ||
124 | $exceptions = array(); | ||
125 | foreach ($this->loaders as $loader) { | ||
126 | if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) { | ||
127 | continue; | ||
128 | } | ||
129 | |||
130 | try { | ||
131 | return $loader->isFresh($name, $time); | ||
132 | } catch (Twig_Error_Loader $e) { | ||
133 | $exceptions[] = get_class($loader).': '.$e->getMessage(); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions))); | ||
138 | } | ||
139 | } | ||