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