You are here

function advagg_js_compress_prep in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7.2 advagg_js_compress/advagg_js_compress.advagg.inc \advagg_js_compress_prep()
  2. 7 advagg_js_compress/advagg_js_compress.module \advagg_js_compress_prep()

Compress a JS string

Parameters

$contents: Javascript string.

1 call to advagg_js_compress_prep()
advagg_js_compress_advagg_js_alter in advagg_js_compress/advagg_js_compress.module
Implement hook_advagg_js_alter.

File

advagg_js_compress/advagg_js_compress.module, line 198
Advanced CSS/JS aggregation js compression module.

Code

function advagg_js_compress_prep(&$contents, $files, $bundle_md5) {

  // Make sure every file in this aggregate is compressible.
  $files_to_test = array();
  $list_bad = array();
  foreach ($files as $filename) {
    $filename_md5 = md5($filename);
    $data = advagg_get_file_data($filename_md5);

    // File needs to be tested.
    if (empty($data['advagg_js_compress']['tested'])) {
      $files_to_test[] = array(
        'md5' => $filename_md5,
        'filename' => $filename,
      );
    }
    elseif ($data['advagg_js_compress']['tested']['jsminplus'] != 1) {
      $list_bad[$filename] = $filename;
    }
  }
  $advagg_js_compress_callback = variable_get('advagg_js_compress_callback', ADVAGG_JS_COMPRESS_CALLBACK);
  if ($advagg_js_compress_callback) {

    // Send test files to worker.
    if (!empty($files_to_test)) {
      $compressible = advagg_js_compress_test_compression($files_to_test);

      // If an array then it is a list of files that can not be compressed.
      if (is_array($compressible)) {

        // Place filename in an array key.
        foreach ($compressible as $filedata) {
          $filename = $filedata['filename'];
          $list_bad[$filename] = $filename;
        }
      }
    }
  }
  $contents = '';

  // Do not compress the file that it bombs on.
  // Compress each file individually.
  foreach ($files as $file) {
    if (!empty($list_bad[$file])) {
      $contents .= advagg_build_js_bundle(array(
        $file,
      ));
    }
    else {
      $data = advagg_build_js_bundle(array(
        $file,
      ));

      // If using a cache, try to get the contents of it.
      $cached = FALSE;
      if (variable_get('advagg_js_compress_file_cache', ADVAGG_JS_COMPRESS_FILE_CACHE)) {
        $key = $file;
        $table = 'cache_advagg_js_compress_file';
        $cached_data = cache_get($key, $table);
        if (!empty($cached_data->data)) {
          $data = $cached_data->data;
          $cached = TRUE;
        }
      }
      if (!$cached && !empty($data)) {
        $compressor = variable_get('advagg_js_compressor', ADVAGG_JS_COMPRESSOR);
        if ($compressor == 0) {
          list($before, $after) = advagg_js_compress_jsminplus($data);
          $ratio = 0;
          if ($before != 0) {
            $ratio = ($before - $after) / $before;
          }

          // Make sure the returned string is not empty or has a VERY high
          // compression ratio.
          if (empty($data) || empty($ratio) || $ratio > variable_get('advagg_js_max_compress_ratio', ADVAGG_JS_MAX_COMPRESS_RATIO)) {
            $data = advagg_build_js_bundle(array(
              $file,
            ));
          }
          elseif (isset($key)) {

            // If using a cache set it.
            cache_set($key, $data, $table);
          }
        }
        elseif ($compressor == 1) {
          $contents = jsmin($contents);

          // Ensure that $contents ends with ; or }.
          if (strpbrk(substr(trim($contents), -1), ';}') === FALSE) {

            // ; or } not found, add in ; to the end of $contents.
            $contents = trim($contents) . ';';
          }
        }
      }
      $url = url($file, array(
        'absolute' => TRUE,
      ));
      $contents .= "/* Source and licensing information for the line(s) below can be found at {$url}. */\n" . $data . ";\n/* Source and licensing information for the above line(s) can be found at {$url}. */\n";
    }
  }
}