diff --git a/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php b/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php index a27b009..aedeea7 100644 --- a/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php +++ b/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php @@ -6,6 +6,8 @@ /** * Optimizes CSS assets. + * + * @deprecated as of Drupal 8.3.x, will be removed before Drupal 9.0.0 */ class CssCollectionOptimizer implements AssetCollectionOptimizerInterface { use CssCollectionOptimizerTrait; diff --git a/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php b/core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php similarity index 50% copy from core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php copy to core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php index a27b009..0ce60e0 100644 --- a/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php +++ b/core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php @@ -2,12 +2,14 @@ namespace Drupal\Core\Asset; -use Drupal\Core\State\StateInterface; +use \Drupal\Component\Utility\UrlHelper; +use \Drupal\Core\Theme\ThemeManagerInterface; /** * Optimizes CSS assets. */ -class CssCollectionOptimizer implements AssetCollectionOptimizerInterface { +class CssCollectionOptimizerLazy implements AssetCollectionGroupOptimizerInterface { + use AssetGroupSetHashTrait; use CssCollectionOptimizerTrait; /** @@ -18,25 +20,11 @@ class CssCollectionOptimizer implements AssetCollectionOptimizerInterface { protected $grouper; /** - * A CSS asset optimizer. + * The theme manager. * - * @var \Drupal\Core\Asset\CssOptimizer + * @var \Drupal\Core\Theme\ThemeManagerInterface */ - protected $optimizer; - - /** - * An asset dumper. - * - * @var \Drupal\Core\Asset\AssetDumper - */ - protected $dumper; - - /** - * The state key/value store. - * - * @var \Drupal\Core\State\StateInterface - */ - protected $state; + protected $themeManager; /** * Constructs a CssCollectionOptimizer. @@ -45,16 +33,13 @@ class CssCollectionOptimizer implements AssetCollectionOptimizerInterface { * The grouper for CSS assets. * @param \Drupal\Core\Asset\AssetOptimizerInterface $optimizer * The optimizer for a single CSS asset. - * @param \Drupal\Core\Asset\AssetDumperInterface $dumper - * The dumper for optimized CSS assets. - * @param \Drupal\Core\State\StateInterface $state - * The state key/value store. + * @param \Drupal\Core\Theme\ThemeManagerInterface + * The Theme manager. */ - public function __construct(AssetCollectionGrouperInterface $grouper, AssetOptimizerInterface $optimizer, AssetDumperInterface $dumper, StateInterface $state) { + public function __construct(AssetCollectionGrouperInterface $grouper, AssetOptimizerInterface $optimizer, ThemeManagerInterface $theme_manager) { $this->grouper = $grouper; $this->optimizer = $optimizer; - $this->dumper = $dumper; - $this->state = $state; + $this->themeManager = $theme_manager; } /** @@ -74,15 +59,10 @@ public function __construct(AssetCollectionGrouperInterface $grouper, AssetOptim public function optimize(array $css_assets) { // Group the assets. $css_groups = $this->grouper->group($css_assets); + $key = $this->generateHash($css_groups); - // Now optimize (concatenate + minify) and dump each asset group, unless - // that was already done, in which case it should appear in - // drupal_css_cache_files. - // Drupal contrib can override this default CSS aggregator to keep the same - // grouping, optimizing and dumping, but change the strategy that is used to - // determine when the aggregate should be rebuilt (e.g. mtime, HTTPS …). - $map = $this->state->get('drupal_css_cache_files') ?: array(); $css_assets = array(); + $libraries = []; foreach ($css_groups as $order => $css_group) { // We have to return a single asset, not a group of assets. It is now up // to one of the pieces of code in the switch statement below to set the @@ -97,26 +77,13 @@ public function optimize(array $css_assets) { $uri = $css_group['items'][0]['data']; $css_assets[$order]['data'] = $uri; } - // Preprocess (aggregate), unless the aggregate file already exists. else { - $key = $this->generateHash($css_group); - $uri = ''; - if (isset($map[$key])) { - $uri = $map[$key]; - } - if (empty($uri) || !file_exists($uri)) { - $data = $this->optimizeGroup($css_group); - // Dump the optimized CSS for this group into an aggregate file. - $uri = $this->dumper->dump($data, 'css'); - // Set the URI for this group's aggregate file. - $css_assets[$order]['data'] = $uri; - // Persist the URI for this aggregate file. - $map[$key] = $uri; - $this->state->set('drupal_css_cache_files', $map); - } - else { - // Use the persisted URI for the optimized CSS file. - $css_assets[$order]['data'] = $uri; + // To reproduce the full context of assets outside of the request, + // we must know the entire set of libraries used to generate all CSS + // groups, whether or not files in a group are from a particular + // library or not. + foreach ($css_group['items'] as $css_asset) { + $libraries[$css_asset['library']] = $css_asset['library']; } $css_assets[$order]['preprocessed'] = TRUE; } @@ -130,32 +97,32 @@ public function optimize(array $css_assets) { break; } } + // Generate a URL for the group, but do not process it inline, this is + // done by \Drupal\system\controller\CssAssetController + $minimal_set = \Drupal::service('library.dependency_resolver')->getMinimalRepresentativeSubset($libraries); + $theme_name = $this->themeManager->getActiveTheme()->getName(); + $ajax_page_state = \Drupal::request()->get('ajax_page_state'); + $already_loaded = isset($ajax_page_state) ? explode(',', $ajax_page_state['libraries']) : []; + $minimal_already_loaded_set = \Drupal::service('library.dependency_resolver')->getMinimalRepresentativeSubset($already_loaded); + $query = UrlHelper::buildQuery(['libraries' => $minimal_set, 'theme' => $theme_name, 'already_loaded' => $minimal_already_loaded_set]); + $path = 'public://' . 'css'; + foreach ($css_assets as $order => $css_asset) { + if (!empty($css_asset['preprocessed'])) { + $filename = 'css' . '_' . $order . '_' . $key . '.' . 'css'; + // Create the css/ or js/ path within the files folder. + $uri = $path . '/' . $filename; + $css_assets[$order]['data'] = file_create_url($uri) . '?' . $query; + } + } return $css_assets; } /** - * Generate a hash for a given group of CSS assets. - * - * @param array $css_group - * A group of CSS assets. - * - * @return string - * A hash to uniquely identify the given group of CSS assets. - */ - protected function generateHash(array $css_group) { - $css_data = array(); - foreach ($css_group['items'] as $css_file) { - $css_data[] = $css_file['data']; - } - return hash('sha256', serialize($css_data)); - } - - /** * {@inheritdoc} */ public function getAll() { - return $this->state->get('drupal_css_cache_files'); + return []; } } diff --git a/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php b/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php index 4d996a3..f90bd99 100644 --- a/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php +++ b/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php @@ -4,9 +4,11 @@ use Drupal\Core\State\StateInterface; - /** * Optimizes JavaScript assets. + * + * @deprecated as of Drupal 8.3.x, will be removed before Drupal 9.0.0 + * */ class JsCollectionOptimizer implements AssetCollectionOptimizerInterface { use JsCollectionOptimizerTrait;