You are here

function advagg_js_compress_test_file in Advanced CSS/JS Aggregation 7.2

Same name and namespace in other branches
  1. 6 advagg_js_compress/advagg_js_compress.module \advagg_js_compress_test_file()
  2. 7 advagg_js_compress/advagg_js_compress.module \advagg_js_compress_test_file()

Test a file, making sure it is compressible.

Parameters

string $filename: Path and filename of the js file to test.

array $compressors: List of compressors to test.

string $cache_id: The cache ID for this file.

bool $debug: Set to TRUE to debug.

Return value

array Array showing the results of the compression tests.

1 call to advagg_js_compress_test_file()
advagg_js_compress_run_test in advagg_js_compress/advagg_js_compress.advagg.inc
Test a JS file to see if it compresses well.
2 string references to 'advagg_js_compress_test_file'
advagg_js_compress_run_mutiple_tests in advagg_js_compress/advagg_js_compress.advagg.inc
Test a JS file to see if it compresses well.
advagg_js_compress_run_test in advagg_js_compress/advagg_js_compress.advagg.inc
Test a JS file to see if it compresses well.

File

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

Code

function advagg_js_compress_test_file($filename, array $compressors = array(), $cache_id = '', $debug = FALSE) {
  $contents = (string) @advagg_file_get_contents($filename);

  // Get the JS string length before the compression operation.
  $contents_before = $contents;
  $before = strlen($contents);
  module_load_include('inc', 'advagg_js_compress', 'advagg_js_compress.advagg');
  if (empty($compressors)) {
    list(, , $compressors) = advagg_js_compress_configuration();
  }
  $results = array();
  foreach ($compressors as $key => $name) {
    $contents = $contents_before;
    $aggregate_settings['variables']['advagg_js_compressor'] = $key;

    // Compress it.
    $no_errors = advagg_js_compress_prep($contents, $filename, $aggregate_settings, FALSE, FALSE, FALSE, TRUE);
    $after = strlen($contents);
    $ratio = 0;
    if ($before != 0) {
      $ratio = ($before - $after) / $before;
    }

    // Set to "-1" if the compressor threw an error.
    if ($no_errors === FALSE) {
      $results[$key] = array(
        'code' => -1,
        'ratio' => round($ratio, 5),
        'name' => $name,
      );
    }
    elseif ($ratio < 0.001) {
      $results[$key] = array(
        'code' => -2,
        'ratio' => round($ratio, 5),
        'name' => $name,
      );
    }
    elseif ($ratio > 0.999) {
      $results[$key] = array(
        'code' => -3,
        'ratio' => round($ratio, 5),
        'name' => $name,
      );
    }
    else {
      $results[$key] = array(
        'code' => 1,
        'ratio' => round($ratio, 5),
        'name' => $name,
      );
    }
    if ($debug) {
      $results[$key]['contents'] = $contents;
    }
  }
  if (!empty($cache_id)) {
    $cache = cache_get($cache_id, 'cache_advagg_info');

    // Merge in old cached data.
    if (!empty($cache->data)) {

      // Do not merge in -1 code.
      foreach ($cache->data as $key => $value) {
        if ($value['code'] == -1) {
          unset($cache->data[$key]);
        }
      }
      $results += $cache->data;
    }

    // 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 stampeed.
    cache_set($cache_id, $results, 'cache_advagg_info', round(REQUEST_TIME + 1209600 + mt_rand(0, 3888000), -3));
  }
  return $results;
}