You are here

function advagg_build_css_bundle in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7 advagg.module \advagg_build_css_bundle()

Given a list of files, grab their contents and glue it into one big string.

Parameters

$files: array of filenames.

Return value

string containing all the files.

3 calls to advagg_build_css_bundle()
advagg_bundler_get_css_selector_count in advagg_bundler/advagg_bundler.module
Gets the selector count of the provided files.
advagg_css_js_file_builder in ./advagg.module
Aggregate CSS/JS files, putting them in the files directory.
_advagg_drupal_load_stylesheet in ./advagg.module
Loads stylesheets recursively and returns contents with corrected paths.

File

./advagg.module, line 2867
Advanced CSS/JS aggregation module

Code

function advagg_build_css_bundle($files) {

  // Check if CSS compression is enabled.
  if (module_exists('advagg_css') && (variable_get('advagg_css_compress_agg_files', ADVAGG_CSS_COMPRESS_AGG_FILES) || variable_get('advagg_css_compress_inline', ADVAGG_CSS_COMPRESS_INLINE))) {
    $optimize = FALSE;
  }
  else {
    $optimize = TRUE;
  }

  // Build aggregate CSS file.
  $data = '';
  foreach ($files as $file) {
    $contents = advagg_drupal_load_stylesheet($file, $optimize);

    // Build the base URL of this CSS file: start with the full URL.
    $css_base_url = advagg_file_create_url($file);

    // Move to the parent.
    $css_base_url = substr($css_base_url, 0, strrpos($css_base_url, '/'));

    // Simplify to a relative URL if the stylesheet URL starts with the
    // base URL of the website.
    if (substr($css_base_url, 0, strlen($GLOBALS['base_root'])) == $GLOBALS['base_root']) {
      $css_base_url = substr($css_base_url, strlen($GLOBALS['base_root']));
    }
    _drupal_build_css_path(NULL, $css_base_url . '/');

    // Anchor all paths in the CSS with its base URL, ignoring external and absolute paths.
    $data .= preg_replace_callback('/url\\(\\s*[\'"]?(?![a-z]+:|\\/+)([^\'")]+)[\'"]?\\s*\\)/i', '_drupal_build_css_path', $contents);
  }

  // Per the W3C specification at http://www.w3.org/TR/REC-CSS2/cascade.html#at-import,
  // @import rules must proceed any other style, so we move those to the top.
  $regexp = '/@import[^;]+;/i';
  preg_match_all($regexp, $data, $matches);
  $data = preg_replace($regexp, '', $data);
  $data = implode('', $matches[0]) . $data;
  return $data;
}