/vendor/twig/twig/lib/Twig/Loader/Filesystem.php

https://bitbucket.org/prauscher/att · PHP · 221 lines · 118 code · 33 blank · 70 comment · 16 complexity · 18928b319d71a493dd6c11ebc755d647 MD5 · raw file

  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, Twig_ExistsLoaderInterface
  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. * @throws Twig_Error_Loader
  74. */
  75. public function addPath($path, $namespace = '__main__')
  76. {
  77. // invalidate the cache
  78. $this->cache = array();
  79. if (!is_dir($path)) {
  80. throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
  81. }
  82. $this->paths[$namespace][] = rtrim($path, '/\\');
  83. }
  84. /**
  85. * Prepends a path where templates are stored.
  86. *
  87. * @param string $path A path where to look for templates
  88. * @param string $namespace A path name
  89. *
  90. * @throws Twig_Error_Loader
  91. */
  92. public function prependPath($path, $namespace = '__main__')
  93. {
  94. // invalidate the cache
  95. $this->cache = array();
  96. if (!is_dir($path)) {
  97. throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
  98. }
  99. $path = rtrim($path, '/\\');
  100. if (!isset($this->paths[$namespace])) {
  101. $this->paths[$namespace][] = $path;
  102. } else {
  103. array_unshift($this->paths[$namespace], $path);
  104. }
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public function getSource($name)
  110. {
  111. return file_get_contents($this->findTemplate($name));
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function getCacheKey($name)
  117. {
  118. return $this->findTemplate($name);
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function exists($name)
  124. {
  125. $name = (string) $name;
  126. if (isset($this->cache[$name])) {
  127. return true;
  128. }
  129. try {
  130. $this->findTemplate($name);
  131. return true;
  132. } catch (Twig_Error_Loader $exception) {
  133. return false;
  134. }
  135. }
  136. /**
  137. * {@inheritdoc}
  138. */
  139. public function isFresh($name, $time)
  140. {
  141. return filemtime($this->findTemplate($name)) <= $time;
  142. }
  143. protected function findTemplate($name)
  144. {
  145. $name = (string) $name;
  146. // normalize name
  147. $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));
  148. if (isset($this->cache[$name])) {
  149. return $this->cache[$name];
  150. }
  151. $this->validateName($name);
  152. $namespace = '__main__';
  153. if (isset($name[0]) && '@' == $name[0]) {
  154. if (false === $pos = strpos($name, '/')) {
  155. throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
  156. }
  157. $namespace = substr($name, 1, $pos - 1);
  158. $name = substr($name, $pos + 1);
  159. }
  160. if (!isset($this->paths[$namespace])) {
  161. throw new Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
  162. }
  163. foreach ($this->paths[$namespace] as $path) {
  164. if (is_file($path.'/'.$name)) {
  165. return $this->cache[$name] = $path.'/'.$name;
  166. }
  167. }
  168. throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
  169. }
  170. protected function validateName($name)
  171. {
  172. if (false !== strpos($name, "\0")) {
  173. throw new Twig_Error_Loader('A template name cannot contain NUL bytes.');
  174. }
  175. $parts = explode('/', $name);
  176. $level = 0;
  177. foreach ($parts as $part) {
  178. if ('..' === $part) {
  179. --$level;
  180. } elseif ('.' !== $part) {
  181. ++$level;
  182. }
  183. if ($level < 0) {
  184. throw new Twig_Error_Loader(sprintf('Looks like you try to load a template outside configured directories (%s).', $name));
  185. }
  186. }
  187. }
  188. }