You are here

function advagg_js_compress_run_test in Advanced CSS/JS Aggregation 7.2

Test a JS file to see if it compresses well.

Parameters

string $filename: Path and filename of JS file.

array $info: (Optional) advagg_get_info_on_file().

array $compressors: (Optional) List of compressors to test.

Return value

array info about the file.

5 calls to advagg_js_compress_run_test()
advagg_js_compress_advagg_get_js_file_contents_alter in advagg_js_compress/advagg_js_compress.advagg.inc
Implements hook_advagg_get_js_file_contents_alter().
advagg_js_compress_advagg_save_aggregate_alter in advagg_js_compress/advagg_js_compress.advagg.inc
Implements hook_advagg_save_aggregate_alter().
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.
hook_advagg_changed_files in ./advagg.api.php
Let other modules know about the changed files.
hook_advagg_get_js_file_contents_alter in ./advagg.api.php
Allow other modules to modify this files contents.

File

advagg_js_compress/advagg_js_compress.advagg.inc, line 688

Code

function advagg_js_compress_run_test($filename, array $info = array(), array $compressors = array()) {

  // Get the info on this file.
  module_load_include('inc', 'advagg', 'advagg');
  if (empty($info)) {
    $info = advagg_get_info_on_file($filename, FALSE, FALSE);
  }
  $cache_id = 'advagg:js_compress:info:' . $info['filename_hash'];
  $cache_id .= !empty($info['content_hash']) ? ':' . $info['content_hash'] : '';
  $compressor_list = advagg_js_compress_get_enabled_compressors(array(), -1);

  // Build list of compressors.
  if (empty($compressors)) {
    $compressors = $compressor_list;
  }

  // Set to 0 if file doesn't exist.
  if (empty($info['content_hash'])) {
    foreach ($compressor_list as $key => $name) {
      $results[$key] = array(
        'code' => 0,
        'ratio' => 0,
        'name' => $name,
      );
    }
  }
  else {

    // Set to "-1" so if php bombs, the file will be marked as bad.
    foreach ($compressor_list as $key => $name) {
      $results[$key] = array(
        'code' => -1,
        'ratio' => 0,
        'name' => $name,
      );
    }
    $run_locally = TRUE;

    // Run this via httprl if possible.
    if (module_exists('httprl') && httprl_is_background_callback_capable()) {
      $run_locally = FALSE;

      // Setup callback options array.
      $callback_options = array(
        array(
          'function' => 'advagg_js_compress_test_file',
          'return' => &$results,
        ),
        $filename,
        $compressors,
        $cache_id,
      );

      // Queue up the request.
      httprl_queue_background_callback($callback_options);

      // Execute request.
      httprl_send_request();

      // If php bombs out, try each compressor individually.
      foreach ($results as $key => $value) {
        if ($value['code'] == -1) {
          $sub_result = array();
          $sub_result[$key] = $value;

          // Setup callback options array.
          $callback_options = array(
            array(
              'function' => 'advagg_js_compress_test_file',
              'return' => &$sub_result,
            ),
            $filename,
            array(
              $key => $value['name'],
            ),
            $cache_id,
          );

          // Queue up the request.
          httprl_queue_background_callback($callback_options);

          // Execute request.
          httprl_send_request();
          $results[$key] = $sub_result[$key];
        }
      }

      // Try locally if all return back -1 (something wrong with httprl).
      foreach ($results as $key => $value) {
        if ($value['code'] != -1) {
          $run_locally = TRUE;
          break;
        }
      }
    }
    if ($run_locally) {

      // Save results, so if PHP bombs, this file is marked as bad.
      // 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));

      // Test the file.
      $results = advagg_js_compress_test_file($filename, $compressors, $cache_id);
    }
  }

  // Save and return results.
  // Save results, so if PHP bombs, this file is marked as bad.
  // 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;
}