| 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\Cache; |
| 12 |
|
| 13 |
/** |
| 14 |
* Implements a cache on the filesystem. |
| 15 |
* |
| 16 |
* @author Andrew Tch <andrew@noop.lv> |
| 17 |
*/ |
| 18 |
class FilesystemCache implements CacheInterface |
| 19 |
{ |
| 20 |
public const FORCE_BYTECODE_INVALIDATION = 1; |
| 21 |
private $directory; |
| 22 |
private $options; |
| 23 |
public function __construct(string $directory, int $options = 0) |
| 24 |
{ |
| 25 |
$this->directory = \rtrim($directory, '\\/') . '/'; |
| 26 |
$this->options = $options; |
| 27 |
} |
| 28 |
public function generateKey(string $name, string $className) : string |
| 29 |
{ |
| 30 |
$hash = \hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $className); |
| 31 |
return $this->directory . $hash[0] . $hash[1] . '/' . $hash . '.php'; |
| 32 |
} |
| 33 |
public function load(string $key) : void |
| 34 |
{ |
| 35 |
if (\is_file($key)) { |
| 36 |
@(include_once $key); |
| 37 |
} |
| 38 |
} |
| 39 |
public function write(string $key, string $content) : void |
| 40 |
{ |
| 41 |
$dir = \dirname($key); |
| 42 |
if (!\is_dir($dir)) { |
| 43 |
if (\false === @\mkdir($dir, 0777, \true)) { |
| 44 |
\clearstatcache(\true, $dir); |
| 45 |
if (!\is_dir($dir)) { |
| 46 |
throw new \RuntimeException(\sprintf('Unable to create the cache directory (%s).', $dir)); |
| 47 |
} |
| 48 |
} |
| 49 |
} elseif (!\is_writable($dir)) { |
| 50 |
throw new \RuntimeException(\sprintf('Unable to write in the cache directory (%s).', $dir)); |
| 51 |
} |
| 52 |
$tmpFile = \tempnam($dir, \basename($key)); |
| 53 |
if (\false !== @\file_put_contents($tmpFile, $content) && @\rename($tmpFile, $key)) { |
| 54 |
@\chmod($key, 0666 & ~\umask()); |
| 55 |
if (self::FORCE_BYTECODE_INVALIDATION == ($this->options & self::FORCE_BYTECODE_INVALIDATION)) { |
| 56 |
// Compile cached file into bytecode cache |
| 57 |
if (\function_exists('opcache_invalidate') && \filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN)) { |
| 58 |
@\opcache_invalidate($key, \true); |
| 59 |
} elseif (\function_exists('apc_compile_file')) { |
| 60 |
\apc_compile_file($key); |
| 61 |
} |
| 62 |
} |
| 63 |
return; |
| 64 |
} |
| 65 |
throw new \RuntimeException(\sprintf('Failed to write cache file "%s".', $key)); |
| 66 |
} |
| 67 |
public function getTimestamp(string $key) : int |
| 68 |
{ |
| 69 |
if (!\is_file($key)) { |
| 70 |
return 0; |
| 71 |
} |
| 72 |
return (int) @\filemtime($key); |
| 73 |
} |
| 74 |
} |
| 75 |
|