diff --git a/core/core.services.yml b/core/core.services.yml index d73f6b5..6eea938 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1500,7 +1500,7 @@ services: arguments: [ '@state' ] asset.css.collection_optimizer: class: Drupal\Core\Asset\CssCollectionOptimizer - arguments: [ '@asset.css.collection_grouper', '@theme.manager' ] + arguments: [ '@asset.css.collection_grouper', '@asset.css.optimizer', '@theme.manager' ] asset.css.optimizer: class: Drupal\Core\Asset\CssOptimizer asset.css.collection_grouper: @@ -1512,7 +1512,7 @@ services: arguments: [ '@state' ] asset.js.collection_optimizer: class: Drupal\Core\Asset\JsCollectionOptimizer - arguments: [ '@asset.js.collection_grouper', '@theme.manager' ] + arguments: [ '@asset.js.collection_grouper', '@asset.js.optimizer', '@theme.manager' ] asset.js.optimizer: class: Drupal\Core\Asset\JsOptimizer asset.js.collection_grouper: diff --git a/core/lib/Drupal/Core/Asset/AssetCollectionGroupOptimizerInterface.php b/core/lib/Drupal/Core/Asset/AssetCollectionGroupOptimizerInterface.php new file mode 100644 index 0000000..6a78faa --- /dev/null +++ b/core/lib/Drupal/Core/Asset/AssetCollectionGroupOptimizerInterface.php @@ -0,0 +1,20 @@ +grouper = $grouper; + $this->optimizer = $optimizer; $this->themeManager = $theme_manager; } @@ -115,6 +126,26 @@ public function optimize(array $css_assets) { } /** + * [@inheritdoc} + */ + public function optimizeGroup(array $group) { + // Optimize each asset within the group. + $data = ''; + foreach ($group['items'] as $asset) { + $data .= $this->optimizer->optimize($asset); + } + // Per the W3C specification at + // http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, @import + // rules must precede any other style, so we move those to the + // top. + $regexp = '/@import[^;]+;/i'; + preg_match_all($regexp, $data, $matches); + $data = preg_replace($regexp, '', $data); + return implode('', $matches[0]) . $data; + } + + + /** * {@inheritdoc} */ public function getAll() { diff --git a/core/lib/Drupal/Core/Asset/JsCollectionGrouper.php b/core/lib/Drupal/Core/Asset/JsCollectionGrouper.php index 2b38d6d..710e3c0 100644 --- a/core/lib/Drupal/Core/Asset/JsCollectionGrouper.php +++ b/core/lib/Drupal/Core/Asset/JsCollectionGrouper.php @@ -45,9 +45,6 @@ public function group(array $js_assets) { // Do not group external items. $group_keys = FALSE; break; - default: - $e = new \Exception(); - trigger_error($e->getTraceAsString()); } // If the group keys don't match the most recent group we're working with, diff --git a/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php b/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php index 4aa4383..76e6564 100644 --- a/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php +++ b/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php @@ -8,16 +8,24 @@ /** * Optimizes JavaScript assets. */ -class JsCollectionOptimizer implements AssetCollectionOptimizerInterface { +class JsCollectionOptimizer implements AssetCollectionGroupOptimizerInterface { + use AssetGroupSetHashTrait; /** * A JS asset grouper. * - * @var \Drupal\Core\Asset\JsCollectionGrouper + * @var \Drupal\Core\Asset\JsCollectionGrouperInterface */ protected $grouper; /** + * A JS asset optimizer. + * + * @var \Drupal\Core\Asset\AssetOptimizerInterface + */ + protected $optimizer; + + /** * The theme manager. * * @var \Drupal\Core\Theme\ThemeManagerInterface @@ -29,11 +37,14 @@ class JsCollectionOptimizer implements AssetCollectionOptimizerInterface { * * @param \Drupal\Core\Asset\AssetCollectionGrouperInterface * The grouper for JS assets. + * @param \Drupal\Core\Asset\AssetOptimizerInterface + * The asset optimizer. * @param \Drupal\Core\Theme\ThemeManagerInterface; * The theme manger. */ - public function __construct(AssetCollectionGrouperInterface $grouper, ThemeManagerInterface $theme_manager) { + public function __construct(AssetCollectionGrouperInterface $grouper, AssetOptimizerInterface $optimizer, ThemeManagerInterface $theme_manager) { $this->grouper = $grouper; + $this->optimizer = $optimizer; $this->themeManager = $theme_manager; } @@ -116,24 +127,27 @@ public function optimize(array $js_assets) { } /** - * Generate a hash for a given group of assets. - * - * @param array $groups - * A group of assets. - * - * @return string - * A hash to uniquely identify the given group of assets. + * {@inheritdoc} */ - protected function generateHash(array $groups) { - $normalized = $groups; - foreach ($normalized as $order => $group) { - foreach ($group['items'] as $key => $asset) { - unset($normalized[$order]['items'][$key]['weight']); + public function optimizegroup(array $group) { + $data = ''; + foreach ($group['items'] as $js_asset) { + // Optimize this JS file, but only if it's not yet minified. + if (isset($js_asset['minified']) && $js_asset['minified']) { + $data .= file_get_contents($js_asset['data']); } + else { + $data .= $this->optimizer->optimize($js_asset); + } + // Append a ';' and a newline after each JS file to prevent them + // from running together. + $data .= ";\n"; } - return hash('sha256', serialize($normalized)); + // Remove unwanted JS code that cause issues. + return $this->optimizer->clean($data); } + /** * {@inheritdoc} */ diff --git a/core/modules/system/src/Controller/AssetControllerBase.php b/core/modules/system/src/Controller/AssetControllerBase.php index a7ff9b3..9544605 100644 --- a/core/modules/system/src/Controller/AssetControllerBase.php +++ b/core/modules/system/src/Controller/AssetControllerBase.php @@ -4,9 +4,13 @@ use Drupal\Component\Utility\Crypt; use Drupal\Core\Asset\AssetCollectionGrouperInterface; -use Drupal\Core\Asset\AssetOptimizerInterface; +use Drupal\Core\Asset\AssetCollectionOptimizerInterface; +use Drupal\Core\Asset\AssetDumperUriInterface; +use Drupal\Core\Asset\AssetGroupSetHashTrait; +use Drupal\Core\Asset\AssetResolverInterface; use Drupal\Core\Asset\AttachedAssets; use Drupal\Core\Asset\AttachedAssetsInterface; +use Drupal\Core\Asset\LibraryDependencyResolverInterface; use Drupal\Core\Theme\ThemeInitializationInterface; use Drupal\Core\Theme\ThemeManagerInterface; use Drupal\system\FileDownloadController; @@ -19,6 +23,7 @@ * Defines a controller to serve asset aggregates. */ abstract class AssetControllerBase extends FileDownloadController { + use AssetGroupSetHashTrait; /** * The library dependency resolver. @@ -56,9 +61,9 @@ protected $grouper; /** - * An asset optimizer. + * An asset collection optimizer. * - * @var \Drupal\Core\Asset\AssetOptimizerInterface + * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface */ protected $optimizer; @@ -108,12 +113,12 @@ * The theme manager. * @param \Drupal\Core\Asset\AssetCollectionGrouperInterface * The asset grouper. - * @param \Drupal\Core\Asset\AssetOptimizerInterface - * The optimizer for a single asset. + * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface + * The asset collection optimizer. * @param \Drupal\Core\Asset\AssetDumperUriInterface * The asset dumper. */ - public function __construct(LibraryDependencyResolverInterface $library_dependency_resolver, AssetResolverInterface $asset_resolver, ThemeInitializationInterface $theme_initialization, ThemeManagerInterface $theme_manager, AssetCollectionGrouperInterface $grouper, AssetOptimizerInterface $optimizer, AssetDumperUriInterface $dumper) { + public function __construct(LibraryDependencyResolverInterface $library_dependency_resolver, AssetResolverInterface $asset_resolver, ThemeInitializationInterface $theme_initialization, ThemeManagerInterface $theme_manager, AssetCollectionGrouperInterface $grouper, AssetCollectionOptimizerInterface $optimizer, AssetDumperUriInterface $dumper) { $this->libraryDependencyResolver = $library_dependency_resolver; $this->assetResolver = $asset_resolver; $this->themeInitialization = $theme_initialization; @@ -209,7 +214,7 @@ public function deliver(Request $request, $file_name) { ]; if (!file_exists($uri)) { - $data = $this->optimize($group); + $data = $this->optimizer->optimizeGroup($group); // Dump the optimized CSS for this group into an aggregate file. if ($match) { $uri = $this->dumper->dump($data, $this->assetType, $uri); @@ -241,16 +246,4 @@ public function deliver(Request $request, $file_name) { * The correct asset group matching $group_delta. */ abstract protected function getGroup(AttachedAssetsInterface $attached_assets, $group_delta, $request); - - /** - * Optimize a group in preparation for serving an aggregate file. - * - * @param array $group - * A group of assets. - * - * @return string - * An optimized string based on the contents of the group. - */ - abstract protected function optimize($group); - } diff --git a/core/modules/system/src/Controller/CssAssetController.php b/core/modules/system/src/Controller/CssAssetController.php index b5d4dc5..cab803c 100644 --- a/core/modules/system/src/Controller/CssAssetController.php +++ b/core/modules/system/src/Controller/CssAssetController.php @@ -33,7 +33,7 @@ public static function create(ContainerInterface $container) { $container->get('theme.initialization'), $container->get('theme.manager'), $container->get('asset.css.collection_grouper'), - $container->get('asset.css.optimizer'), + $container->get('asset.css.collection_optimizer'), $container->get('asset.css.dumper') ); } @@ -52,23 +52,4 @@ protected function getGroup(AttachedAssetsInterface $attached_assets, $group_del return $groups[$group_delta]; } - /** - * {@inheritdoc} - */ - protected function optimize($group) { - // Optimize each asset within the group. - $data = ''; - foreach ($group['items'] as $asset) { - $data .= $this->optimizer->optimize($asset); - } - // Per the W3C specification at - // http://www.w3.org/TR/REC-CSS2/cascade.html#at-import, @import - // rules must precede any other style, so we move those to the - // top. - $regexp = '/@import[^;]+;/i'; - preg_match_all($regexp, $data, $matches); - $data = preg_replace($regexp, '', $data); - return implode('', $matches[0]) . $data; - } - } diff --git a/core/modules/system/src/Controller/JsAssetController.php b/core/modules/system/src/Controller/JsAssetController.php index 6a92a85..138f4a4 100644 --- a/core/modules/system/src/Controller/JsAssetController.php +++ b/core/modules/system/src/Controller/JsAssetController.php @@ -34,7 +34,7 @@ public static function create(ContainerInterface $container) { $container->get('theme.initialization'), $container->get('theme.manager'), $container->get('asset.js.collection_grouper'), - $container->get('asset.js.optimizer'), + $container->get('asset.js.collection_optimizer'), $container->get('asset.js.dumper') ); } @@ -64,27 +64,4 @@ protected function getGroup(AttachedAssetsInterface $attached_assets, $group_del return $group; } - /** - * {@inheritdoc} - */ - protected function optimize($group) { - $data = ''; - foreach ($group['items'] as $js_asset) { - // Optimize this JS file, but only if it's not yet minified. - if (isset($js_asset['minified']) && $js_asset['minified']) { - $data .= file_get_contents($js_asset['data']); - } - else { - $data .= $this->optimizer->optimize($js_asset); - } - // Append a ';' and a newline after each JS file to prevent them - // from running together. - $data .= ";\n"; - } - // Remove unwanted JS code that cause issues. - return $this->optimizer->clean($data); - } - - - }