You are here

function advagg_cron in Advanced CSS/JS Aggregation 8.3

Same name and namespace in other branches
  1. 8.4 advagg.module \advagg_cron()
  2. 8.2 advagg.module \advagg_cron()
  3. 6 advagg.module \advagg_cron()
  4. 7.2 advagg.module \advagg_cron()
  5. 7 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
Advanced CSS/JS aggregation module.

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;
}