independent-analytics
/
vendor
/
doctrine
/
cache
/
lib
/
Doctrine
/
Common
/
Cache
Last commit date
Psr6
2 years ago
ApcCache.php
2 years ago
ApcuCache.php
2 years ago
ArrayCache.php
2 years ago
Cache.php
2 years ago
CacheProvider.php
2 years ago
ChainCache.php
2 years ago
ClearableCache.php
2 years ago
CouchbaseBucketCache.php
2 years ago
CouchbaseCache.php
2 years ago
ExtMongoDBCache.php
2 years ago
FileCache.php
2 years ago
FilesystemCache.php
2 years ago
FlushableCache.php
2 years ago
InvalidCacheId.php
2 years ago
LegacyMongoDBCache.php
2 years ago
MemcacheCache.php
2 years ago
MemcachedCache.php
2 years ago
MongoDBCache.php
2 years ago
MultiDeleteCache.php
2 years ago
MultiGetCache.php
2 years ago
MultiOperationCache.php
2 years ago
MultiPutCache.php
2 years ago
PhpFileCache.php
2 years ago
PredisCache.php
2 years ago
RedisCache.php
2 years ago
SQLite3Cache.php
2 years ago
Version.php
2 years ago
VoidCache.php
2 years ago
WinCacheCache.php
2 years ago
XcacheCache.php
2 years ago
ZendDataCache.php
2 years ago
FileCache.php
228 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\Doctrine\Common\Cache; |
| 4 | |
| 5 | use FilesystemIterator; |
| 6 | use InvalidArgumentException; |
| 7 | use Iterator; |
| 8 | use RecursiveDirectoryIterator; |
| 9 | use RecursiveIteratorIterator; |
| 10 | use SplFileInfo; |
| 11 | use function bin2hex; |
| 12 | use function chmod; |
| 13 | use function defined; |
| 14 | use function disk_free_space; |
| 15 | use function file_exists; |
| 16 | use function file_put_contents; |
| 17 | use function gettype; |
| 18 | use function hash; |
| 19 | use function is_dir; |
| 20 | use function is_int; |
| 21 | use function is_writable; |
| 22 | use function mkdir; |
| 23 | use function pathinfo; |
| 24 | use function realpath; |
| 25 | use function rename; |
| 26 | use function rmdir; |
| 27 | use function sprintf; |
| 28 | use function strlen; |
| 29 | use function strrpos; |
| 30 | use function substr; |
| 31 | use function tempnam; |
| 32 | use function unlink; |
| 33 | use const DIRECTORY_SEPARATOR; |
| 34 | use const PATHINFO_DIRNAME; |
| 35 | /** |
| 36 | * Base file cache driver. |
| 37 | * |
| 38 | * @deprecated Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0 |
| 39 | * @internal |
| 40 | */ |
| 41 | abstract class FileCache extends CacheProvider |
| 42 | { |
| 43 | /** |
| 44 | * The cache directory. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $directory; |
| 49 | /** |
| 50 | * The cache file extension. |
| 51 | * |
| 52 | * @var string |
| 53 | */ |
| 54 | private $extension; |
| 55 | /** @var int */ |
| 56 | private $umask; |
| 57 | /** @var int */ |
| 58 | private $directoryStringLength; |
| 59 | /** @var int */ |
| 60 | private $extensionStringLength; |
| 61 | /** @var bool */ |
| 62 | private $isRunningOnWindows; |
| 63 | /** |
| 64 | * @param string $directory The cache directory. |
| 65 | * @param string $extension The cache file extension. |
| 66 | * @param int $umask |
| 67 | * |
| 68 | * @throws InvalidArgumentException |
| 69 | */ |
| 70 | public function __construct($directory, $extension = '', $umask = 02) |
| 71 | { |
| 72 | // YES, this needs to be *before* createPathIfNeeded() |
| 73 | if (!is_int($umask)) { |
| 74 | throw new InvalidArgumentException(sprintf('The umask parameter is required to be integer, was: %s', gettype($umask))); |
| 75 | } |
| 76 | $this->umask = $umask; |
| 77 | if (!$this->createPathIfNeeded($directory)) { |
| 78 | throw new InvalidArgumentException(sprintf('The directory "%s" does not exist and could not be created.', $directory)); |
| 79 | } |
| 80 | if (!is_writable($directory)) { |
| 81 | throw new InvalidArgumentException(sprintf('The directory "%s" is not writable.', $directory)); |
| 82 | } |
| 83 | // YES, this needs to be *after* createPathIfNeeded() |
| 84 | $this->directory = realpath($directory); |
| 85 | $this->extension = (string) $extension; |
| 86 | $this->directoryStringLength = strlen($this->directory); |
| 87 | $this->extensionStringLength = strlen($this->extension); |
| 88 | $this->isRunningOnWindows = defined('PHP_WINDOWS_VERSION_BUILD'); |
| 89 | } |
| 90 | /** |
| 91 | * Gets the cache directory. |
| 92 | * |
| 93 | * @return string |
| 94 | */ |
| 95 | public function getDirectory() |
| 96 | { |
| 97 | return $this->directory; |
| 98 | } |
| 99 | /** |
| 100 | * Gets the cache file extension. |
| 101 | * |
| 102 | * @return string |
| 103 | */ |
| 104 | public function getExtension() |
| 105 | { |
| 106 | return $this->extension; |
| 107 | } |
| 108 | /** |
| 109 | * @param string $id |
| 110 | * |
| 111 | * @return string |
| 112 | */ |
| 113 | protected function getFilename($id) |
| 114 | { |
| 115 | $hash = hash('sha256', $id); |
| 116 | // This ensures that the filename is unique and that there are no invalid chars in it. |
| 117 | if ($id === '' || strlen($id) * 2 + $this->extensionStringLength > 255 || $this->isRunningOnWindows && $this->directoryStringLength + 4 + strlen($id) * 2 + $this->extensionStringLength > 258) { |
| 118 | // Most filesystems have a limit of 255 chars for each path component. On Windows the the whole path is limited |
| 119 | // to 260 chars (including terminating null char). Using long UNC ("\\?\" prefix) does not work with the PHP API. |
| 120 | // And there is a bug in PHP (https://bugs.php.net/bug.php?id=70943) with path lengths of 259. |
| 121 | // So if the id in hex representation would surpass the limit, we use the hash instead. The prefix prevents |
| 122 | // collisions between the hash and bin2hex. |
| 123 | $filename = '_' . $hash; |
| 124 | } else { |
| 125 | $filename = bin2hex($id); |
| 126 | } |
| 127 | return $this->directory . DIRECTORY_SEPARATOR . substr($hash, 0, 2) . DIRECTORY_SEPARATOR . $filename . $this->extension; |
| 128 | } |
| 129 | /** |
| 130 | * {@inheritdoc} |
| 131 | */ |
| 132 | protected function doDelete($id) |
| 133 | { |
| 134 | $filename = $this->getFilename($id); |
| 135 | return @unlink($filename) || !file_exists($filename); |
| 136 | } |
| 137 | /** |
| 138 | * {@inheritdoc} |
| 139 | */ |
| 140 | protected function doFlush() |
| 141 | { |
| 142 | foreach ($this->getIterator() as $name => $file) { |
| 143 | if ($file->isDir()) { |
| 144 | // Remove the intermediate directories which have been created to balance the tree. It only takes effect |
| 145 | // if the directory is empty. If several caches share the same directory but with different file extensions, |
| 146 | // the other ones are not removed. |
| 147 | @rmdir($name); |
| 148 | } elseif ($this->isFilenameEndingWithExtension($name)) { |
| 149 | // If an extension is set, only remove files which end with the given extension. |
| 150 | // If no extension is set, we have no other choice than removing everything. |
| 151 | @unlink($name); |
| 152 | } |
| 153 | } |
| 154 | return \true; |
| 155 | } |
| 156 | /** |
| 157 | * {@inheritdoc} |
| 158 | */ |
| 159 | protected function doGetStats() |
| 160 | { |
| 161 | $usage = 0; |
| 162 | foreach ($this->getIterator() as $name => $file) { |
| 163 | if ($file->isDir() || !$this->isFilenameEndingWithExtension($name)) { |
| 164 | continue; |
| 165 | } |
| 166 | $usage += $file->getSize(); |
| 167 | } |
| 168 | $free = disk_free_space($this->directory); |
| 169 | return [Cache::STATS_HITS => null, Cache::STATS_MISSES => null, Cache::STATS_UPTIME => null, Cache::STATS_MEMORY_USAGE => $usage, Cache::STATS_MEMORY_AVAILABLE => $free]; |
| 170 | } |
| 171 | /** |
| 172 | * Create path if needed. |
| 173 | * |
| 174 | * @return bool TRUE on success or if path already exists, FALSE if path cannot be created. |
| 175 | */ |
| 176 | private function createPathIfNeeded(string $path) : bool |
| 177 | { |
| 178 | if (!is_dir($path)) { |
| 179 | if (@mkdir($path, 0777 & ~$this->umask, \true) === \false && !is_dir($path)) { |
| 180 | return \false; |
| 181 | } |
| 182 | } |
| 183 | return \true; |
| 184 | } |
| 185 | /** |
| 186 | * Writes a string content to file in an atomic way. |
| 187 | * |
| 188 | * @param string $filename Path to the file where to write the data. |
| 189 | * @param string $content The content to write |
| 190 | * |
| 191 | * @return bool TRUE on success, FALSE if path cannot be created, if path is not writable or an any other error. |
| 192 | */ |
| 193 | protected function writeFile(string $filename, string $content) : bool |
| 194 | { |
| 195 | $filepath = pathinfo($filename, PATHINFO_DIRNAME); |
| 196 | if (!$this->createPathIfNeeded($filepath)) { |
| 197 | return \false; |
| 198 | } |
| 199 | if (!is_writable($filepath)) { |
| 200 | return \false; |
| 201 | } |
| 202 | $tmpFile = tempnam($filepath, 'swap'); |
| 203 | @chmod($tmpFile, 0666 & ~$this->umask); |
| 204 | if (file_put_contents($tmpFile, $content) !== \false) { |
| 205 | @chmod($tmpFile, 0666 & ~$this->umask); |
| 206 | if (@rename($tmpFile, $filename)) { |
| 207 | return \true; |
| 208 | } |
| 209 | @unlink($tmpFile); |
| 210 | } |
| 211 | return \false; |
| 212 | } |
| 213 | /** |
| 214 | * @return Iterator<string, SplFileInfo> |
| 215 | */ |
| 216 | private function getIterator() : Iterator |
| 217 | { |
| 218 | return new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->directory, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST); |
| 219 | } |
| 220 | /** |
| 221 | * @param string $name The filename |
| 222 | */ |
| 223 | private function isFilenameEndingWithExtension(string $name) : bool |
| 224 | { |
| 225 | return $this->extension === '' || strrpos($name, $this->extension) === strlen($name) - $this->extensionStringLength; |
| 226 | } |
| 227 | } |
| 228 |