diff --git a/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php b/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php index 73bac77..5b63897 100644 --- a/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php +++ b/core/lib/Drupal/Core/Asset/CssCollectionOptimizer.php @@ -4,13 +4,17 @@ use Drupal\Core\State\StateInterface; +@trigger_error('The ' . __NAMESPACE__ . '\CssCollectionOptimizer is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use ' . __NAMESPACE__ . '\CssCollectionOptimizerLazy', E_USER_DEPRECATED); + /** * Optimizes CSS assets. * * @deprecated as of Drupal 8.3.x, will be removed before Drupal 9.0.0 + * Instead you should use \Drupal\Core\Asset\CssCollectionOptimizerLazy + * + * @see https://www.drupal.org/node/2888767 */ class CssCollectionOptimizer implements AssetCollectionOptimizerInterface { - use CssCollectionOptimizerTrait; /** * A CSS asset grouper. @@ -34,6 +38,20 @@ class CssCollectionOptimizer implements AssetCollectionOptimizerInterface { protected $dumper; /** + * The CSS asset optimizer. + * + * @var \Drupal\Core\Asset\CssOptimizer + */ + protected $optimizer; + + /** + * The state key/value store. + * + * @var \Drupal\Core\State\StateInterface + */ + protected $state; + + /** * Constructs a CssCollectionOptimizer. * * @param \Drupal\Core\Asset\AssetCollectionGrouperInterface $grouper @@ -146,4 +164,53 @@ protected function generateHash(array $css_group) { return hash('sha256', serialize($css_data)); } + /** + * Optimizes a specific group assets. + * + * @param array $group + * An asset group. + * + * @return array + * The optimized string for the group. + */ + 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; + } + + /** + * Deletes all optimized asset collections assets. + */ + public function deleteAll() { + $this->state->delete('drupal_css_cache_files'); + $delete_stale = function($uri) { + // Default stale file threshold is 30 days. + if (REQUEST_TIME - filemtime($uri) > \Drupal::config('system.performance')->get('stale_file_threshold')) { + file_unmanaged_delete($uri); + } + }; + file_scan_directory('public://css', '/.*/', array('callback' => $delete_stale)); + } + + /** + * Returns all optimized asset collections assets. + * + * @return string[] + * URIs for all optimized asset collection assets. + */ + public function getAll() { + return $this->state->get('drupal_css_cache_files', []); + } + } diff --git a/core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php b/core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php index d020e54..715e48d 100644 --- a/core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php +++ b/core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php @@ -12,7 +12,6 @@ */ class CssCollectionOptimizerLazy implements AssetCollectionGroupOptimizerInterface { use AssetGroupSetHashTrait; - use CssCollectionOptimizerTrait; /** * The grouper for CSS assets. @@ -36,6 +35,13 @@ class CssCollectionOptimizerLazy implements AssetCollectionGroupOptimizerInterfa protected $dependencyResolver; /** + * The CSS asset optimizer. + * + * @var \Drupal\Core\Asset\CssOptimizer + */ + protected $optimizer; + + /** * The request stack. * * @var \Symfony\HttpFoundation\RequestStack @@ -43,6 +49,14 @@ class CssCollectionOptimizerLazy implements AssetCollectionGroupOptimizerInterfa protected $requestStack; /** + * The state key/value store. + * + * @var \Drupal\Core\State\StateInterface + */ + protected $state; + + + /** * Constructs a CssCollectionOptimizerLazy. * * @param \Drupal\Core\Asset\AssetCollectionGrouperInterface $grouper @@ -141,5 +155,53 @@ public function optimize(array $css_assets) { return $css_assets; } + /** + * Optimizes a specific group assets. + * + * @param array $group + * An asset group. + * + * @return array + * The optimized string for the group. + */ + 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; + } + + /** + * Deletes all optimized asset collections assets. + */ + public function deleteAll() { + $this->state->delete('drupal_css_cache_files'); + $delete_stale = function($uri) { + // Default stale file threshold is 30 days. + if (REQUEST_TIME - filemtime($uri) > \Drupal::config('system.performance')->get('stale_file_threshold')) { + file_unmanaged_delete($uri); + } + }; + file_scan_directory('public://css', '/.*/', array('callback' => $delete_stale)); + } + + /** + * Returns all optimized asset collections assets. + * + * @return string[] + * URIs for all optimized asset collection assets. + */ + public function getAll() { + return $this->state->get('drupal_css_cache_files', []); + } } diff --git a/core/lib/Drupal/Core/Asset/CssCollectionOptimizerTrait.php b/core/lib/Drupal/Core/Asset/CssCollectionOptimizerTrait.php deleted file mode 100644 index e884003..0000000 --- a/core/lib/Drupal/Core/Asset/CssCollectionOptimizerTrait.php +++ /dev/null @@ -1,73 +0,0 @@ -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; - } - - /** - * Deletes all optimized asset collections assets. - */ - public function deleteAll() { - $this->state->delete('drupal_css_cache_files'); - $delete_stale = function($uri) { - // Default stale file threshold is 30 days. - if (REQUEST_TIME - filemtime($uri) > \Drupal::config('system.performance')->get('stale_file_threshold')) { - file_unmanaged_delete($uri); - } - }; - file_scan_directory('public://css', '/.*/', array('callback' => $delete_stale)); - } - - /** - * Returns all optimized asset collections assets. - * - * @return string[] - * URIs for all optimized asset collection assets. - */ - public function getAll() { - return $this->state->get('drupal_css_cache_files', []); - } - -} diff --git a/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php b/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php index f82c5bc..20e0938 100644 --- a/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php +++ b/core/lib/Drupal/Core/Asset/JsCollectionOptimizer.php @@ -4,14 +4,18 @@ use Drupal\Core\State\StateInterface; +@trigger_error('The ' . __NAMESPACE__ . '\JsCollectionOptimizer is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use ' . __NAMESPACE__ . '\JsCollectionOptimizerLazy', E_USER_DEPRECATED); + /** * Optimizes JavaScript assets. * * @deprecated as of Drupal 8.3.x, will be removed before Drupal 9.0.0 + * Instead you should use \Drupal\Core\Asset\JsCollectionOptimizerLazy + * + * @see https://www.drupal.org/node/2888767 * */ class JsCollectionOptimizer implements AssetCollectionOptimizerInterface { - use JsCollectionOptimizerTrait; /** * A JS asset grouper. @@ -35,6 +39,20 @@ class JsCollectionOptimizer implements AssetCollectionOptimizerInterface { protected $dumper; /** + * A JS asset optimizer. + * + * @var \Drupal\Core\Asset\AssetOptimizerInterface + */ + protected $optimizer; + + /** + * The state key/value store. + * + * @var \Drupal\Core\State\StateInterface + */ + protected $state; + + /** * Constructs a JsCollectionOptimizer. * * @param \Drupal\Core\Asset\AssetCollectionGrouperInterface $grouper @@ -147,4 +165,54 @@ protected function generateHash(array $js_group) { return hash('sha256', serialize($js_data)); } + /** + * Optimizes a specific group assets. + * + * @param array $group + * An asset group. + * + * @return array + * The optimized string for the group. + */ + 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"; + } + // Remove unwanted JS code that cause issues. + return $this->optimizer->clean($data); + } + + /** + * Deletes all optimized asset collections assets. + */ + public function deleteAll() { + $this->state->delete('system.js_cache_files'); + $delete_stale = function($uri) { + // Default stale file threshold is 30 days. + if (REQUEST_TIME - filemtime($uri) > \Drupal::config('system.performance')->get('stale_file_threshold')) { + file_unmanaged_delete($uri); + } + }; + file_scan_directory('public://js', '/.*/', array('callback' => $delete_stale)); + } + + /** + * Returns all optimized asset collections assets. + * + * @return string[] + * URIs for all optimized asset collection assets. + */ + public function getAll() { + return $this->state->get('system.js_cache_files', []); + } } diff --git a/core/lib/Drupal/Core/Asset/JsCollectionOptimizerLazy.php b/core/lib/Drupal/Core/Asset/JsCollectionOptimizerLazy.php index e355078..607d509 100644 --- a/core/lib/Drupal/Core/Asset/JsCollectionOptimizerLazy.php +++ b/core/lib/Drupal/Core/Asset/JsCollectionOptimizerLazy.php @@ -13,7 +13,6 @@ */ class JsCollectionOptimizerLazy implements AssetCollectionGroupOptimizerInterface { use AssetGroupSetHashTrait; - use JsCollectionOptimizerTrait; /** * A JS asset grouper. @@ -44,6 +43,21 @@ class JsCollectionOptimizerLazy implements AssetCollectionGroupOptimizerInterfac protected $requestStack; /** + * A JS asset optimizer. + * + * @var \Drupal\Core\Asset\AssetOptimizerInterface + */ + protected $optimizer; + + /** + * The state key/value store. + * + * @var \Drupal\Core\State\StateInterface + */ + protected $state; + + + /** * Constructs a JsCollectionOptimizerLazy. * * @param \Drupal\Core\Asset\AssetCollectionGrouperInterface $grouper @@ -150,4 +164,55 @@ public function optimize(array $js_assets) { return $js_assets; } + /** + * Optimizes a specific group assets. + * + * @param array $group + * An asset group. + * + * @return array + * The optimized string for the group. + */ + 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"; + } + // Remove unwanted JS code that cause issues. + return $this->optimizer->clean($data); + } + + /** + * Deletes all optimized asset collections assets. + */ + public function deleteAll() { + $this->state->delete('system.js_cache_files'); + $delete_stale = function($uri) { + // Default stale file threshold is 30 days. + if (REQUEST_TIME - filemtime($uri) > \Drupal::config('system.performance')->get('stale_file_threshold')) { + file_unmanaged_delete($uri); + } + }; + file_scan_directory('public://js', '/.*/', array('callback' => $delete_stale)); + } + + /** + * Returns all optimized asset collections assets. + * + * @return string[] + * URIs for all optimized asset collection assets. + */ + public function getAll() { + return $this->state->get('system.js_cache_files', []); + } + } diff --git a/core/lib/Drupal/Core/Asset/JsCollectionOptimizerTrait.php b/core/lib/Drupal/Core/Asset/JsCollectionOptimizerTrait.php deleted file mode 100644 index 9bea1a8..0000000 --- a/core/lib/Drupal/Core/Asset/JsCollectionOptimizerTrait.php +++ /dev/null @@ -1,78 +0,0 @@ -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); - } - - /** - * Deletes all optimized asset collections assets. - */ - public function deleteAll() { - $this->state->delete('system.js_cache_files'); - $delete_stale = function($uri) { - // Default stale file threshold is 30 days. - if (REQUEST_TIME - filemtime($uri) > \Drupal::config('system.performance')->get('stale_file_threshold')) { - file_unmanaged_delete($uri); - } - }; - file_scan_directory('public://js', '/.*/', array('callback' => $delete_stale)); - } - - /** - * Returns all optimized asset collections assets. - * - * @return string[] - * URIs for all optimized asset collection assets. - */ - public function getAll() { - return $this->state->get('system.js_cache_files', []); - } - - -}