You are here

public function Files::scanFile in Advanced CSS/JS Aggregation 8.2

Given a filename calculate various hashes and gather meta data.


  'filesize' => filesize($file),
  'mtime' => @filemtime($file),
  'filename_hash' => Crypt::hashBase64($file),
  'content_hash' => Crypt::hashBase64($file_contents),
  'linecount' => $linecount,
  'data' => $file,
  'fileext' => $ext,
  ...

Parameters

string $file: A filename/path.

array $cached: An array of previous values from the cache.

string $file_contents: Contents of the given file.

Return value

array $data which contains

1 call to Files::scanFile()
Files::getMultiple in src/State/Files.php
Returns the stored key/value pairs for a given set of keys.

File

src/State/Files.php, line 96
Given a filename calculate various hashes and gather meta data.

Class

Files
Provides AdvAgg with a file status state system using a key value store.

Namespace

Drupal\advagg\State

Code

public function scanFile($file, array $cached = [], $file_contents = '') {

  // Clear PHP's internal file status cache.
  clearstatcache(TRUE, $file);
  if (empty($file_contents)) {
    $file_contents = (string) @file_get_contents($file);
  }
  $content_hash = Crypt::hashBase64($file_contents);
  if (!empty($cached) && $content_hash != $cached['content_hash']) {
    $changes = $cached['changes'] + 1;
  }
  else {
    $changes = 0;
  }
  $ext = pathinfo($file, PATHINFO_EXTENSION);
  if ($ext !== 'css' && $ext !== 'js') {
    if ($ext === 'less') {
      $ext = 'css';
    }
  }
  if ($ext === 'css') {

    // Get the number of selectors.
    // http://stackoverflow.com/a/12567381/125684
    $linecount = preg_match_all('/\\{.+?\\}|,/s', $file_contents);
  }
  else {

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

  // Build meta data array.
  $data = [
    'filesize' => (int) @filesize($file),
    'mtime' => @filemtime($file),
    'filename_hash' => Crypt::hashBase64($file),
    'content_hash' => $content_hash,
    'linecount' => $linecount,
    'data' => $file,
    'fileext' => $ext,
    'updated' => REQUEST_TIME,
    'contents' => $file_contents,
    'changes' => $changes,
  ];
  if ($ext === 'css' && $linecount > $this->config
    ->get('css.ie.selector_limit')) {
    $this
      ->splitCssFile($data);
  }

  // Run hook so other modules can modify the data.
  // Call hook_advagg_scan_file_alter().
  $this->moduleHandler
    ->alter('advagg_scan_file', $file, $data, $cached);
  unset($data['contents']);
  $this
    ->set($file, $data);
  return $data;
}