|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of Twig. |
|
5 * |
|
6 * (c) 2009 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 template from the filesystem. |
|
14 * |
|
15 * @package twig |
|
16 * @author Fabien Potencier <fabien@symfony.com> |
|
17 */ |
|
18 class Twig_Loader_Filesystem implements Twig_LoaderInterface |
|
19 { |
|
20 protected $paths; |
|
21 protected $cache; |
|
22 |
|
23 /** |
|
24 * Constructor. |
|
25 * |
|
26 * @param string|array $paths A path or an array of paths where to look for templates |
|
27 */ |
|
28 public function __construct($paths) |
|
29 { |
|
30 $this->setPaths($paths); |
|
31 } |
|
32 |
|
33 /** |
|
34 * Returns the paths to the templates. |
|
35 * |
|
36 * @return array The array of paths where to look for templates |
|
37 */ |
|
38 public function getPaths() |
|
39 { |
|
40 return $this->paths; |
|
41 } |
|
42 |
|
43 /** |
|
44 * Sets the paths where templates are stored. |
|
45 * |
|
46 * @param string|array $paths A path or an array of paths where to look for templates |
|
47 */ |
|
48 public function setPaths($paths) |
|
49 { |
|
50 if (!is_array($paths)) { |
|
51 $paths = array($paths); |
|
52 } |
|
53 |
|
54 $this->paths = array(); |
|
55 foreach ($paths as $path) { |
|
56 $this->addPath($path); |
|
57 } |
|
58 } |
|
59 |
|
60 /** |
|
61 * Adds a path where templates are stored. |
|
62 * |
|
63 * @param string $path A path where to look for templates |
|
64 */ |
|
65 public function addPath($path) |
|
66 { |
|
67 // invalidate the cache |
|
68 $this->cache = array(); |
|
69 |
|
70 if (!is_dir($path)) { |
|
71 throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path)); |
|
72 } |
|
73 |
|
74 $this->paths[] = $path; |
|
75 } |
|
76 |
|
77 /** |
|
78 * Gets the source code of a template, given its name. |
|
79 * |
|
80 * @param string $name The name of the template to load |
|
81 * |
|
82 * @return string The template source code |
|
83 */ |
|
84 public function getSource($name) |
|
85 { |
|
86 return file_get_contents($this->findTemplate($name)); |
|
87 } |
|
88 |
|
89 /** |
|
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 |
|
95 */ |
|
96 public function getCacheKey($name) |
|
97 { |
|
98 return $this->findTemplate($name); |
|
99 } |
|
100 |
|
101 /** |
|
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 |
|
106 */ |
|
107 public function isFresh($name, $time) |
|
108 { |
|
109 return filemtime($this->findTemplate($name)) < $time; |
|
110 } |
|
111 |
|
112 protected function findTemplate($name) |
|
113 { |
|
114 // normalize name |
|
115 $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')); |
|
116 |
|
117 if (isset($this->cache[$name])) { |
|
118 return $this->cache[$name]; |
|
119 } |
|
120 |
|
121 $this->validateName($name); |
|
122 |
|
123 foreach ($this->paths as $path) { |
|
124 if (is_file($path.'/'.$name)) { |
|
125 return $this->cache[$name] = $path.'/'.$name; |
|
126 } |
|
127 } |
|
128 |
|
129 throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths))); |
|
130 } |
|
131 |
|
132 protected function validateName($name) |
|
133 { |
|
134 if (false !== strpos($name, "\0")) { |
|
135 throw new Twig_Error_Loader('A template name cannot contain NUL bytes.'); |
|
136 } |
|
137 |
|
138 $parts = explode('/', $name); |
|
139 $level = 0; |
|
140 foreach ($parts as $part) { |
|
141 if ('..' === $part) { |
|
142 --$level; |
|
143 } elseif ('.' !== $part) { |
|
144 ++$level; |
|
145 } |
|
146 |
|
147 if ($level < 0) { |
|
148 throw new Twig_Error_Loader(sprintf('Looks like you try to load a template outside configured directories (%s).', $name)); |
|
149 } |
|
150 } |
|
151 } |
|
152 } |