class FilesystemCache
Implements a cache on the filesystem.
@author Andrew Tch <andrew@noop.lv>
Hierarchy
- class \Twig\Cache\FilesystemCache implements \Twig\Cache\CacheInterface, \Twig\Cache\RemovableCacheInterface
Expanded class hierarchy of FilesystemCache
1 file declares its use of FilesystemCache
- Environment.php in vendor/
twig/ twig/ src/ Environment.php
File
-
vendor/
twig/ twig/ src/ Cache/ FilesystemCache.php, line 19
Namespace
Twig\CacheView source
class FilesystemCache implements CacheInterface, RemovableCacheInterface {
public const FORCE_BYTECODE_INVALIDATION = 1;
private $directory;
private $options;
public function __construct(string $directory, int $options = 0) {
$this->directory = rtrim($directory, '\\/') . '/';
$this->options = $options;
}
public function generateKey(string $name, string $className) : string {
$hash = hash(\PHP_VERSION_ID < 80100 ? 'sha256' : 'xxh128', $className);
return $this->directory . $hash[0] . $hash[1] . '/' . $hash . '.php';
}
public function load(string $key) : void {
if (is_file($key)) {
@(include_once $key);
}
}
public function write(string $key, string $content) : void {
$dir = \dirname($key);
if (!is_dir($dir)) {
if (false === @mkdir($dir, 0777, true)) {
clearstatcache(true, $dir);
if (!is_dir($dir)) {
throw new \RuntimeException(\sprintf('Unable to create the cache directory (%s).', $dir));
}
}
}
elseif (!is_writable($dir)) {
throw new \RuntimeException(\sprintf('Unable to write in the cache directory (%s).', $dir));
}
$tmpFile = tempnam($dir, basename($key));
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $key)) {
@chmod($key, 0666 & ~umask());
if (self::FORCE_BYTECODE_INVALIDATION == ($this->options & self::FORCE_BYTECODE_INVALIDATION)) {
// Compile cached file into bytecode cache
if (\function_exists('opcache_invalidate') && filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN)) {
@opcache_invalidate($key, true);
}
elseif (\function_exists('apc_compile_file')) {
apc_compile_file($key);
}
}
return;
}
throw new \RuntimeException(\sprintf('Failed to write cache file "%s".', $key));
}
public function remove(string $name, string $cls) : void {
$key = $this->generateKey($name, $cls);
if (!@unlink($key) && file_exists($key)) {
throw new \RuntimeException(\sprintf('Failed to delete cache file "%s".', $key));
}
}
public function getTimestamp(string $key) : int {
if (!is_file($key)) {
return 0;
}
return (int) @filemtime($key);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
FilesystemCache::$directory | private | property | |||
FilesystemCache::$options | private | property | |||
FilesystemCache::FORCE_BYTECODE_INVALIDATION | public | constant | |||
FilesystemCache::generateKey | public | function | Generates a cache key for the given template class name. | Overrides CacheInterface::generateKey | |
FilesystemCache::getTimestamp | public | function | Returns the modification timestamp of a key. | Overrides CacheInterface::getTimestamp | |
FilesystemCache::load | public | function | Loads a template from the cache. | Overrides CacheInterface::load | |
FilesystemCache::remove | public | function | Overrides RemovableCacheInterface::remove | ||
FilesystemCache::write | public | function | Writes the compiled template to cache. | Overrides CacheInterface::write | 1 |
FilesystemCache::__construct | public | function |