Revision 1852
Added by darkviper over 11 years ago
Filesystem.php | ||
---|---|---|
15 | 15 |
* @package twig |
16 | 16 |
* @author Fabien Potencier <fabien@symfony.com> |
17 | 17 |
*/ |
18 |
class Twig_Loader_Filesystem implements Twig_LoaderInterface |
|
18 |
class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
|
|
19 | 19 |
{ |
20 | 20 |
protected $paths; |
21 | 21 |
protected $cache; |
... | ... | |
33 | 33 |
/** |
34 | 34 |
* Returns the paths to the templates. |
35 | 35 |
* |
36 |
* @param string $namespace A path namespace |
|
37 |
* |
|
36 | 38 |
* @return array The array of paths where to look for templates |
37 | 39 |
*/ |
38 |
public function getPaths() |
|
40 |
public function getPaths($namespace = '__main__')
|
|
39 | 41 |
{ |
40 |
return $this->paths;
|
|
42 |
return isset($this->paths[$namespace]) ? $this->paths[$namespace] : array();
|
|
41 | 43 |
} |
42 | 44 |
|
43 | 45 |
/** |
46 |
* Returns the path namespaces. |
|
47 |
* |
|
48 |
* The "__main__" namespace is always defined. |
|
49 |
* |
|
50 |
* @return array The array of defined namespaces |
|
51 |
*/ |
|
52 |
public function getNamespaces() |
|
53 |
{ |
|
54 |
return array_keys($this->paths); |
|
55 |
} |
|
56 |
|
|
57 |
/** |
|
44 | 58 |
* Sets the paths where templates are stored. |
45 | 59 |
* |
46 |
* @param string|array $paths A path or an array of paths where to look for templates |
|
60 |
* @param string|array $paths A path or an array of paths where to look for templates |
|
61 |
* @param string $namespace A path namespace |
|
47 | 62 |
*/ |
48 |
public function setPaths($paths) |
|
63 |
public function setPaths($paths, $namespace = '__main__')
|
|
49 | 64 |
{ |
50 | 65 |
if (!is_array($paths)) { |
51 | 66 |
$paths = array($paths); |
52 | 67 |
} |
53 | 68 |
|
54 |
$this->paths = array(); |
|
69 |
$this->paths[$namespace] = array();
|
|
55 | 70 |
foreach ($paths as $path) { |
56 |
$this->addPath($path); |
|
71 |
$this->addPath($path, $namespace);
|
|
57 | 72 |
} |
58 | 73 |
} |
59 | 74 |
|
60 | 75 |
/** |
61 | 76 |
* Adds a path where templates are stored. |
62 | 77 |
* |
63 |
* @param string $path A path where to look for templates |
|
78 |
* @param string $path A path where to look for templates |
|
79 |
* @param string $namespace A path name |
|
80 |
* |
|
81 |
* @throws Twig_Error_Loader |
|
64 | 82 |
*/ |
65 |
public function addPath($path) |
|
83 |
public function addPath($path, $namespace = '__main__')
|
|
66 | 84 |
{ |
67 | 85 |
// invalidate the cache |
68 | 86 |
$this->cache = array(); |
... | ... | |
71 | 89 |
throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path)); |
72 | 90 |
} |
73 | 91 |
|
74 |
$this->paths[] = $path;
|
|
92 |
$this->paths[$namespace][] = rtrim($path, '/\\');
|
|
75 | 93 |
} |
76 | 94 |
|
77 | 95 |
/** |
78 |
* Gets the source code of a template, given its name.
|
|
96 |
* Prepends a path where templates are stored.
|
|
79 | 97 |
* |
80 |
* @param string $name The name of the template to load |
|
98 |
* @param string $path A path where to look for templates |
|
99 |
* @param string $namespace A path name |
|
81 | 100 |
* |
82 |
* @return string The template source code
|
|
101 |
* @throws Twig_Error_Loader
|
|
83 | 102 |
*/ |
103 |
public function prependPath($path, $namespace = '__main__') |
|
104 |
{ |
|
105 |
// invalidate the cache |
|
106 |
$this->cache = array(); |
|
107 |
|
|
108 |
if (!is_dir($path)) { |
|
109 |
throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path)); |
|
110 |
} |
|
111 |
|
|
112 |
$path = rtrim($path, '/\\'); |
|
113 |
|
|
114 |
if (!isset($this->paths[$namespace])) { |
|
115 |
$this->paths[$namespace][] = $path; |
|
116 |
} else { |
|
117 |
array_unshift($this->paths[$namespace], $path); |
|
118 |
} |
|
119 |
} |
|
120 |
|
|
121 |
/** |
|
122 |
* {@inheritdoc} |
|
123 |
*/ |
|
84 | 124 |
public function getSource($name) |
85 | 125 |
{ |
86 | 126 |
return file_get_contents($this->findTemplate($name)); |
87 | 127 |
} |
88 | 128 |
|
89 | 129 |
/** |
90 |
* Gets the cache key to use for the cache for a given template name. |
|
91 |
* |
|
92 |
* @param string $name The name of the template to load |
|
93 |
* |
|
94 |
* @return string The cache key |
|
130 |
* {@inheritdoc} |
|
95 | 131 |
*/ |
96 | 132 |
public function getCacheKey($name) |
97 | 133 |
{ |
... | ... | |
99 | 135 |
} |
100 | 136 |
|
101 | 137 |
/** |
102 |
* Returns true if the template is still fresh. |
|
103 |
* |
|
104 |
* @param string $name The template name |
|
105 |
* @param timestamp $time The last modification time of the cached template |
|
138 |
* {@inheritdoc} |
|
106 | 139 |
*/ |
140 |
public function exists($name) |
|
141 |
{ |
|
142 |
$name = (string) $name; |
|
143 |
if (isset($this->cache[$name])) { |
|
144 |
return true; |
|
145 |
} |
|
146 |
|
|
147 |
try { |
|
148 |
$this->findTemplate($name); |
|
149 |
|
|
150 |
return true; |
|
151 |
} catch (Twig_Error_Loader $exception) { |
|
152 |
return false; |
|
153 |
} |
|
154 |
} |
|
155 |
|
|
156 |
/** |
|
157 |
* {@inheritdoc} |
|
158 |
*/ |
|
107 | 159 |
public function isFresh($name, $time) |
108 | 160 |
{ |
109 | 161 |
return filemtime($this->findTemplate($name)) <= $time; |
... | ... | |
111 | 163 |
|
112 | 164 |
protected function findTemplate($name) |
113 | 165 |
{ |
166 |
$name = (string) $name; |
|
167 |
|
|
114 | 168 |
// normalize name |
115 | 169 |
$name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')); |
116 | 170 |
|
... | ... | |
120 | 174 |
|
121 | 175 |
$this->validateName($name); |
122 | 176 |
|
123 |
foreach ($this->paths as $path) { |
|
177 |
$namespace = '__main__'; |
|
178 |
if (isset($name[0]) && '@' == $name[0]) { |
|
179 |
if (false === $pos = strpos($name, '/')) { |
|
180 |
throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name)); |
|
181 |
} |
|
182 |
|
|
183 |
$namespace = substr($name, 1, $pos - 1); |
|
184 |
|
|
185 |
$name = substr($name, $pos + 1); |
|
186 |
} |
|
187 |
|
|
188 |
if (!isset($this->paths[$namespace])) { |
|
189 |
throw new Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace)); |
|
190 |
} |
|
191 |
|
|
192 |
foreach ($this->paths[$namespace] as $path) { |
|
124 | 193 |
if (is_file($path.'/'.$name)) { |
125 | 194 |
return $this->cache[$name] = $path.'/'.$name; |
126 | 195 |
} |
127 | 196 |
} |
128 | 197 |
|
129 |
throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths))); |
|
198 |
throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
|
|
130 | 199 |
} |
131 | 200 |
|
132 | 201 |
protected function validateName($name) |
Also available in: Unified diff
updated Twig template engine to stable version 1.11.1 step2