function CronHook::__invoke

Implements hook_cron().

File

core/modules/file/src/Hook/CronHook.php, line 36

Class

CronHook
Implements hook_cron().

Namespace

Drupal\file\Hook

Code

public function __invoke() : void {
    $age = $this->configFactory
        ->get('system.file')
        ->get('temporary_maximum_age');
    $fileStorage = $this->entityTypeManager
        ->getStorage('file');
    // Only delete temporary files if older than $age. Note that automatic cleanup
    // is disabled if $age set to 0.
    if ($age) {
        $fids = $fileStorage->getQuery()
            ->accessCheck(FALSE)
            ->condition('status', FileInterface::STATUS_PERMANENT, '<>')
            ->condition('changed', $this->time
            ->getRequestTime() - $age, '<')
            ->range(0, 100)
            ->execute();
        
        /** @var \Drupal\file\FileInterface[] $files */
        $files = $fileStorage->loadMultiple($fids);
        foreach ($files as $file) {
            $references = $this->fileUsage
                ->listUsage($file);
            if (empty($references)) {
                if (!file_exists($file->getFileUri())) {
                    if (!$this->streamWrapperManager
                        ->isValidUri($file->getFileUri())) {
                        $this->logger
                            ->warning('Temporary file "%path" that was deleted during garbage collection did not exist on the filesystem. This could be caused by a missing stream wrapper.', [
                            '%path' => $file->getFileUri(),
                        ]);
                    }
                    else {
                        $this->logger
                            ->warning('Temporary file "%path" that was deleted during garbage collection did not exist on the filesystem.', [
                            '%path' => $file->getFileUri(),
                        ]);
                    }
                }
                // Delete the file entity. If the file does not exist, this will
                // generate a second notice in the watchdog.
                $file->delete();
            }
            else {
                $this->logger
                    ->info('Did not delete temporary file "%path" during garbage collection because it is in use by the following modules: %modules.', [
                    '%path' => $file->getFileUri(),
                    '%modules' => implode(', ', array_keys($references)),
                ]);
            }
        }
    }
}

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