class CronHook
Implements hook_cron().
Hierarchy
- class \Drupal\file\Hook\CronHook
Expanded class hierarchy of CronHook
File
-
core/
modules/ file/ src/ Hook/ CronHook.php, line 20
Namespace
Drupal\file\HookView source
class CronHook {
public function __construct(EntityTypeManagerInterface $entityTypeManager, StreamWrapperManagerInterface $streamWrapperManager, ConfigFactoryInterface $configFactory, FileUsageInterface $fileUsage, TimeInterface $time, LoggerInterface $logger) {
}
/**
* Implements hook_cron().
*/
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)),
]);
}
}
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
CronHook::__construct | public | function | |
CronHook::__invoke | public | function | Implements hook_cron(). |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.