You are here

function advagg_js_compress_run_mutiple_tests in Advanced CSS/JS Aggregation 7.2

Test a JS file to see if it compresses well.

Parameters

array $filenames_info: Array of filenames and info from advagg_get_info_on_file().

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

Return value

array Info about the file.

2 calls to advagg_js_compress_run_mutiple_tests()
advagg_js_compress_advagg_get_info_on_files_alter in advagg_js_compress/advagg_js_compress.advagg.inc
Implements hook_advagg_get_info_on_files_alter().
advagg_js_compress_redo_files in advagg_js_compress/advagg_js_compress.module
Get all js files and js files that are not compressed.

File

advagg_js_compress/advagg_js_compress.advagg.inc, line 791

Code

function advagg_js_compress_run_mutiple_tests(array $filenames_info, array $compressors = array()) {

  // Get the info on this file.
  module_load_include('inc', 'advagg', 'advagg');
  $compressor_list = advagg_js_compress_get_enabled_compressors(array(), -1);

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

  // Prevent PHP from running out of time if working with a lot of files.
  if (count($filenames_info) > 5) {

    // Set to max time if running from the command line.
    if (drupal_is_cli()) {
      drupal_set_time_limit(0);
    }
    else {
      $max_execution_time = ini_get('max_execution_time');
      if ($max_execution_time != 0) {
        $current_time = 5;
        if (is_callable('getrusage')) {
          $dat = getrusage();
          $current_time = $dat["ru_utime.tv_sec"];
        }
        $max_time = max(30, ini_get('max_execution_time'));
        $time_left = $max_time - $current_time;

        // Give every file 3 seconds.
        drupal_set_time_limit(count($filenames_info) * 3 + $time_left);
      }
    }
  }
  $return = array();
  foreach ($filenames_info as $filename => $info) {
    if (!module_exists('httprl') || !httprl_is_background_callback_capable()) {
      $return[$filename] = advagg_js_compress_run_test($filename, $info, $compressors);
      continue;
    }

    // Setup this run.
    $results = array();
    if (empty($info)) {
      $info = advagg_get_info_on_file($filename, FALSE, FALSE);
    }

    // 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,
        );
      }
      $return[$filename] = $results;
      continue;
    }
    $results =& $return[$filename];

    // 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,
      );
    }

    // Get cache id.
    $cache_id = 'advagg:js_compress:info:' . $info['filename_hash'];
    $cache_id .= !empty($info['content_hash']) ? ':' . $info['content_hash'] : '';

    // 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);
  }
  if (module_exists('httprl') && httprl_is_background_callback_capable()) {

    // Execute request.
    httprl_send_request();
    foreach ($return as $filename => &$results) {
      if (!empty($results)) {

        // If we have one bad set, we need to retest all.
        foreach ($results as $key => $value) {
          if ($value['code'] == -1) {
            unset($results);
            $results = array();
            break;
          }
        }
      }

      // If php bombs out, try each compressor individually.
      if (empty($results)) {
        foreach ($compressor_list as $key => $name) {
          $info = $filenames_info[$filename];
          $cache_id = 'advagg:js_compress:info:' . $info['filename_hash'];
          $cache_id .= !empty($info['content_hash']) ? ':' . $info['content_hash'] : '';
          $sub_result = array();
          $sub_result[$key] = '';

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

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

          // Execute request.
          httprl_send_request();
          if (!empty($sub_result[$key])) {
            $results[$key] = $sub_result[$key];
          }
          else {

            // Set to "-1" as php bombed, the file will be marked as bad.
            $results[$key] = array(
              'code' => -1,
              'ratio' => 0,
              'name' => $name,
            );
          }
        }
      }
    }
    unset($results);
  }
  return $return;
}