CssCollectionGrouper.php 3.5 KB

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