comparison core/lib/Drupal/Core/Asset/CssCollectionGrouper.php @ 0:4c8ae668cc8c

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