Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / advagg / src / Asset / CssCollectionGrouper.php
1 <?php
2
3 namespace Drupal\advagg\Asset;
4
5 use Drupal\Core\Asset\AssetCollectionGrouperInterface;
6 use Drupal\Core\Asset\CssCollectionGrouper as CoreCssCollectionGrouper;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\State\StateInterface;
10
11 /**
12  * Groups CSS assets.
13  */
14 class CssCollectionGrouper extends CoreCssCollectionGrouper implements AssetCollectionGrouperInterface {
15
16   /**
17    * A config object for the advagg configuration.
18    *
19    * @var \Drupal\Core\Config\Config
20    */
21   protected $config;
22
23   /**
24    * The AdvAgg file status state information storage service.
25    *
26    * @var \Drupal\Core\State\StateInterface
27    */
28
29   protected $advaggFiles;
30
31   /**
32    * Module handler service.
33    *
34    * @var \Drupal\Core\Extension\ModuleHandlerInterface
35    */
36   protected $moduleHandler;
37
38   /**
39    * Construct the AssetDumper instance.
40    *
41    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
42    *   A config factory for retrieving required config objects.
43    * @param \Drupal\Core\State\StateInterface $advagg_files
44    *   The AdvAgg file status state information storage service.
45    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
46    *   The module handler.
47    */
48   public function __construct(ConfigFactoryInterface $config_factory, StateInterface $advagg_files, ModuleHandlerInterface $module_handler) {
49     $this->config = $config_factory->get('advagg.settings');
50     $this->advaggFiles = $advagg_files;
51     $this->moduleHandler = $module_handler;
52   }
53
54   /**
55    * {@inheritdoc}
56    *
57    * Puts multiple items into the same group if they are groupable and if they
58    * are for the same 'browsers' and 'inline'. Items of the 'file' type are
59    * groupable if their 'preprocess' flag is TRUE, and items of the 'external'
60    * type are never groupable.
61    *
62    * Also ensures that the process of grouping items does not change their
63    * relative order. This requirement may result in multiple groups for the same
64    * type, inline, media and browsers, depending on settings if needed to
65    * accommodate other items in between.
66    */
67   public function group(array $css_assets) {
68     if ($this->config->get('core_groups')) {
69       return parent::group($css_assets);
70     }
71     $combine_media = $this->config->get('css.combine_media');
72     $ie_limit_selectors = $this->config->get('css.ie.limit_selectors');
73     $ie_selector_limit = $this->config->get('css.ie.selector_limit');
74     if ($ie_limit_selectors) {
75       $file_info = $this->advaggFiles->getMultiple(array_column($css_assets, 'data'));
76     }
77     $groups = [];
78     // If a group can contain multiple items, we track the information that must
79     // be the same for each item in the group, so that when we iterate the next
80     // item, we can determine if it can be put into the current group, or if a
81     // new group needs to be made for it.
82     $current_group_keys = NULL;
83     // When creating a new group, we pre-increment $i, so by initializing it to
84     // -1, the first group will have index 0.
85     $i = -1;
86     $selectors = 0;
87     foreach ($css_assets as $item) {
88       // The browsers for which the CSS item needs to be loaded is part of the
89       // information that determines when a new group is needed, but the order
90       // of keys in the array doesn't matter, and we don't want a new group if
91       // all that's different is that order.
92       ksort($item['browsers']);
93
94       // If the item can be grouped with other items, set $group_keys to an
95       // array of information that must be the same for all items in its group.
96       // If the item can't be grouped with other items, set $group_keys to
97       // FALSE. We put items into a group that can be aggregated together:
98       // whether they will be aggregated is up to the _drupal_css_aggregate()
99       // function or an
100       // override of that function specified in hook_css_alter(), but regardless
101       // of the details of that function, a group represents items that can be
102       // aggregated. Since a group may be rendered with a single HTML tag, all
103       // items in the group must share the same information that would need to
104       // be part of that HTML tag.
105       switch ($item['type']) {
106         case 'file':
107           // Group file items if their 'preprocess' flag is TRUE.
108           // Help ensure maximum reuse of aggregate files by only grouping
109           // together items that share the same 'group' value.
110           if ($item['preprocess']) {
111             $group_keys = [$item['group'], $item['browsers']];
112             if (!$combine_media) {
113               $group_keys[] = $item['media'];
114             }
115             if (isset($item['inline'])) {
116               $group_keys[] = $item['inline'];
117             }
118             if ($ie_limit_selectors) {
119               if (isset($file_info[$item['data']]['parts'])) {
120                 foreach ($file_info[$item['data']]['parts'] as $part) {
121                   $i++;
122                   $groups[$i] = $item;
123                   unset($groups[$i]['data'], $groups[$i]['weight'], $groups[$i]['basename']);
124                   $groups[$i]['items'] = [0 => $item];
125                   $groups[$i]['items'][0]['data'] = $part['path'];
126                   $selectors = $part['selectors'];
127                 }
128               }
129               else {
130                 $selectors += $file_info[$item['data']]['linecount'];
131                 if ($selectors > $ie_selector_limit) {
132                   $group_keys['break'] = TRUE;
133                 }
134               }
135             }
136           }
137           else {
138             $group_keys = FALSE;
139             if ($ie_limit_selectors && $file_info[$item['data']]['linecount'] > $ie_selector_limit) {
140               foreach ($file_info[$item['data']]['parts'] as $part) {
141                 $i++;
142                 $groups[$i] = $item;
143                 unset($groups[$i]['data'], $groups[$i]['weight'], $groups[$i]['basename']);
144                 $groups[$i]['items'] = [0 => $item];
145                 $groups[$i]['items'][0]['data'] = $part['path'];
146               }
147               continue;
148             }
149           }
150           break;
151
152         case 'external':
153           // Do not group external items.
154           $group_keys = FALSE;
155           break;
156       }
157       // If the group keys don't match the most recent group we're working with,
158       // then a new group must be made.
159       if ($group_keys !== $current_group_keys) {
160         if ($ie_limit_selectors) {
161           if ($item['type'] == 'file' && $item['preprocess']) {
162             $selectors = $file_info[$item['data']]['linecount'];
163           }
164           else {
165             $selectors = 0;
166           }
167         }
168         unset($group_keys['break']);
169         $i++;
170         // Initialize the new group with the same properties as the first item
171         // being placed into it. The item's 'data', 'weight' and 'basename'
172         // properties are unique to the item and should not be carried over to
173         // the group.
174         $groups[$i] = $item;
175         unset($groups[$i]['data'], $groups[$i]['weight'], $groups[$i]['basename']);
176         if ($combine_media && $item['type'] == 'file' && $item['preprocess']) {
177           unset($groups[$i]['media']);
178         }
179         $groups[$i]['items'] = [];
180         $current_group_keys = $group_keys ? $group_keys : NULL;
181       }
182
183       // Add the item to the current group.
184       $groups[$i]['items'][] = $item;
185     }
186
187     // Run hook so other modules can modify the data.
188     // Call hook_advagg_asset_grouping_alter().
189     $type = 'css';
190     $this->moduleHandler->alter('advagg_aggregate_grouping', $groups, $type);
191
192     return $groups;
193   }
194
195 }