Filesystem.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Loads template from the filesystem.
  12. *
  13. * @package twig
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_Loader_Filesystem implements Twig_LoaderInterface
  17. {
  18. protected $paths;
  19. protected $cache;
  20. /**
  21. * Constructor.
  22. *
  23. * @param string|array $paths A path or an array of paths where to look for templates
  24. */
  25. public function __construct($paths)
  26. {
  27. $this->setPaths($paths);
  28. }
  29. /**
  30. * Returns the paths to the templates.
  31. *
  32. * @param string $namespace A path namespace
  33. *
  34. * @return array The array of paths where to look for templates
  35. */
  36. public function getPaths($namespace = '__main__')
  37. {
  38. return isset($this->paths[$namespace]) ? $this->paths[$namespace] : array();
  39. }
  40. /**
  41. * Returns the path namespaces.
  42. *
  43. * The "__main__" namespace is always defined.
  44. *
  45. * @return array The array of defined namespaces
  46. */
  47. public function getNamespaces()
  48. {
  49. return array_keys($this->paths);
  50. }
  51. /**
  52. * Sets the paths where templates are stored.
  53. *
  54. * @param string|array $paths A path or an array of paths where to look for templates
  55. * @param string $namespace A path namespace
  56. */
  57. public function setPaths($paths, $namespace = '__main__')
  58. {
  59. if (!is_array($paths)) {
  60. $paths = array($paths);
  61. }
  62. $this->paths[$namespace] = array();
  63. foreach ($paths as $path) {
  64. $this->addPath($path, $namespace);
  65. }
  66. }
  67. /**
  68. * Adds a path where templates are stored.
  69. *
  70. * @param string $path A path where to look for templates
  71. * @param string $namespace A path name
  72. */
  73. public function addPath($path, $namespace = '__main__')
  74. {
  75. // invalidate the cache
  76. $this->cache = array();
  77. if (!is_dir($path)) {
  78. throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
  79. }
  80. $this->paths[$namespace][] = rtrim($path, '/\\');
  81. }
  82. /**
  83. * Prepends a path where templates are stored.
  84. *
  85. * @param string $path A path where to look for templates
  86. * @param string $namespace A path name
  87. */
  88. public function prependPath($path, $namespace = '__main__')
  89. {
  90. // invalidate the cache
  91. $this->cache = array();
  92. if (!is_dir($path)) {
  93. throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
  94. }
  95. $path = rtrim($path, '/\\');
  96. if (!isset($this->paths[$namespace])) {
  97. $this->paths[$namespace][] = $path;
  98. } else {
  99. array_unshift($this->paths[$namespace], $path);
  100. }
  101. }
  102. /**
  103. * Gets the source code of a template, given its name.
  104. *
  105. * @param string $name The name of the template to load
  106. *
  107. * @return string The template source code
  108. */
  109. public function getSource($name)
  110. {
  111. return file_get_contents($this->findTemplate($name));
  112. }
  113. /**
  114. * Gets the cache key to use for the cache for a given template name.
  115. *
  116. * @param string $name The name of the template to load
  117. *
  118. * @return string The cache key
  119. */
  120. public function getCacheKey($name)
  121. {
  122. return $this->findTemplate($name);
  123. }
  124. /**
  125. * Returns true if the template is still fresh.
  126. *
  127. * @param string $name The template name
  128. * @param timestamp $time The last modification time of the cached template
  129. */
  130. public function isFresh($name, $time)
  131. {
  132. return filemtime($this->findTemplate($name)) <= $time;
  133. }
  134. protected function findTemplate($name)
  135. {
  136. // normalize name
  137. $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
  138. if (isset($this->cache[$name])) {
  139. return $this->cache[$name];
  140. }
  141. $this->validateName($name);
  142. $namespace = '__main__';
  143. if (isset($name[0]) && '@' == $name[0]) {
  144. if (false === $pos = strpos($name, '/')) {
  145. throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
  146. }
  147. $namespace = substr($name, 1, $pos - 1);
  148. $name = substr($name, $pos + 1);
  149. }
  150. if (!isset($this->paths[$namespace])) {
  151. throw new Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
  152. }
  153. foreach ($this->paths[$namespace] as $path) {
  154. if (is_file($path.'/'.$name)) {
  155. return $this->cache[$name] = $path.'/'.$name;
  156. }
  157. }
  158. throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
  159. }
  160. protected function validateName($name)
  161. {
  162. if (false !== strpos($name, "\0")) {
  163. throw new Twig_Error_Loader('A template name cannot contain NUL bytes.');
  164. }
  165. $parts = explode('/', $name);
  166. $level = 0;
  167. foreach ($parts as $part) {
  168. if ('..' === $part) {
  169. --$level;
  170. } elseif ('.' !== $part) {
  171. ++$level;
  172. }
  173. if ($level < 0) {
  174. throw new Twig_Error_Loader(sprintf('Looks like you try to load a template outside configured directories (%s).', $name));
  175. }
  176. }
  177. }
  178. }