Same name in other branches
- 5.0.x advagg.module \advagg_cron()
- 6.0.x advagg.module \advagg_cron()
- 7.x-1.x advagg.module \advagg_cron()
- 7.x-2.x advagg.module \advagg_cron()
- 8.x-2.x advagg.module \advagg_cron()
- 8.x-4.x advagg.module \advagg_cron()
Implements hook_cron().
Deletes outdated file/aggregate data.
3 calls to advagg_cron()
- AdvaggCommands::cron in src/
Commands/ AdvaggCommands.php - Run the advagg cron hook.
- drush_advagg_cron in ./
advagg.drush.inc - Callback function for drush advagg-cron.
- OperationsForm::clearStaleAggregates in src/
Form/ OperationsForm.php - Clear out all stale advagg aggregated files.
File
-
./
advagg.module, line 46
Code
function advagg_cron($bypass = FALSE) {
$time = \Drupal::time()->getRequestTime();
$state = \Drupal::state();
// Ensure the htaccess files exist.
AssetOptimizer::generateHtaccess('css');
AssetOptimizer::generateHtaccess('js');
// Execute only if been longer than advagg cron setting since last run.
if (!$bypass && $state->get('advagg.cron_timestamp', $time) > $time - \Drupal::config('advagg.settings')->get('cron_frequency')) {
return [];
}
$state->set('advagg.cron_timestamp', $time);
$outdated = [
'css' => [],
'js' => [],
];
$file_system = \Drupal::service('file_system');
$cache = \Drupal::cache('advagg');
foreach ([
'css',
'js',
] as $type) {
$path = $file_system->realpath("public://{$type}/optimized");
if ($files = glob("{$path}/*.{$type}")) {
foreach ($files as $file) {
// Only delete files older than 1 week.
if (filemtime($file) > $time - 604800) {
continue;
}
$filename = str_replace([
'css_',
'js_',
], '', pathinfo($file, PATHINFO_FILENAME));
$cid = substr($filename, 0, strpos($filename, '.'));
if (!$cache->get($cid)) {
@file_unmanaged_delete($file);
@file_unmanaged_delete("{$file}.gz");
$outdated[$type][] = "{$filename}.{$type}";
}
}
}
}
return $outdated;
}