4 * This file is part of Twig.
6 * (c) 2009 Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Loads template from the filesystem.
15 * @author Fabien Potencier <fabien@symfony.com>
17 class Twig_Loader_Filesystem
implements Twig_LoaderInterface
, Twig_ExistsLoaderInterface
25 * @param string|array $paths A path or an array of paths where to look for templates
27 public function __construct($paths = array())
30 $this->setPaths($paths);
35 * Returns the paths to the templates.
37 * @param string $namespace A path namespace
39 * @return array The array of paths where to look for templates
41 public function getPaths($namespace = '__main__')
43 return isset($this->paths
[$namespace]) ? $this->paths
[$namespace] : array();
47 * Returns the path namespaces.
49 * The "__main__" namespace is always defined.
51 * @return array The array of defined namespaces
53 public function getNamespaces()
55 return array_keys($this->paths
);
59 * Sets the paths where templates are stored.
61 * @param string|array $paths A path or an array of paths where to look for templates
62 * @param string $namespace A path namespace
64 public function setPaths($paths, $namespace = '__main__')
66 if (!is_array($paths)) {
67 $paths = array($paths);
70 $this->paths
[$namespace] = array();
71 foreach ($paths as $path) {
72 $this->addPath($path, $namespace);
77 * Adds a path where templates are stored.
79 * @param string $path A path where to look for templates
80 * @param string $namespace A path name
82 * @throws Twig_Error_Loader
84 public function addPath($path, $namespace = '__main__')
86 // invalidate the cache
87 $this->cache
= array();
90 throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
93 $this->paths
[$namespace][] = rtrim($path, '/\\');
97 * Prepends a path where templates are stored.
99 * @param string $path A path where to look for templates
100 * @param string $namespace A path name
102 * @throws Twig_Error_Loader
104 public function prependPath($path, $namespace = '__main__')
106 // invalidate the cache
107 $this->cache
= array();
109 if (!is_dir($path)) {
110 throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
113 $path = rtrim($path, '/\\');
115 if (!isset($this->paths
[$namespace])) {
116 $this->paths
[$namespace][] = $path;
118 array_unshift($this->paths
[$namespace], $path);
125 public function getSource($name)
127 return file_get_contents($this->findTemplate($name));
133 public function getCacheKey($name)
135 return $this->findTemplate($name);
141 public function exists($name)
143 $name = (string) $name;
144 if (isset($this->cache
[$name])) {
149 $this->findTemplate($name);
152 } catch (Twig_Error_Loader
$exception) {
160 public function isFresh($name, $time)
162 return filemtime($this->findTemplate($name)) <= $time;
165 protected function findTemplate($name)
167 $name = (string) $name;
170 $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
172 if (isset($this->cache
[$name])) {
173 return $this->cache
[$name];
176 $this->validateName($name);
178 $namespace = '__main__';
179 if (isset($name[0]) && '@' == $name[0]) {
180 if (false === $pos = strpos($name, '/')) {
181 throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
184 $namespace = substr($name, 1, $pos - 1);
186 $name = substr($name, $pos +
1);
189 if (!isset($this->paths
[$namespace])) {
190 throw new Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
193 foreach ($this->paths
[$namespace] as $path) {
194 if (is_file($path.'/'.$name)) {
195 return $this->cache
[$name] = $path.'/'.$name;
199 throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths
[$namespace])));
202 protected function validateName($name)
204 if (false !== strpos($name, "\0")) {
205 throw new Twig_Error_Loader('A template name cannot contain NUL bytes.');
208 $name = ltrim($name, '/');
209 $parts = explode('/', $name);
211 foreach ($parts as $part) {
212 if ('..' === $part) {
214 } elseif ('.' !== $part) {
219 throw new Twig_Error_Loader(sprintf('Looks like you try to load a template outside configured directories (%s).', $name));