You are here

function advagg_save_aggregate in Advanced CSS/JS Aggregation 7.2

Save an aggregate given a filename, the files included in it, and the type.

Parameters

string $filename: Just the filename no path information.

array $files: Array of filenames.

string $type: String: css or js.

array $aggregate_settings: Array of settings.

Return value

array array($files_to_save, $errors).

2 calls to advagg_save_aggregate()
advagg_missing_create_file in ./advagg.missing.inc
Given a filename create that file.
advagg_requirements in ./advagg.install
Implements hook_requirements().

File

./advagg.missing.inc, line 1166
Advanced CSS/JS aggregation module.

Code

function advagg_save_aggregate($filename, array $files, $type, array $aggregate_settings = array()) {
  list($css_path, $js_path) = advagg_get_root_files_dir();
  $uri = '';
  if ($type === 'css') {
    $uri = $css_path[0] . '/' . $filename;
  }
  elseif ($type === 'js') {
    $uri = $js_path[0] . '/' . $filename;
  }
  if (empty($aggregate_settings)) {
    $aggregate_settings = advagg_current_hooks_hash_array();
  }

  // Allow other modules to alter the location, files included, and settings.
  if (empty($aggregate_settings['settings']['no_alters'])) {

    // Call hook_advagg_save_aggregate_pre_alter().
    drupal_alter('advagg_save_aggregate_pre', $uri, $files, $aggregate_settings);
  }

  // Build the aggregates contents.
  $contents = '';
  if ($type === 'css') {
    list($contents, $write_aggregate) = advagg_get_css_aggregate_contents($files, $aggregate_settings, $filename);
  }
  elseif ($type === 'js') {
    list($contents, $write_aggregate) = advagg_get_js_aggregate_contents($files, $aggregate_settings, $filename);
  }
  if (variable_get('advagg_cache_level', ADVAGG_CACHE_LEVEL) < 0) {
    $contents = "/* This aggregate contains the following files:\n" . implode(",\n", array_keys($files)) . ". */\n\n" . $contents;
  }

  // List of files to save.
  $files_to_save = array(
    $uri => $contents,
  );

  // Allow other modules to alter the contents and add new files to save.
  // Call hook_advagg_save_aggregate_alter().
  $other_parameters = array(
    $files,
    $type,
  );
  if (empty($aggregate_settings['settings']['no_alters'])) {
    drupal_alter('advagg_save_aggregate', $files_to_save, $aggregate_settings, $other_parameters);
  }
  $errors = array();
  if ($write_aggregate) {
    foreach ($files_to_save as $uri => $data) {
      $errors = advagg_save_data($uri, $data);
      if (!file_exists($uri) || filesize($uri) == 0) {
        if ($type === 'css') {
          $full_dir = DRUPAL_ROOT . '/' . $css_path[1];
        }
        elseif ($type === 'js') {
          $full_dir = DRUPAL_ROOT . '/' . $js_path[1];
        }
        $free_space = @disk_free_space($full_dir);
        if ($free_space !== FALSE && strlen($data) > $free_space) {
          watchdog('advagg', 'Write to file system failed. Disk is full. %uri. !errors. %full_dir.', array(
            '%uri' => $uri,
            '!errors' => print_r($errors, TRUE),
            '%full_dir' => $full_dir,
          ), WATCHDOG_ALERT);
        }
        elseif (!is_writable($full_dir)) {
          watchdog('advagg', 'Write to file system failed. Check directory permissions. %uri. !errors. %full_dir.', array(
            '%uri' => $uri,
            '!errors' => print_r($errors, TRUE),
            '%full_dir' => $full_dir,
          ), WATCHDOG_ERROR);
        }
        else {
          watchdog('advagg', 'Write to file system failed. %uri. !errors. %full_dir.', array(
            '%uri' => $uri,
            '!errors' => print_r($errors, TRUE),
            '%full_dir' => $full_dir,
          ), WATCHDOG_ERROR);
        }

        // If the file is empty, remove it. Serving via drupal is better than an
        // empty aggregate being served.
        if (file_exists($uri) && filesize($uri) == 0) {
          @unlink($uri);
        }
      }
    }
  }
  return array(
    $files_to_save,
    $errors,
  );
}