You are here

function advagg_bundler_get_css_selector_count in Advanced CSS/JS Aggregation 6

Gets the selector count of the provided files.

Parameters

array $files: Array of files to use.

Return value

array The selector counts of each file.

1 call to advagg_bundler_get_css_selector_count()
advagg_bundler_merge in advagg_bundler/advagg_bundler.module
Merge bundles together if too many where created.

File

advagg_bundler/advagg_bundler.module, line 438
Advanced aggregation bundler module.

Code

function advagg_bundler_get_css_selector_count($files) {
  $results = array();
  $placeholders = db_placeholders($files);
  $result = db_query("SELECT filename, selector_count, timestamp FROM {advagg_bundler_selector_count} WHERE filename IN ({$placeholders})", $files);
  while ($row = db_fetch_array($result)) {
    $modified = 0;
    if (is_readable($row['filename'])) {
      $modified = filemtime($row['filename']);
    }
    if ($modified > $row['timestamp']) {
      $css = advagg_build_css_bundle(array(
        $row['filename'],
      ), TRUE);

      // Get the number of selectors.
      // http://stackoverflow.com/questions/12567000/regex-matching-for-counting-css-selectors/12567381#12567381
      $selector_count = preg_match_all('/\\{.+?\\}|,/s', $css, $matched);
      db_query("UPDATE {advagg_bundler_selector_count} SET timestamp = %d, selector_count = %d WHERE filename LIKE '%s'", $modified, $selector_count, $row['filename']);
      $results[$row['filename']] = $selector_count;
    }
    else {
      $results[$row['filename']] = $row['selector_count'];
    }
  }
  foreach ($files as $file) {
    if (!isset($results[$file]) && file_exists($file)) {
      $css = advagg_build_css_bundle(array(
        $file,
      ), TRUE);
      $selector_count = preg_match_all('/\\{.+?\\}|,/s', $css, $matched);
      db_query("INSERT INTO {advagg_bundler_selector_count} VALUES('%s', '%s', %d, %d)", $file, md5($file), $selector_count, filemtime($file));
      $results[$file] = $selector_count;
    }
  }
  return $results;
}