Implements hook_advagg_filenames_alter().

File

advagg_bundler/advagg_bundler.module, line 57

Code

function advagg_bundler_advagg_filenames_alter(&$filenames) {
    // Get max number of sub aggregates to create.
    $max_css = variable_get('advagg_bundler_max_css', ADVAGG_BUNDLER_MAX_CSS);
    $max_js = variable_get('advagg_bundler_max_js', ADVAGG_BUNDLER_MAX_JS);
    $schema = advagg_get_current_schema();
    $output = array();
    foreach ($filenames as $values) {
        // Extract values and set them.
        $filetype = $values['filetype'];
        $files = $values['files'];
        $bundle_md5 = $values['bundle_md5'];
        $cached_data_key = 'bundler_' . $schema . '_' . $bundle_md5;
        // Try cache first; cache table is cache_advagg_bundle_reuse.
        $cached_data = advagg_cached_bundle_get($cached_data_key, 'advagg_bundler_filenames_alter');
        if (!empty($cached_data)) {
            $output = array_merge($output, $cached_data);
            continue;
        }
        // If cache miss then start to do processing.
        // Set the max value based off of the filetype.
        if ($filetype == 'css') {
            $max = $max_css;
        }
        if ($filetype == 'js') {
            $max = $max_js;
        }
        // If we are only going to have one bundle then do not do any more
        // processing.
        if (empty($max) || $max == 1) {
            $output[] = $values;
            $data = array(
                $values,
            );
            cache_set($cached_data_key, $data, 'cache_advagg_bundle_reuse', CACHE_TEMPORARY);
            continue;
        }
        // Load up each file.
        $groupings = array();
        // Preserve the order while grouping.
        $last_group = '';
        foreach ($files as $key => $filename) {
            // Assign each file to their group.
            $group = advagg_bundler_analysis($filename);
            // Set $last_group if this is the first run of this foreach loop.
            if (empty($last_group)) {
                $last_group = $group;
            }
            if ($last_group == $group) {
                // Include this new file into the last used group.
                $groupings[$group][] = $filename;
            }
            else {
                // In order to preserve CSS/JS execution order we need to move the last
                // group to a unique name. Use the array key to make this group unique.
                $groupings[$key . ' ' . $last_group] = $groupings[$last_group];
                unset($groupings[$last_group]);
                // Place the new file into the new group and set $last_group.
                $groupings[$group][] = $filename;
                $last_group = $group;
            }
        }
        // If only one group then don't do any more processing.
        if (count($groupings) == 1) {
            $output[] = $values;
            $data = array(
                $values,
            );
            cache_set($cached_data_key, $data, 'cache_advagg_bundle_reuse', CACHE_TEMPORARY);
            continue;
        }
        // Make sure we didn't go over the max; if we did merge the smallest bundles
        // together.
        advagg_bundler_merge($groupings, $max);
        // If only one group then don't do any more processing. The merge algorithm
        // could have reduce the groupings down to one.
        if (count($groupings) == 1) {
            $output[] = $values;
            $data = array(
                $values,
            );
            cache_set($cached_data_key, $data, 'cache_advagg_bundle_reuse', CACHE_TEMPORARY);
            continue;
        }
        // Set groups.
        $data = array();
        foreach ($groupings as $bundle) {
            $values['files'] = $bundle;
            $values['bundle_md5'] = md5($schema . implode('', $bundle));
            $data[] = $values;
            $output[] = $values;
        }
        cache_set($cached_data_key, $data, 'cache_advagg_bundle_reuse', CACHE_TEMPORARY);
    }
    // Return groupings.
    if (variable_get('advagg_bundler_active', ADVAGG_BUNDLER_ACTIVE)) {
        $filenames = $output;
    }
}