Matthias Andreas Benkard | 12a5735 | 2021-12-28 18:02:04 +0100 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of Twig. |
| 5 | * |
| 6 | * (c) 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 | namespace Twig\Loader; |
| 13 | |
| 14 | use Twig\Error\LoaderError; |
| 15 | use Twig\Source; |
| 16 | |
| 17 | /** |
| 18 | * Loads template from the filesystem. |
| 19 | * |
| 20 | * @author Fabien Potencier <fabien@symfony.com> |
| 21 | */ |
| 22 | class FilesystemLoader implements LoaderInterface |
| 23 | { |
| 24 | /** Identifier of the main namespace. */ |
| 25 | public const MAIN_NAMESPACE = '__main__'; |
| 26 | |
| 27 | protected $paths = []; |
| 28 | protected $cache = []; |
| 29 | protected $errorCache = []; |
| 30 | |
| 31 | private $rootPath; |
| 32 | |
| 33 | /** |
| 34 | * @param string|array $paths A path or an array of paths where to look for templates |
| 35 | * @param string|null $rootPath The root path common to all relative paths (null for getcwd()) |
| 36 | */ |
| 37 | public function __construct($paths = [], string $rootPath = null) |
| 38 | { |
| 39 | $this->rootPath = (null === $rootPath ? getcwd() : $rootPath).\DIRECTORY_SEPARATOR; |
| 40 | if (null !== $rootPath && false !== ($realPath = realpath($rootPath))) { |
| 41 | $this->rootPath = $realPath.\DIRECTORY_SEPARATOR; |
| 42 | } |
| 43 | |
| 44 | if ($paths) { |
| 45 | $this->setPaths($paths); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns the paths to the templates. |
| 51 | */ |
| 52 | public function getPaths(string $namespace = self::MAIN_NAMESPACE): array |
| 53 | { |
| 54 | return $this->paths[$namespace] ?? []; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Returns the path namespaces. |
| 59 | * |
| 60 | * The main namespace is always defined. |
| 61 | */ |
| 62 | public function getNamespaces(): array |
| 63 | { |
| 64 | return array_keys($this->paths); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param string|array $paths A path or an array of paths where to look for templates |
| 69 | */ |
| 70 | public function setPaths($paths, string $namespace = self::MAIN_NAMESPACE): void |
| 71 | { |
| 72 | if (!\is_array($paths)) { |
| 73 | $paths = [$paths]; |
| 74 | } |
| 75 | |
| 76 | $this->paths[$namespace] = []; |
| 77 | foreach ($paths as $path) { |
| 78 | $this->addPath($path, $namespace); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @throws LoaderError |
| 84 | */ |
| 85 | public function addPath(string $path, string $namespace = self::MAIN_NAMESPACE): void |
| 86 | { |
| 87 | // invalidate the cache |
| 88 | $this->cache = $this->errorCache = []; |
| 89 | |
| 90 | $checkPath = $this->isAbsolutePath($path) ? $path : $this->rootPath.$path; |
| 91 | if (!is_dir($checkPath)) { |
| 92 | throw new LoaderError(sprintf('The "%s" directory does not exist ("%s").', $path, $checkPath)); |
| 93 | } |
| 94 | |
| 95 | $this->paths[$namespace][] = rtrim($path, '/\\'); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @throws LoaderError |
| 100 | */ |
| 101 | public function prependPath(string $path, string $namespace = self::MAIN_NAMESPACE): void |
| 102 | { |
| 103 | // invalidate the cache |
| 104 | $this->cache = $this->errorCache = []; |
| 105 | |
| 106 | $checkPath = $this->isAbsolutePath($path) ? $path : $this->rootPath.$path; |
| 107 | if (!is_dir($checkPath)) { |
| 108 | throw new LoaderError(sprintf('The "%s" directory does not exist ("%s").', $path, $checkPath)); |
| 109 | } |
| 110 | |
| 111 | $path = rtrim($path, '/\\'); |
| 112 | |
| 113 | if (!isset($this->paths[$namespace])) { |
| 114 | $this->paths[$namespace][] = $path; |
| 115 | } else { |
| 116 | array_unshift($this->paths[$namespace], $path); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | public function getSourceContext(string $name): Source |
| 121 | { |
| 122 | if (null === $path = $this->findTemplate($name)) { |
| 123 | return new Source('', $name, ''); |
| 124 | } |
| 125 | |
| 126 | return new Source(file_get_contents($path), $name, $path); |
| 127 | } |
| 128 | |
| 129 | public function getCacheKey(string $name): string |
| 130 | { |
| 131 | if (null === $path = $this->findTemplate($name)) { |
| 132 | return ''; |
| 133 | } |
| 134 | $len = \strlen($this->rootPath); |
| 135 | if (0 === strncmp($this->rootPath, $path, $len)) { |
| 136 | return substr($path, $len); |
| 137 | } |
| 138 | |
| 139 | return $path; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @return bool |
| 144 | */ |
| 145 | public function exists(string $name) |
| 146 | { |
| 147 | $name = $this->normalizeName($name); |
| 148 | |
| 149 | if (isset($this->cache[$name])) { |
| 150 | return true; |
| 151 | } |
| 152 | |
| 153 | return null !== $this->findTemplate($name, false); |
| 154 | } |
| 155 | |
| 156 | public function isFresh(string $name, int $time): bool |
| 157 | { |
| 158 | // false support to be removed in 3.0 |
| 159 | if (null === $path = $this->findTemplate($name)) { |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | return filemtime($path) < $time; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * @return string|null |
| 168 | */ |
| 169 | protected function findTemplate(string $name, bool $throw = true) |
| 170 | { |
| 171 | $name = $this->normalizeName($name); |
| 172 | |
| 173 | if (isset($this->cache[$name])) { |
| 174 | return $this->cache[$name]; |
| 175 | } |
| 176 | |
| 177 | if (isset($this->errorCache[$name])) { |
| 178 | if (!$throw) { |
| 179 | return null; |
| 180 | } |
| 181 | |
| 182 | throw new LoaderError($this->errorCache[$name]); |
| 183 | } |
| 184 | |
| 185 | try { |
| 186 | $this->validateName($name); |
| 187 | |
| 188 | list($namespace, $shortname) = $this->parseName($name); |
| 189 | } catch (LoaderError $e) { |
| 190 | if (!$throw) { |
| 191 | return null; |
| 192 | } |
| 193 | |
| 194 | throw $e; |
| 195 | } |
| 196 | |
| 197 | if (!isset($this->paths[$namespace])) { |
| 198 | $this->errorCache[$name] = sprintf('There are no registered paths for namespace "%s".', $namespace); |
| 199 | |
| 200 | if (!$throw) { |
| 201 | return null; |
| 202 | } |
| 203 | |
| 204 | throw new LoaderError($this->errorCache[$name]); |
| 205 | } |
| 206 | |
| 207 | foreach ($this->paths[$namespace] as $path) { |
| 208 | if (!$this->isAbsolutePath($path)) { |
| 209 | $path = $this->rootPath.$path; |
| 210 | } |
| 211 | |
| 212 | if (is_file($path.'/'.$shortname)) { |
| 213 | if (false !== $realpath = realpath($path.'/'.$shortname)) { |
| 214 | return $this->cache[$name] = $realpath; |
| 215 | } |
| 216 | |
| 217 | return $this->cache[$name] = $path.'/'.$shortname; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | $this->errorCache[$name] = sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])); |
| 222 | |
| 223 | if (!$throw) { |
| 224 | return null; |
| 225 | } |
| 226 | |
| 227 | throw new LoaderError($this->errorCache[$name]); |
| 228 | } |
| 229 | |
| 230 | private function normalizeName(string $name): string |
| 231 | { |
| 232 | return preg_replace('#/{2,}#', '/', str_replace('\\', '/', $name)); |
| 233 | } |
| 234 | |
| 235 | private function parseName(string $name, string $default = self::MAIN_NAMESPACE): array |
| 236 | { |
| 237 | if (isset($name[0]) && '@' == $name[0]) { |
| 238 | if (false === $pos = strpos($name, '/')) { |
| 239 | throw new LoaderError(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name)); |
| 240 | } |
| 241 | |
| 242 | $namespace = substr($name, 1, $pos - 1); |
| 243 | $shortname = substr($name, $pos + 1); |
| 244 | |
| 245 | return [$namespace, $shortname]; |
| 246 | } |
| 247 | |
| 248 | return [$default, $name]; |
| 249 | } |
| 250 | |
| 251 | private function validateName(string $name): void |
| 252 | { |
| 253 | if (false !== strpos($name, "\0")) { |
| 254 | throw new LoaderError('A template name cannot contain NUL bytes.'); |
| 255 | } |
| 256 | |
| 257 | $name = ltrim($name, '/'); |
| 258 | $parts = explode('/', $name); |
| 259 | $level = 0; |
| 260 | foreach ($parts as $part) { |
| 261 | if ('..' === $part) { |
| 262 | --$level; |
| 263 | } elseif ('.' !== $part) { |
| 264 | ++$level; |
| 265 | } |
| 266 | |
| 267 | if ($level < 0) { |
| 268 | throw new LoaderError(sprintf('Looks like you try to load a template outside configured directories (%s).', $name)); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | private function isAbsolutePath(string $file): bool |
| 274 | { |
| 275 | return strspn($file, '/\\', 0, 1) |
| 276 | || (\strlen($file) > 3 && ctype_alpha($file[0]) |
| 277 | && ':' === $file[1] |
| 278 | && strspn($file, '/\\', 2, 1) |
| 279 | ) |
| 280 | || null !== parse_url($file, \PHP_URL_SCHEME) |
| 281 | ; |
| 282 | } |
| 283 | } |