Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / advagg / src / Asset / JsCollectionGrouper.php
1 <?php
2
3 namespace Drupal\advagg\Asset;
4
5 use Drupal\Core\Asset\AssetCollectionGrouperInterface;
6 use Drupal\Core\Asset\JsCollectionGrouper as CoreJsCollectionGrouper;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8
9 /**
10  * Groups JavaScript assets.
11  */
12 class JsCollectionGrouper extends CoreJsCollectionGrouper implements AssetCollectionGrouperInterface {
13
14   /**
15    * Module handler service.
16    *
17    * @var \Drupal\Core\Extension\ModuleHandlerInterface
18    */
19   protected $moduleHandler;
20
21   /**
22    * Construct the AssetDumper instance.
23    *
24    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
25    *   The module handler.
26    */
27   public function __construct(ModuleHandlerInterface $module_handler) {
28     $this->moduleHandler = $module_handler;
29   }
30
31   /**
32    * {@inheritdoc}
33    *
34    * Puts multiple items into the same group if they are groupable and if they
35    * are for the same browsers. Items of the 'file' type are groupable if their
36    * 'preprocess' flag is TRUE. Items of the 'external' type are not groupable.
37    *
38    * Also ensures that the process of grouping items does not change their
39    * relative order. This requirement may result in multiple groups for the same
40    * type and browsers, if needed to accommodate other items in between.
41    */
42   public function group(array $js_assets) {
43     $groups = [];
44     // If a group can contain multiple items, we track the information that must
45     // be the same for each item in the group, so that when we iterate the next
46     // item, we can determine if it can be put into the current group, or if a
47     // new group needs to be made for it.
48     $current_group_keys = NULL;
49     $index = -1;
50     foreach ($js_assets as $item) {
51       // The browsers for which the JavaScript item needs to be loaded is part
52       // of the information that determines when a new group is needed, but the
53       // order of keys in the array doesn't matter, and we don't want a new
54       // group if all that's different is that order.
55       ksort($item['browsers']);
56
57       switch ($item['type']) {
58         case 'file':
59           // Group file items if their 'preprocess' flag is TRUE.
60           // Help ensure maximum reuse of aggregate files by only grouping
61           // together items that share the same 'group' value.
62           if ($item['preprocess']) {
63             $group_keys = [$item['group'], $item['browsers']];
64             if (isset($item['inline'])) {
65               $group_keys[] = $item['inline'];
66             }
67           }
68           else {
69             $group_keys = FALSE;
70           }
71           break;
72
73         case 'external':
74           // Do not group external items.
75           $group_keys = FALSE;
76           break;
77       }
78
79       // If the group keys don't match the most recent group we're working with,
80       // then a new group must be made.
81       if ($group_keys !== $current_group_keys) {
82         $index++;
83         // Initialize the new group with the same properties as the first item
84         // being placed into it. The item's 'data' and 'weight' properties are
85         // unique to the item and should not be carried over to the group.
86         $groups[$index] = $item;
87         unset($groups[$index]['data'], $groups[$index]['weight']);
88         $groups[$index]['items'] = [];
89         $current_group_keys = $group_keys ? $group_keys : NULL;
90       }
91
92       // Add the item to the current group.
93       $groups[$index]['items'][] = $item;
94     }
95
96     // Run hook so other modules can modify the data.
97     // Call hook_advagg_aggregate_grouping_alter().
98     $type = 'js';
99     $this->moduleHandler->alter('advagg_aggregate_grouping', $groups, $type);
100
101     return $groups;
102   }
103
104 }