You are here

function advagg_advagg_save_aggregate_alter in Advanced CSS/JS Aggregation 7.2

Implements hook_advagg_save_aggregate_alter().

Used to add in a .gz file if none exits.

Related topics

File

./advagg.advagg.inc, line 20
Advanced CSS/JS aggregation module.

Code

function advagg_advagg_save_aggregate_alter(array &$files_to_save, array $aggregate_settings, array $other_parameters) {

  // * @param array $files_to_save
  // *   Array($uri => $contents).
  // * @param array $aggregate_settings
  // *   Array of settings.
  // * @param array $other_parameters
  // *   Array of containing $files and $type.
  $file_types = array();

  // Handle gzip.
  if (!empty($aggregate_settings['variables']['advagg_gzip'])) {

    // Use zopfli_encode if it exists.
    // See https://github.com/kjdev/php-ext-zopfli
    if (function_exists('zopfli_encode') && defined('ZOPFLI_GZIP') && empty($aggregate_settings['variables']['advagg_no_zopfli'])) {
      $file_types['.gz'] = array(
        'zopfli_encode',
        15,
        constant('ZOPFLI_GZIP'),
      );
    }
    else {
      $file_types['.gz'] = array(
        'gzencode',
        9,
        FORCE_GZIP,
      );
    }
  }

  // Handle brotli.
  // See https://github.com/kjdev/php-ext-brotli
  if (!empty($aggregate_settings['variables']['advagg_brotli']) && defined('BROTLI_TEXT') && function_exists('brotli_compress')) {
    $file_types['.br'] = array(
      'brotli_compress',
      11,
      constant('BROTLI_TEXT'),
    );
  }
  if (empty($file_types)) {
    return;
  }

  // Special S3 handling.
  $s3fs = FALSE;
  $files_to_save_keys = array_keys($files_to_save);
  foreach ($files_to_save_keys as $uri) {
    $wrapper = file_stream_wrapper_get_instance_by_uri($uri);
    if ($wrapper && get_class($wrapper) === 'S3fsStreamWrapper') {
      $s3fs = TRUE;
      break;
    }
  }
  foreach ($file_types as $ext => $settings) {

    // See if a file already exists with this extension.
    $ext_exists = FALSE;
    foreach ($files_to_save as $uri => $contents) {

      // See if this uri contains $ext near the end of it.
      if (strlen($uri) > 91 + strlen(ADVAGG_SPACE) * 3) {
        $pos = strripos($uri, $ext, 91 + strlen(ADVAGG_SPACE) * 3);
      }
      else {
        $pos = strripos($uri, $ext);
      }
      if (!empty($pos)) {
        $len = strlen($uri);

        // $ext file exists, exit loop.
        if ($pos == $len - 3) {
          $ext_exists = TRUE;
          break;
        }
      }
    }

    // If a $ext file does not exist, create one.
    if (!$ext_exists) {

      // Use the first file in the array.
      $data = reset($files_to_save);
      $uri = key($files_to_save);

      // Compress it and add it to the $files_to_save array.
      $callback = $settings[0];
      $settings[0] = $data;
      $compressed = call_user_func_array($callback, $settings);
      if (!empty($compressed)) {
        if ($s3fs && $ext === '.gz') {

          // Only serve gzip files from S3.
          $files_to_save[$uri] = $compressed;
        }
        elseif ($s3fs && $ext === '.br' && !isset($file_types['.gz'])) {

          // Only serve br files from S3.
          $files_to_save[$uri] = $compressed;
        }
        else {
          $files_to_save[$uri . $ext] = $compressed;
        }
      }
    }
  }
}