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.potencier@symfony-project.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 | // invalidate the cache |
---|
51 | $this->cache = array(); |
---|
52 | |
---|
53 | if (!is_array($paths)) { |
---|
54 | $paths = array($paths); |
---|
55 | } |
---|
56 | |
---|
57 | $this->paths = array(); |
---|
58 | foreach ($paths as $path) { |
---|
59 | if (!is_dir($path)) { |
---|
60 | throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path)); |
---|
61 | } |
---|
62 | |
---|
63 | $this->paths[] = $path; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Gets the source code of a template, given its name. |
---|
69 | * |
---|
70 | * @param string $name The name of the template to load |
---|
71 | * |
---|
72 | * @return string The template source code |
---|
73 | */ |
---|
74 | public function getSource($name) |
---|
75 | { |
---|
76 | return file_get_contents($this->findTemplate($name)); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Gets the cache key to use for the cache for a given template name. |
---|
81 | * |
---|
82 | * @param string $name The name of the template to load |
---|
83 | * |
---|
84 | * @return string The cache key |
---|
85 | */ |
---|
86 | public function getCacheKey($name) |
---|
87 | { |
---|
88 | return $this->findTemplate($name); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Returns true if the template is still fresh. |
---|
93 | * |
---|
94 | * @param string $name The template name |
---|
95 | * @param timestamp $time The last modification time of the cached template |
---|
96 | */ |
---|
97 | public function isFresh($name, $time) |
---|
98 | { |
---|
99 | return filemtime($this->findTemplate($name)) < $time; |
---|
100 | } |
---|
101 | |
---|
102 | protected function findTemplate($name) |
---|
103 | { |
---|
104 | // normalize name |
---|
105 | $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')); |
---|
106 | |
---|
107 | if (isset($this->cache[$name])) { |
---|
108 | return $this->cache[$name]; |
---|
109 | } |
---|
110 | |
---|
111 | $this->validateName($name); |
---|
112 | |
---|
113 | foreach ($this->paths as $path) { |
---|
114 | if (is_file($path.'/'.$name)) { |
---|
115 | return $this->cache[$name] = $path.'/'.$name; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths))); |
---|
120 | } |
---|
121 | |
---|
122 | protected function validateName($name) |
---|
123 | { |
---|
124 | $parts = explode('/', $name); |
---|
125 | $level = 0; |
---|
126 | foreach ($parts as $part) { |
---|
127 | if ('..' === $part) { |
---|
128 | --$level; |
---|
129 | } elseif ('.' !== $part) { |
---|
130 | ++$level; |
---|
131 | } |
---|
132 | |
---|
133 | if ($level < 0) { |
---|
134 | throw new Twig_Error_Loader(sprintf('Looks like you try to load a template outside configured directories (%s).', $name)); |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|