You are here

function advagg_merge_plans in Advanced CSS/JS Aggregation 7.2

Apply the advagg changes to the $css_js_groups array.

Parameters

array $css_js_groups: An array of CSS or JS groups as returned by drupal_group_css/js().

array $plans: An array of changes to do to the $css_js_groups array.

Return value

array New version of $css_js_groups.

2 calls to advagg_merge_plans()
_advagg_aggregate_css in ./advagg.module
Default callback to aggregate CSS files and inline content.
_advagg_aggregate_js in ./advagg.module
Default callback to aggregate JavaScript files.

File

./advagg.module, line 3170
Advanced CSS/JS aggregation module.

Code

function advagg_merge_plans(array $css_js_groups, array $plans) {
  $used_keys = array();
  foreach ($plans as $plan) {
    $plan_added = FALSE;
    foreach ($css_js_groups as $key => $group) {

      // Remove files from the old css/js array.
      $file_removed = FALSE;
      foreach ($css_js_groups[$key]['items'] as $k => $values) {
        if (is_array($values) && array_key_exists('data', $values) && is_array($plan['items']['files']) && is_string($values['data'])) {

          // If the CSS is a split file, the first file is very meaningful, and
          // is probably the only file.
          $first_file = reset($plan['items']['files']);
          if (array_key_exists($values['data'], $plan['items']['files'])) {
            unset($css_js_groups[$key]['items'][$k]);
            $file_removed = TRUE;
          }
          elseif (!empty($first_file['split'])) {
            if ($values['data'] == $first_file['split_original']) {
              if (!empty($first_file['split_last_part'])) {
                unset($css_js_groups[$key]['items'][$k]);
              }
              $file_removed = TRUE;
            }
          }
        }
      }

      // Replace first file of the old css/js array with one from advagg.
      if ($file_removed && !$plan_added) {
        $step = 0;
        do {
          ++$step;
          $insert_key = '' . floatval($key) . '.' . sprintf('%03d', $step);
        } while (array_key_exists($insert_key, $css_js_groups));
        $css_js_groups[(string) $insert_key] = $plan;
        $plan_added = TRUE;
      }
    }

    // Remove old css/js grouping if no files are left in it.
    foreach ($css_js_groups as $key => $group) {
      if (empty($css_js_groups[$key]['items'])) {
        unset($css_js_groups[$key]);
      }
    }
    if (!$plan_added) {
      foreach ($css_js_groups as $key => $group) {
        if (empty($group['items']['aggregate_filenames_hash']) || $group['items']['aggregate_filenames_hash'] != $plan['items']['aggregate_filenames_hash'] || empty($group['items']['aggregate_contents_hash']) || $group['items']['aggregate_contents_hash'] != $plan['items']['aggregate_contents_hash']) {
          continue;
        }

        // Insert a unique key.
        do {
          $key = '' . (floatval($key) + 0.01);
        } while (array_key_exists((string) $key, $css_js_groups) || array_key_exists((string) $key, $used_keys));
        $used_keys[(string) $key] = TRUE;
        $css_js_groups[(string) $key] = $plan;
        $plan_added = TRUE;
        break;
      }
    }
  }

  // Key sort and normalize the array before returning it.
  ksort($css_js_groups);
  $css_js_groups = array_values($css_js_groups);
  return $css_js_groups;
}