Chris@0: . Chris@0: */ Chris@0: Chris@0: namespace Doctrine\Common\Cache; Chris@0: Chris@0: /** Chris@0: * Base file cache driver. Chris@0: * Chris@0: * @since 2.3 Chris@0: * @author Fabio B. Silva Chris@0: * @author Tobias Schultze Chris@0: */ Chris@0: abstract class FileCache extends CacheProvider Chris@0: { Chris@0: /** Chris@0: * The cache directory. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: protected $directory; Chris@0: Chris@0: /** Chris@0: * The cache file extension. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: private $extension; Chris@0: Chris@0: /** Chris@0: * @var int Chris@0: */ Chris@0: private $umask; Chris@0: Chris@0: /** Chris@0: * @var int Chris@0: */ Chris@0: private $directoryStringLength; Chris@0: Chris@0: /** Chris@0: * @var int Chris@0: */ Chris@0: private $extensionStringLength; Chris@0: Chris@0: /** Chris@0: * @var bool Chris@0: */ Chris@0: private $isRunningOnWindows; Chris@0: Chris@0: /** Chris@0: * Constructor. Chris@0: * Chris@0: * @param string $directory The cache directory. Chris@0: * @param string $extension The cache file extension. Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@0: */ Chris@0: public function __construct($directory, $extension = '', $umask = 0002) Chris@0: { Chris@0: // YES, this needs to be *before* createPathIfNeeded() Chris@0: if ( ! is_int($umask)) { Chris@0: throw new \InvalidArgumentException(sprintf( Chris@0: 'The umask parameter is required to be integer, was: %s', Chris@0: gettype($umask) Chris@0: )); Chris@0: } Chris@0: $this->umask = $umask; Chris@0: Chris@0: if ( ! $this->createPathIfNeeded($directory)) { Chris@0: throw new \InvalidArgumentException(sprintf( Chris@0: 'The directory "%s" does not exist and could not be created.', Chris@0: $directory Chris@0: )); Chris@0: } Chris@0: Chris@0: if ( ! is_writable($directory)) { Chris@0: throw new \InvalidArgumentException(sprintf( Chris@0: 'The directory "%s" is not writable.', Chris@0: $directory Chris@0: )); Chris@0: } Chris@0: Chris@0: // YES, this needs to be *after* createPathIfNeeded() Chris@0: $this->directory = realpath($directory); Chris@0: $this->extension = (string) $extension; Chris@0: Chris@0: $this->directoryStringLength = strlen($this->directory); Chris@0: $this->extensionStringLength = strlen($this->extension); Chris@0: $this->isRunningOnWindows = defined('PHP_WINDOWS_VERSION_BUILD'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the cache directory. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getDirectory() Chris@0: { Chris@0: return $this->directory; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the cache file extension. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getExtension() Chris@0: { Chris@0: return $this->extension; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param string $id Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: protected function getFilename($id) Chris@0: { Chris@0: $hash = hash('sha256', $id); Chris@0: Chris@0: // This ensures that the filename is unique and that there are no invalid chars in it. Chris@0: if ( Chris@0: '' === $id Chris@0: || ((strlen($id) * 2 + $this->extensionStringLength) > 255) Chris@0: || ($this->isRunningOnWindows && ($this->directoryStringLength + 4 + strlen($id) * 2 + $this->extensionStringLength) > 258) Chris@0: ) { Chris@0: // Most filesystems have a limit of 255 chars for each path component. On Windows the the whole path is limited Chris@0: // to 260 chars (including terminating null char). Using long UNC ("\\?\" prefix) does not work with the PHP API. Chris@0: // And there is a bug in PHP (https://bugs.php.net/bug.php?id=70943) with path lengths of 259. Chris@0: // So if the id in hex representation would surpass the limit, we use the hash instead. The prefix prevents Chris@0: // collisions between the hash and bin2hex. Chris@0: $filename = '_' . $hash; Chris@0: } else { Chris@0: $filename = bin2hex($id); Chris@0: } Chris@0: Chris@0: return $this->directory Chris@0: . DIRECTORY_SEPARATOR Chris@0: . substr($hash, 0, 2) Chris@0: . DIRECTORY_SEPARATOR Chris@0: . $filename Chris@0: . $this->extension; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doDelete($id) Chris@0: { Chris@0: $filename = $this->getFilename($id); Chris@0: Chris@0: return @unlink($filename) || ! file_exists($filename); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doFlush() Chris@0: { Chris@0: foreach ($this->getIterator() as $name => $file) { Chris@0: if ($file->isDir()) { Chris@0: // Remove the intermediate directories which have been created to balance the tree. It only takes effect Chris@0: // if the directory is empty. If several caches share the same directory but with different file extensions, Chris@0: // the other ones are not removed. Chris@0: @rmdir($name); Chris@0: } elseif ($this->isFilenameEndingWithExtension($name)) { Chris@0: // If an extension is set, only remove files which end with the given extension. Chris@0: // If no extension is set, we have no other choice than removing everything. Chris@0: @unlink($name); Chris@0: } Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: protected function doGetStats() Chris@0: { Chris@0: $usage = 0; Chris@0: foreach ($this->getIterator() as $name => $file) { Chris@0: if (! $file->isDir() && $this->isFilenameEndingWithExtension($name)) { Chris@0: $usage += $file->getSize(); Chris@0: } Chris@0: } Chris@0: Chris@0: $free = disk_free_space($this->directory); Chris@0: Chris@0: return array( Chris@0: Cache::STATS_HITS => null, Chris@0: Cache::STATS_MISSES => null, Chris@0: Cache::STATS_UPTIME => null, Chris@0: Cache::STATS_MEMORY_USAGE => $usage, Chris@0: Cache::STATS_MEMORY_AVAILABLE => $free, Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Create path if needed. Chris@0: * Chris@0: * @param string $path Chris@0: * @return bool TRUE on success or if path already exists, FALSE if path cannot be created. Chris@0: */ Chris@0: private function createPathIfNeeded($path) Chris@0: { Chris@0: if ( ! is_dir($path)) { Chris@0: if (false === @mkdir($path, 0777 & (~$this->umask), true) && !is_dir($path)) { Chris@0: return false; Chris@0: } Chris@0: } Chris@0: Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Writes a string content to file in an atomic way. Chris@0: * Chris@0: * @param string $filename Path to the file where to write the data. Chris@0: * @param string $content The content to write Chris@0: * Chris@0: * @return bool TRUE on success, FALSE if path cannot be created, if path is not writable or an any other error. Chris@0: */ Chris@0: protected function writeFile($filename, $content) Chris@0: { Chris@0: $filepath = pathinfo($filename, PATHINFO_DIRNAME); Chris@0: Chris@0: if ( ! $this->createPathIfNeeded($filepath)) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: if ( ! is_writable($filepath)) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: $tmpFile = tempnam($filepath, 'swap'); Chris@0: @chmod($tmpFile, 0666 & (~$this->umask)); Chris@0: Chris@0: if (file_put_contents($tmpFile, $content) !== false) { Chris@0: if (@rename($tmpFile, $filename)) { Chris@0: return true; Chris@0: } Chris@0: Chris@0: @unlink($tmpFile); Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return \Iterator Chris@0: */ Chris@0: private function getIterator() Chris@0: { Chris@0: return new \RecursiveIteratorIterator( Chris@0: new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS), Chris@0: \RecursiveIteratorIterator::CHILD_FIRST Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param string $name The filename Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: private function isFilenameEndingWithExtension($name) Chris@0: { Chris@0: return '' === $this->extension Chris@0: || strrpos($name, $this->extension) === (strlen($name) - $this->extensionStringLength); Chris@0: } Chris@0: }