Same name and namespace in other branches
- 6.0.x advagg_bundler/src/Asset/AdvaggJsCollectionGrouper.php \Drupal\advagg_bundler\Asset\AdvaggJsCollectionGrouper::group()
- 8.x-3.x advagg_bundler/src/Asset/AdvaggJsCollectionGrouper.php \Drupal\advagg_bundler\Asset\AdvaggJsCollectionGrouper::group()
- 8.x-4.x advagg_bundler/src/Asset/AdvaggJsCollectionGrouper.php \Drupal\advagg_bundler\Asset\AdvaggJsCollectionGrouper::group()
Fichier
-
advagg_bundler/src/Asset/AdvaggJsCollectionGrouper.php, line 27
Classe
- AdvaggJsCollectionGrouper
- Groups JavaScript assets.
Namespace
Drupal\advagg_bundler\Asset
Code
public function group(array $js_assets) {
$max = $this->config
->get('js.max');
if (!$this->config
->get('active') || !$max) {
return parent::group($js_assets);
}
$logic = $this->config
->get('js.logic');
$preprocess_count = count(array_filter(array_column($js_assets, 'preprocess')));
$target = max($max - (count($js_assets) - $preprocess_count), 1);
if ($logic === 0) {
$split = round($preprocess_count / $target);
}
else {
$split = array_sum(array_column($js_assets, 'size')) / $target;
}
$groups = [];
$current_group_keys = NULL;
$current_size = 0;
$index = -1;
foreach ($js_assets as $item) {
ksort($item['browsers']);
switch ($item['type']) {
case 'file':
$arrayItems = [
$item['type'],
$item['group'],
$item['browsers'],
];
$group_keys = $item['preprocess'] ? $arrayItems : FALSE;
break;
case 'external':
$group_keys = FALSE;
break;
}
if ($group_keys !== $current_group_keys) {
$index++;
$groups[$index] = $item;
unset($groups[$index]['data'], $groups[$index]['weight']);
$groups[$index]['items'] = [];
$current_group_keys = $group_keys ? $group_keys : NULL;
}
$groups[$index]['items'][] = $item;
if ($current_group_keys) {
if ($logic === 0) {
if (count($groups[$index]['items']) >= $split) {
$current_group_keys = NULL;
}
}
else {
$current_size += isset($item['size']) ? $item['size'] : 0;
if ($current_size >= $split) {
$current_size = 0;
$current_group_keys = NULL;
}
}
}
else {
$current_size = 0;
}
}
return $groups;
}