PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php

https://bitbucket.org/aswinvk28/smartpan-stock-drupal
PHP | 178 lines | 77 code | 16 blank | 85 comment | 7 complexity | d740f332fee1d16d00a890f26353c2ca MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Contains \Drupal\Core\Asset\CssCollectionOptimizer.
  4. */
  5. namespace Drupal\Core\Asset;
  6. use Drupal\Core\KeyValueStore\StateInterface;
  7. /**
  8. * Optimizes CSS assets.
  9. */
  10. class CssCollectionOptimizer implements AssetCollectionOptimizerInterface {
  11. /**
  12. * A CSS asset grouper.
  13. *
  14. * @var \Drupal\Core\Asset\CssCollectionGrouper
  15. */
  16. protected $grouper;
  17. /**
  18. * A CSS asset optimizer.
  19. *
  20. * @var \Drupal\Core\Asset\CssOptimizer
  21. */
  22. protected $optimizer;
  23. /**
  24. * An asset dumper.
  25. *
  26. * @var \Drupal\Core\Asset\AssetDumper
  27. */
  28. protected $dumper;
  29. /**
  30. * The state key/value store.
  31. *
  32. * @var \Drupal\Core\KeyValueStore\StateInterface
  33. */
  34. protected $state;
  35. /**
  36. * Constructs a CssCollectionOptimizer.
  37. *
  38. * @param \Drupal\Core\Asset\AssetCollectionGrouperInterface
  39. * The grouper for CSS assets.
  40. * @param \Drupal\Core\Asset\AssetOptimizerInterface
  41. * The optimizer for a single CSS asset.
  42. * @param \Drupal\Core\Asset\AssetDumperInterface
  43. * The dumper for optimized CSS assets.
  44. * @param \Drupal\Core\KeyValueStore\StateInterface
  45. * The state key/value store.
  46. */
  47. public function __construct(AssetCollectionGrouperInterface $grouper, AssetOptimizerInterface $optimizer, AssetDumperInterface $dumper, StateInterface $state) {
  48. $this->grouper = $grouper;
  49. $this->optimizer = $optimizer;
  50. $this->dumper = $dumper;
  51. $this->state = $state;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. *
  56. * The cache file name is retrieved on a page load via a lookup variable that
  57. * contains an associative array. The array key is the hash of the file names
  58. * in $css while the value is the cache file name. The cache file is generated
  59. * in two cases. First, if there is no file name value for the key, which will
  60. * happen if a new file name has been added to $css or after the lookup
  61. * variable is emptied to force a rebuild of the cache. Second, the cache file
  62. * is generated if it is missing on disk. Old cache files are not deleted
  63. * immediately when the lookup variable is emptied, but are deleted after a
  64. * set period by drupal_delete_file_if_stale(). This ensures that files
  65. * referenced by a cached page will still be available.
  66. */
  67. public function optimize(array $css_assets) {
  68. // Group the assets.
  69. $css_groups = $this->grouper->group($css_assets);
  70. // Now optimize (concatenate + minify) and dump each asset group, unless
  71. // that was already done, in which case it should appear in
  72. // drupal_css_cache_files.
  73. // Drupal contrib can override this default CSS aggregator to keep the same
  74. // grouping, optimizing and dumping, but change the strategy that is used to
  75. // determine when the aggregate should be rebuilt (e.g. mtime, HTTPS …).
  76. $map = $this->state->get('drupal_css_cache_files') ?: array();
  77. $css_assets = array();
  78. foreach ($css_groups as $order => $css_group) {
  79. // We have to return a single asset, not a group of assets. It is now up
  80. // to one of the pieces of code in the switch statement below to set the
  81. // 'data' property to the appropriate value.
  82. $css_assets[$order] = $css_group;
  83. unset($css_assets[$order]['items']);
  84. switch ($css_group['type']) {
  85. case 'file':
  86. // No preprocessing, single CSS asset: just use the existing URI.
  87. if (!$css_group['preprocess']) {
  88. $uri = $css_group['items'][0]['data'];
  89. $css_assets[$order]['data'] = $uri;
  90. }
  91. // Preprocess (aggregate), unless the aggregate file already exists.
  92. else {
  93. $key = $this->generateHash($css_group);
  94. $uri = '';
  95. if (isset($map[$key])) {
  96. $uri = $map[$key];
  97. }
  98. if (empty($uri) || !file_exists($uri)) {
  99. // Optimize each asset within the group.
  100. $data = '';
  101. foreach ($css_group['items'] as $css_asset) {
  102. $data .= $this->optimizer->optimize($css_asset);
  103. }
  104. // Per the W3C specification at
  105. // http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, @import
  106. // rules must proceed any other style, so we move those to the
  107. // top.
  108. $regexp = '/@import[^;]+;/i';
  109. preg_match_all($regexp, $data, $matches);
  110. $data = preg_replace($regexp, '', $data);
  111. $data = implode('', $matches[0]) . $data;
  112. // Dump the optimized CSS for this group into an aggregate file.
  113. $uri = $this->dumper->dump($data, 'css');
  114. // Set the URI for this group's aggregate file.
  115. $css_assets[$order]['data'] = $uri;
  116. // Persist the URI for this aggregate file.
  117. $map[$key] = $uri;
  118. $this->state->set('drupal_css_cache_files', $map);
  119. }
  120. else {
  121. // Use the persisted URI for the optimized CSS file.
  122. $css_assets[$order]['data'] = $uri;
  123. }
  124. $css_assets[$order]['preprocessed'] = TRUE;
  125. }
  126. break;
  127. case 'inline':
  128. // We don't do any caching for inline CSS assets.
  129. $data = '';
  130. foreach ($css_group['items'] as $css_asset) {
  131. $data .= $this->optimizer->optimize($css_asset);
  132. }
  133. unset($css_assets[$order]['data']['items']);
  134. $css_assets[$order]['data'] = $data;
  135. break;
  136. case 'external':
  137. // We don't do any aggregation and hence also no caching for external
  138. // CSS assets.
  139. $uri = $css_group['items'][0]['data'];
  140. $css_assets[$order]['data'] = $uri;
  141. break;
  142. }
  143. }
  144. return $css_assets;
  145. }
  146. /**
  147. * Generate a hash for a given group of CSS assets.
  148. *
  149. * @param array $css_group
  150. * A group of CSS assets.
  151. *
  152. * @return string
  153. * A hash to uniquely identify the given group of CSS assets.
  154. */
  155. protected function generateHash(array $css_group) {
  156. $css_data = array();
  157. foreach ($css_group['items'] as $css_file) {
  158. $css_data[] = $css_file['data'];
  159. }
  160. return hash('sha256', serialize($css_data));
  161. }
  162. }