Revision 1852
Added by darkviper over 11 years ago
Chain.php | ||
---|---|---|
15 | 15 |
* @package twig |
16 | 16 |
* @author Fabien Potencier <fabien@symfony.com> |
17 | 17 |
*/ |
18 |
class Twig_Loader_Chain implements Twig_LoaderInterface |
|
18 |
class Twig_Loader_Chain implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
|
|
19 | 19 |
{ |
20 |
private $hasSourceCache = array(); |
|
20 | 21 |
protected $loaders; |
21 | 22 |
|
22 | 23 |
/** |
... | ... | |
40 | 41 |
public function addLoader(Twig_LoaderInterface $loader) |
41 | 42 |
{ |
42 | 43 |
$this->loaders[] = $loader; |
44 |
$this->hasSourceCache = array(); |
|
43 | 45 |
} |
44 | 46 |
|
45 | 47 |
/** |
46 |
* Gets the source code of a template, given its name. |
|
47 |
* |
|
48 |
* @param string $name The name of the template to load |
|
49 |
* |
|
50 |
* @return string The template source code |
|
48 |
* {@inheritdoc} |
|
51 | 49 |
*/ |
52 | 50 |
public function getSource($name) |
53 | 51 |
{ |
52 |
$exceptions = array(); |
|
54 | 53 |
foreach ($this->loaders as $loader) { |
54 |
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) { |
|
55 |
continue; |
|
56 |
} |
|
57 |
|
|
55 | 58 |
try { |
56 | 59 |
return $loader->getSource($name); |
57 | 60 |
} catch (Twig_Error_Loader $e) { |
61 |
$exceptions[] = $e->getMessage(); |
|
58 | 62 |
} |
59 | 63 |
} |
60 | 64 |
|
61 |
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
|
|
65 |
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(', ', $exceptions)));
|
|
62 | 66 |
} |
63 | 67 |
|
64 | 68 |
/** |
65 |
* Gets the cache key to use for the cache for a given template name. |
|
66 |
* |
|
67 |
* @param string $name The name of the template to load |
|
68 |
* |
|
69 |
* @return string The cache key |
|
69 |
* {@inheritdoc} |
|
70 | 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 |
*/ |
|
71 | 98 |
public function getCacheKey($name) |
72 | 99 |
{ |
100 |
$exceptions = array(); |
|
73 | 101 |
foreach ($this->loaders as $loader) { |
102 |
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) { |
|
103 |
continue; |
|
104 |
} |
|
105 |
|
|
74 | 106 |
try { |
75 | 107 |
return $loader->getCacheKey($name); |
76 | 108 |
} catch (Twig_Error_Loader $e) { |
109 |
$exceptions[] = get_class($loader).': '.$e->getMessage(); |
|
77 | 110 |
} |
78 | 111 |
} |
79 | 112 |
|
80 |
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
|
|
113 |
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
|
|
81 | 114 |
} |
82 | 115 |
|
83 | 116 |
/** |
84 |
* Returns true if the template is still fresh. |
|
85 |
* |
|
86 |
* @param string $name The template name |
|
87 |
* @param timestamp $time The last modification time of the cached template |
|
117 |
* {@inheritdoc} |
|
88 | 118 |
*/ |
89 | 119 |
public function isFresh($name, $time) |
90 | 120 |
{ |
121 |
$exceptions = array(); |
|
91 | 122 |
foreach ($this->loaders as $loader) { |
123 |
if ($loader instanceof Twig_ExistsLoaderInterface && !$loader->exists($name)) { |
|
124 |
continue; |
|
125 |
} |
|
126 |
|
|
92 | 127 |
try { |
93 | 128 |
return $loader->isFresh($name, $time); |
94 | 129 |
} catch (Twig_Error_Loader $e) { |
130 |
$exceptions[] = get_class($loader).': '.$e->getMessage(); |
|
95 | 131 |
} |
96 | 132 |
} |
97 | 133 |
|
98 |
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name));
|
|
134 |
throw new Twig_Error_Loader(sprintf('Template "%s" is not defined (%s).', $name, implode(' ', $exceptions)));
|
|
99 | 135 |
} |
100 | 136 |
} |
Also available in: Unified diff
updated Twig template engine to stable version 1.11.1 step2