annotate core/lib/Drupal/Core/Asset/CssCollectionGrouper.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Asset;
Chris@0 4
Chris@0 5 /**
Chris@0 6 * Groups CSS assets.
Chris@0 7 */
Chris@0 8 class CssCollectionGrouper implements AssetCollectionGrouperInterface {
Chris@0 9
Chris@0 10 /**
Chris@0 11 * {@inheritdoc}
Chris@0 12 *
Chris@0 13 * Puts multiple items into the same group if they are groupable and if they
Chris@0 14 * are for the same 'media' and 'browsers'. Items of the 'file' type are
Chris@0 15 * groupable if their 'preprocess' flag is TRUE, and items of the 'external'
Chris@0 16 * type are never groupable.
Chris@0 17 *
Chris@0 18 * Also ensures that the process of grouping items does not change their
Chris@0 19 * relative order. This requirement may result in multiple groups for the same
Chris@0 20 * type, media, and browsers, if needed to accommodate other items in between.
Chris@0 21 */
Chris@0 22 public function group(array $css_assets) {
Chris@0 23 $groups = [];
Chris@0 24 // If a group can contain multiple items, we track the information that must
Chris@0 25 // be the same for each item in the group, so that when we iterate the next
Chris@0 26 // item, we can determine if it can be put into the current group, or if a
Chris@0 27 // new group needs to be made for it.
Chris@0 28 $current_group_keys = NULL;
Chris@0 29 // When creating a new group, we pre-increment $i, so by initializing it to
Chris@0 30 // -1, the first group will have index 0.
Chris@0 31 $i = -1;
Chris@0 32 foreach ($css_assets as $item) {
Chris@0 33 // The browsers for which the CSS item needs to be loaded is part of the
Chris@0 34 // information that determines when a new group is needed, but the order
Chris@0 35 // of keys in the array doesn't matter, and we don't want a new group if
Chris@0 36 // all that's different is that order.
Chris@0 37 ksort($item['browsers']);
Chris@0 38
Chris@0 39 // If the item can be grouped with other items, set $group_keys to an
Chris@0 40 // array of information that must be the same for all items in its group.
Chris@0 41 // If the item can't be grouped with other items, set $group_keys to
Chris@0 42 // FALSE. We put items into a group that can be aggregated together:
Chris@0 43 // whether they will be aggregated is up to the _drupal_css_aggregate()
Chris@0 44 // function or an
Chris@0 45 // override of that function specified in hook_css_alter(), but regardless
Chris@0 46 // of the details of that function, a group represents items that can be
Chris@0 47 // aggregated. Since a group may be rendered with a single HTML tag, all
Chris@0 48 // items in the group must share the same information that would need to
Chris@0 49 // be part of that HTML tag.
Chris@0 50 switch ($item['type']) {
Chris@0 51 case 'file':
Chris@0 52 // Group file items if their 'preprocess' flag is TRUE.
Chris@0 53 // Help ensure maximum reuse of aggregate files by only grouping
Chris@0 54 // together items that share the same 'group' value.
Chris@0 55 $group_keys = $item['preprocess'] ? [$item['type'], $item['group'], $item['media'], $item['browsers']] : FALSE;
Chris@0 56 break;
Chris@0 57
Chris@0 58 case 'external':
Chris@0 59 // Do not group external items.
Chris@0 60 $group_keys = FALSE;
Chris@0 61 break;
Chris@0 62 }
Chris@0 63
Chris@0 64 // If the group keys don't match the most recent group we're working with,
Chris@0 65 // then a new group must be made.
Chris@0 66 if ($group_keys !== $current_group_keys) {
Chris@0 67 $i++;
Chris@0 68 // Initialize the new group with the same properties as the first item
Chris@0 69 // being placed into it. The item's 'data', 'weight' and 'basename'
Chris@0 70 // properties are unique to the item and should not be carried over to
Chris@0 71 // the group.
Chris@0 72 $groups[$i] = $item;
Chris@0 73 unset($groups[$i]['data'], $groups[$i]['weight'], $groups[$i]['basename']);
Chris@0 74 $groups[$i]['items'] = [];
Chris@0 75 $current_group_keys = $group_keys ? $group_keys : NULL;
Chris@0 76 }
Chris@0 77
Chris@0 78 // Add the item to the current group.
Chris@0 79 $groups[$i]['items'][] = $item;
Chris@0 80 }
Chris@0 81
Chris@0 82 return $groups;
Chris@0 83 }
Chris@0 84
Chris@0 85 }