/** * Force deletion on a cached resource. * * @param {string} $key Identifier of target cache. * @param {?string} $hash Target revision to delete, all revisions will be deleted if omitted. */ public static function delete($key, $hash = '*') { $res = self::resolve($key, $hash); // Skip the delete if nothing is found. if ($res === null) { return; } if ($res->isFile()) { // Remove target revision(s). if (!$res->isWritable()) { Log::warning('Target file is not writable, deletion skipped.'); } else { $path = $res->getRealPath(); unlink($path); $path = dirname($path); // Remove the directory if empty. $res = new \RecursiveDirectoryIterator($path, \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS); if ($res->isDir() && !$res->hasChildren()) { rmdir($path); } } } else { if ($res->isDir()) { $cacheDirectory = $res->getRealPath(); foreach ($res as $file) { if ($file->isFile()) { unlink($file->getRealPath()); } else { if ($file->isDir()) { rmdir($file->getRealPath()); } } } rmdir($cacheDirectory); } } }