You are here

function advagg_get_info_on_files in Advanced CSS/JS Aggregation 7.2

Given a filename calculate various hashes and gather meta data.


  'filesize' => filesize($filename),
  'mtime' => @filemtime($filename),
  'filename_hash' => $filename_hash,
  'content_hash' => drupal_hash_base64($file_contents),
  'linecount' => $linecount,
  'data' => $filename,
  'fileext' => $ext,

Parameters

array $files: Array; array of filenames containing path information as well.

bool $bypass_cache: Bool: TRUE to bypass the cache.

Return value

array $return['filename'] which contains

11 calls to advagg_get_info_on_files()
advagg_admin_get_file_info in ./advagg.admin.inc
Get detailed info about the given filename.
advagg_generate_groups in ./advagg.inc
Group the CSS/JS into the biggest buckets possible.
advagg_get_aggregate_info_from_files in ./advagg.inc
Given a group of files calculate various hashes and gather meta data.
advagg_get_info_on_file in ./advagg.inc
Given a filename calculate various hashes and gather meta data.
advagg_js_compress_prep in advagg_js_compress/advagg_js_compress.advagg.inc
Compress a JS string.

... See full list

File

./advagg.inc, line 618
Advanced CSS/JS aggregation module.

Code

function advagg_get_info_on_files(array $files, $bypass_cache = FALSE, $run_alter = TRUE) {

  // Get the cached data.
  $cached_data =& advagg_load_files_info_into_static_cache($files);

  // Get basic info on the files.
  $return = array();
  foreach ($files as $file) {
    $filename_hash = advagg_drupal_hash_base64($file);
    $cache_id = 'advagg:file:' . $filename_hash;

    // If we are not bypassing the cache add cached data.
    if ($bypass_cache == FALSE && is_array($cached_data) && array_key_exists($cache_id, $cached_data)) {
      $return[$file] = $cached_data[$cache_id];
      continue;
    }

    // Clear PHP's internal file status cache.
    advagg_clearstatcache($file);

    // Remove file in the cache if it does not exist.
    if (!file_exists($file) || is_dir($file)) {
      if (isset($cached_data[$cache_id])) {
        cache_clear_all($cache_id, 'cache_advagg_info', FALSE);
      }

      // Return filename_hash and data. Empty values for the other keys.
      $return[$file] = array(
        'filesize' => 0,
        'mtime' => 0,
        'filename_hash' => $filename_hash,
        'content_hash' => '',
        'linecount' => 0,
        'data' => $file,
        'cache_id' => $cache_id,
        '#no_cache' => TRUE,
      );
      continue;
    }

    // Get the file contents.
    $file_contents = (string) @advagg_file_get_contents($file);
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    if ($ext !== 'css' && $ext !== 'js') {

      // Get the $ext from the database.
      $row = db_select('advagg_files', 'af')
        ->fields('af')
        ->condition('filename', $file)
        ->execute()
        ->fetchAssoc();
      if (!empty($row['filetype'])) {
        $ext = $row['filetype'];
      }
      if ($ext === 'less') {
        $ext = 'css';
      }
    }
    if ($ext === 'css') {

      // Get the number of selectors.
      $linecount = advagg_count_css_selectors($file_contents);
    }
    else {

      // Get the number of lines.
      $linecount = substr_count($file_contents, "\n");
    }

    // Build meta data array and set cache.
    $return[$file] = array(
      'filesize' => (int) @filesize($file),
      'mtime' => @filemtime($file),
      'filename_hash' => $filename_hash,
      'content_hash' => drupal_hash_base64($file_contents),
      'linecount' => $linecount,
      'data' => $file,
      'fileext' => $ext,
      'cache_id' => $cache_id,
    );
    if (isset($cached_data[$cache_id])) {
      $return[$file] += $cached_data[$cache_id];
    }
  }
  if ($run_alter) {

    // Run hook so other modules can modify the data on these files.
    // Call hook_advagg_get_info_on_files_alter().
    drupal_alter('advagg_get_info_on_files', $return, $cached_data, $bypass_cache);

    // Set the cache and populate return array.
    foreach ($return as $info) {

      // If no cache is empty add/update the cached entry.
      // Update the cache if it is new or something changed.
      if (empty($info['#no_cache']) && !empty($info['cache_id']) && (empty($cached_data[$info['cache_id']]) || $info !== $cached_data[$info['cache_id']])) {

        // CACHE_PERMANENT isn't good here. Use 2 weeks from now + 0-45 days.
        // The random 0 to 45 day addition is to prevent a cache stampede.
        cache_set($info['cache_id'], $info, 'cache_advagg_info', round(REQUEST_TIME + 1209600 + mt_rand(0, 3888000), -3));
      }

      // Update static cache.
      $cached_data[$info['cache_id']] = $info;
    }
  }
  return $return;
}