Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Asset;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Groups JavaScript assets.
|
Chris@0
|
7 */
|
Chris@0
|
8 class JsCollectionGrouper 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 browsers. Items of the 'file' type are groupable if their
|
Chris@0
|
15 * 'preprocess' flag is TRUE. Items of the 'external' type are not groupable.
|
Chris@0
|
16 *
|
Chris@0
|
17 * Also ensures that the process of grouping items does not change their
|
Chris@0
|
18 * relative order. This requirement may result in multiple groups for the same
|
Chris@0
|
19 * type and browsers, if needed to accommodate other items in between.
|
Chris@0
|
20 */
|
Chris@0
|
21 public function group(array $js_assets) {
|
Chris@0
|
22 $groups = [];
|
Chris@0
|
23 // If a group can contain multiple items, we track the information that must
|
Chris@0
|
24 // be the same for each item in the group, so that when we iterate the next
|
Chris@0
|
25 // item, we can determine if it can be put into the current group, or if a
|
Chris@0
|
26 // new group needs to be made for it.
|
Chris@0
|
27 $current_group_keys = NULL;
|
Chris@0
|
28 $index = -1;
|
Chris@0
|
29 foreach ($js_assets as $item) {
|
Chris@0
|
30 // The browsers for which the JavaScript item needs to be loaded is part
|
Chris@0
|
31 // of the information that determines when a new group is needed, but the
|
Chris@0
|
32 // order of keys in the array doesn't matter, and we don't want a new
|
Chris@0
|
33 // group if all that's different is that order.
|
Chris@0
|
34 ksort($item['browsers']);
|
Chris@0
|
35
|
Chris@0
|
36 switch ($item['type']) {
|
Chris@0
|
37 case 'file':
|
Chris@0
|
38 // Group file items if their 'preprocess' flag is TRUE.
|
Chris@0
|
39 // Help ensure maximum reuse of aggregate files by only grouping
|
Chris@0
|
40 // together items that share the same 'group' value.
|
Chris@0
|
41 $group_keys = $item['preprocess'] ? [$item['type'], $item['group'], $item['browsers']] : FALSE;
|
Chris@0
|
42 break;
|
Chris@0
|
43
|
Chris@0
|
44 case 'external':
|
Chris@0
|
45 // Do not group external items.
|
Chris@0
|
46 $group_keys = FALSE;
|
Chris@0
|
47 break;
|
Chris@0
|
48 }
|
Chris@0
|
49
|
Chris@0
|
50 // If the group keys don't match the most recent group we're working with,
|
Chris@0
|
51 // then a new group must be made.
|
Chris@0
|
52 if ($group_keys !== $current_group_keys) {
|
Chris@0
|
53 $index++;
|
Chris@0
|
54 // Initialize the new group with the same properties as the first item
|
Chris@0
|
55 // being placed into it. The item's 'data' and 'weight' properties are
|
Chris@0
|
56 // unique to the item and should not be carried over to the group.
|
Chris@0
|
57 $groups[$index] = $item;
|
Chris@0
|
58 unset($groups[$index]['data'], $groups[$index]['weight']);
|
Chris@0
|
59 $groups[$index]['items'] = [];
|
Chris@0
|
60 $current_group_keys = $group_keys ? $group_keys : NULL;
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 // Add the item to the current group.
|
Chris@0
|
64 $groups[$index]['items'][] = $item;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 return $groups;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 }
|