PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/ineszribi/SmartBookStoreWeb
PHP | 239 lines | 148 code | 31 blank | 60 comment | 14 complexity | 8376cad781e3e6186611d06d9b4f4186 MD5 | raw file
Possible License(s): BSD-3-Clause
  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. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderInterface
  16. {
  17. /** Identifier of the main namespace. */
  18. const MAIN_NAMESPACE = '__main__';
  19. protected $paths = array();
  20. protected $cache = array();
  21. /**
  22. * Constructor.
  23. *
  24. * @param string|array $paths A path or an array of paths where to look for templates
  25. */
  26. public function __construct($paths = array())
  27. {
  28. if ($paths) {
  29. $this->setPaths($paths);
  30. }
  31. }
  32. /**
  33. * Returns the paths to the templates.
  34. *
  35. * @param string $namespace A path namespace
  36. *
  37. * @return array The array of paths where to look for templates
  38. */
  39. public function getPaths($namespace = self::MAIN_NAMESPACE)
  40. {
  41. return isset($this->paths[$namespace]) ? $this->paths[$namespace] : array();
  42. }
  43. /**
  44. * Returns the path namespaces.
  45. *
  46. * The main namespace is always defined.
  47. *
  48. * @return array The array of defined namespaces
  49. */
  50. public function getNamespaces()
  51. {
  52. return array_keys($this->paths);
  53. }
  54. /**
  55. * Sets the paths where templates are stored.
  56. *
  57. * @param string|array $paths A path or an array of paths where to look for templates
  58. * @param string $namespace A path namespace
  59. */
  60. public function setPaths($paths, $namespace = self::MAIN_NAMESPACE)
  61. {
  62. if (!is_array($paths)) {
  63. $paths = array($paths);
  64. }
  65. $this->paths[$namespace] = array();
  66. foreach ($paths as $path) {
  67. $this->addPath($path, $namespace);
  68. }
  69. }
  70. /**
  71. * Adds a path where templates are stored.
  72. *
  73. * @param string $path A path where to look for templates
  74. * @param string $namespace A path name
  75. *
  76. * @throws Twig_Error_Loader
  77. */
  78. public function addPath($path, $namespace = self::MAIN_NAMESPACE)
  79. {
  80. // invalidate the cache
  81. $this->cache = array();
  82. if (!is_dir($path)) {
  83. throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
  84. }
  85. $this->paths[$namespace][] = rtrim($path, '/\\');
  86. }
  87. /**
  88. * Prepends a path where templates are stored.
  89. *
  90. * @param string $path A path where to look for templates
  91. * @param string $namespace A path name
  92. *
  93. * @throws Twig_Error_Loader
  94. */
  95. public function prependPath($path, $namespace = self::MAIN_NAMESPACE)
  96. {
  97. // invalidate the cache
  98. $this->cache = array();
  99. if (!is_dir($path)) {
  100. throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
  101. }
  102. $path = rtrim($path, '/\\');
  103. if (!isset($this->paths[$namespace])) {
  104. $this->paths[$namespace][] = $path;
  105. } else {
  106. array_unshift($this->paths[$namespace], $path);
  107. }
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function getSource($name)
  113. {
  114. return file_get_contents($this->findTemplate($name));
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function getCacheKey($name)
  120. {
  121. return $this->findTemplate($name);
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function exists($name)
  127. {
  128. $name = $this->normalizeName($name);
  129. if (isset($this->cache[$name])) {
  130. return true;
  131. }
  132. try {
  133. $this->findTemplate($name);
  134. return true;
  135. } catch (Twig_Error_Loader $exception) {
  136. return false;
  137. }
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function isFresh($name, $time)
  143. {
  144. return filemtime($this->findTemplate($name)) <= $time;
  145. }
  146. protected function findTemplate($name)
  147. {
  148. $name = $this->normalizeName($name);
  149. if (isset($this->cache[$name])) {
  150. return $this->cache[$name];
  151. }
  152. $this->validateName($name);
  153. list($namespace, $shortname) = $this->parseName($name);
  154. if (!isset($this->paths[$namespace])) {
  155. throw new Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
  156. }
  157. foreach ($this->paths[$namespace] as $path) {
  158. if (false !== $realpath = realpath($path.'/'.$shortname)) {
  159. return $this->cache[$name] = $realpath;
  160. }
  161. if (is_file($path.'/'.$shortname)) {
  162. return $this->cache[$name] = $path.'/'.$shortname;
  163. }
  164. }
  165. throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
  166. }
  167. protected function parseName($name, $default = self::MAIN_NAMESPACE)
  168. {
  169. if (isset($name[0]) && '@' == $name[0]) {
  170. if (false === $pos = strpos($name, '/')) {
  171. throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name));
  172. }
  173. $namespace = substr($name, 1, $pos - 1);
  174. $shortname = substr($name, $pos + 1);
  175. return array($namespace, $shortname);
  176. }
  177. return array($default, $name);
  178. }
  179. protected function normalizeName($name)
  180. {
  181. return preg_replace('#/{2,}#', '/', strtr((string) $name, '\\', '/'));
  182. }
  183. protected function validateName($name)
  184. {
  185. if (false !== strpos($name, "\0")) {
  186. throw new Twig_Error_Loader('A template name cannot contain NUL bytes.');
  187. }
  188. $name = ltrim($name, '/');
  189. $parts = explode('/', $name);
  190. $level = 0;
  191. foreach ($parts as $part) {
  192. if ('..' === $part) {
  193. --$level;
  194. } elseif ('.' !== $part) {
  195. ++$level;
  196. }
  197. if ($level < 0) {
  198. throw new Twig_Error_Loader(sprintf('Looks like you try to load a template outside configured directories (%s).', $name));
  199. }
  200. }
  201. }
  202. }