Created
June 10, 2013 13:46
-
-
Save wimleers/5748812 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function aggregate(array $css_assets) { | |
// Group the assets. | |
$css_groups = $this->grouper->group($css_assets); | |
// Now optimize (concatenate + minify) and dump each asset group, unless | |
// that was already done, in which case it should appear in | |
// drupal_css_cache_files. | |
// Drupal contrib can override this default CSS aggregator to keep the same | |
// grouping, optimizing and dumping, but change the strategy that is used to | |
// determine when the aggregate should be rebuilt (e.g. mtime, HTTPS …). | |
$map = state()->get('drupal_css_cache_files') ?: array(); | |
$css_assets = array(); | |
foreach ($css_groups as $order => $css_group) { | |
// We have to return a single asset, not a group of assets. It is now up | |
// to one of the pieces of code in the switch statement below to set the | |
// 'data' property to the appropriate value. | |
$css_assets[$order] = $css_group; | |
unset($css_assets[$order]['items']); | |
switch ($css_group['type']) { | |
case 'file': | |
// No preprocessing, single CSS asset: just use the existing URI. | |
if (!$css_group['preprocess']) { | |
$uri = $css_group['items'][0]['data']; | |
$css_assets[$order]['data'] = $uri; | |
} | |
// Preprocess (aggregate), unless the aggregate file already exists. | |
else { | |
$key = $this->generateHash($css_group); | |
$uri = ''; | |
if (isset($map[$key])) { | |
$uri = $map[$key]; | |
} | |
if (empty($uri) || !file_exists($uri)) { | |
$optimized_css_contents = $this->optimizer->optimize($css_group); | |
$uri = $this->dumper->dump($optimized_css_contents, $css_group); | |
$css_assets[$order]['data'] = $uri; | |
// Persist the URI for this aggregate file. | |
$map[$key] = $uri; | |
state()->set('drupal_css_cache_files', $map); | |
} | |
else { | |
// Use the persisted URI for the optimized CSS file. | |
$css_assets[$order]['data'] = $uri; | |
} | |
} | |
break; | |
case 'inline': | |
// We don't do any caching for inline CSS assets. | |
$optimized_css_contents = $this->optimizer->optimize($css_group); | |
unset($css_assets[$order]['data']['items']); | |
$css_assets[$order]['data'] = $optimized_css_contents; | |
break; | |
case 'external': | |
// We don't do any aggregation and hence also no caching for external | |
// CSS assets. | |
$css_assets[$order] = $css_group; | |
break; | |
} | |
} | |
return $css_assets; | |
} | |
/** | |
* Generate a hash for a given group of CSS assets. | |
*/ | |
protected function generateHash($css_group) { | |
$css_data = array(); | |
foreach ($css_group['items'] as $css_file) { | |
$css_data[] = $css_file['data']; | |
} | |
return hash('sha256', serialize($css_data)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment