function MTimeProtectedFastFileStorage::garbageCollection

Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php \Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage::garbageCollection()
  2. 10 core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php \Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage::garbageCollection()
  3. 11.x core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php \Drupal\Component\PhpStorage\MTimeProtectedFastFileStorage::garbageCollection()

Overrides FileStorage::garbageCollection

File

core/lib/Drupal/Component/PhpStorage/MTimeProtectedFastFileStorage.php, line 152

Class

MTimeProtectedFastFileStorage
Stores PHP code in files with securely hashed names.

Namespace

Drupal\Component\PhpStorage

Code

public function garbageCollection() {
    $flags = \FilesystemIterator::CURRENT_AS_FILEINFO;
    $flags += \FilesystemIterator::SKIP_DOTS;
    foreach ($this->listAll() as $name) {
        $directory = $this->getContainingDirectoryFullPath($name);
        try {
            $dir_iterator = new \FilesystemIterator($directory, $flags);
        } catch (\UnexpectedValueException $e) {
            // FilesystemIterator throws an UnexpectedValueException if the
            // specified path is not a directory, or if it is not accessible.
            continue;
        }
        $directory_unlink = TRUE;
        $directory_mtime = filemtime($directory);
        foreach ($dir_iterator as $fileinfo) {
            if ($directory_mtime > $fileinfo->getMTime()) {
                // Ensure the folder is writable.
                @chmod($directory, 0777);
                @unlink($fileinfo->getPathName());
            }
            else {
                // The directory still contains valid files.
                $directory_unlink = FALSE;
            }
        }
        if ($directory_unlink) {
            $this->unlink($name);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.