View source
<?php
namespace Drupal\advagg\Asset;
use Drupal\Core\Asset\AssetCollectionGrouperInterface;
use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
use Drupal\Core\Asset\AssetDumperInterface;
use Drupal\Core\Asset\AssetOptimizerInterface;
use Drupal\Core\Asset\CssCollectionOptimizer as CoreCssCollectionOptimizer;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CssCollectionOptimizer extends CoreCssCollectionOptimizer implements AssetCollectionOptimizerInterface {
protected $config;
protected $systemConfig;
protected $advaggAggregates;
protected $moduleHandler;
protected $advaggFiles;
protected $settingsHash;
protected $requestStack;
public function __construct(AssetCollectionGrouperInterface $grouper, AssetOptimizerInterface $optimizer, AssetDumperInterface $dumper, StateInterface $state, ConfigFactoryInterface $config_factory, StateInterface $advagg_aggregates, ModuleHandlerInterface $module_handler, StateInterface $advagg_files, RequestStack $request_stack) {
$this->grouper = $grouper;
$this->optimizer = $optimizer;
$this->dumper = $dumper;
$this->state = $state;
$this->config = $config_factory
->get('advagg.settings');
$this->systemConfig = $config_factory
->get('system.performance');
$this->advaggAggregates = $advagg_aggregates;
$this->moduleHandler = $module_handler;
$this->advaggFiles = $advagg_files;
$this->settingsHash = '_' . advagg_get_current_hooks_hash();
$this->requestStack = $request_stack;
}
public function optimize(array $css_assets) {
$css_groups = $this->grouper
->group($css_assets);
$css_assets = [];
$protocol_relative = $this->config
->get('path.convert.absolute_to_protocol_relative');
$force_https = $this->config
->get('path.convert.force_https');
$combine_media = $this->config
->get('css.combine_media');
$page_uri = $this->requestStack
->getCurrentRequest()
->getUri();
foreach ($css_groups as $order => $css_group) {
$css_assets[$order] = $css_group;
unset($css_assets[$order]['items']);
switch ($css_group['type']) {
case 'file':
if (!$css_group['preprocess']) {
$css_assets[$order]['data'] = $css_group['items'][0]['data'];
}
else {
$key = $this
->generateHash($css_group) . $this->settingsHash;
$uri = '';
if ($aggregate = $this->advaggAggregates
->get($key)) {
$uri = $aggregate['uri'];
$aggregate['basic']['pages'][] = $page_uri;
$this->advaggAggregates
->set($key, $aggregate);
}
if (empty($uri) || !file_exists($uri)) {
$data = '';
$group_file_info = $this->advaggFiles
->getMultiple(array_column($css_group['items'], 'data'));
foreach ($group_file_info as &$file) {
$file['aggregates'][] = $key;
$file['pages'][] = $page_uri;
}
if (array_column($group_file_info, 'dns_prefetch')) {
$css_assets[$order]['dns_prefetch'] = [];
foreach ($group_file_info as $file) {
if (!empty($file['dns_prefetch'])) {
$css_assets[$order]['dns_prefetch'] = array_merge($css_assets[$order]['dns_prefetch'], $file['dns_prefetch']);
}
}
}
foreach ($css_group['items'] as $css_asset) {
$content = $this->optimizer
->optimize($css_asset);
if ($combine_media && isset($css_asset['media']) && $css_asset['media'] != 'all') {
$content = '@media ' . $css_asset['media'] . ' {' . $content . '}';
}
$this->moduleHandler
->alter('advagg_css_contents', $content, $css_asset, $group_file_info);
$data .= $content;
}
$regexp = '/@import[^;]+;/i';
preg_match_all($regexp, $data, $matches);
$data = preg_replace($regexp, '', $data);
$data = implode('', $matches[0]) . $data;
list($uri, $filename) = $this->dumper
->dump($data, 'css');
$css_assets[$order]['data'] = $uri;
$aggregate_info = [
'uri' => $uri,
'uid' => $filename,
'basic' => [
'files' => array_keys($css_group['items']),
'pages' => [
$page_uri,
],
],
'detailed' => [
'files' => $css_group['items'],
'hooks_hash' => advagg_current_hooks_hash_array(),
],
];
$this->advaggAggregates
->set($key, $aggregate_info);
}
else {
$css_assets[$order]['data'] = $uri;
}
$css_assets[$order]['preprocessed'] = TRUE;
}
break;
case 'external':
$uri = $css_group['items'][0]['data'];
if ($force_https) {
$uri = advagg_path_convert_force_https($uri);
}
elseif ($protocol_relative) {
$uri = advagg_path_convert_protocol_relative($uri);
}
$css_assets[$order]['data'] = $uri;
break;
}
}
return $css_assets;
}
protected function generateHash(array $css_group) {
$css_data = [];
foreach ($css_group['items'] as $css_file) {
$css_data[] = $css_file['data'];
$css_data[] = filemtime($css_file['data']);
}
return hash('sha256', serialize($css_data));
}
public function deleteAllReal() {
$log = [];
$this->state
->delete('system.css_cache_files');
Cache::invalidateTags([
'library_info',
]);
$delete_all = function ($uri) use (&$log) {
file_unmanaged_delete($uri);
$log[] = $uri;
};
$this->state
->delete('system.js_cache_files');
file_scan_directory($this->dumper
->preparePath('css'), '/.*/', [
'callback' => $delete_all,
]);
return $log;
}
public function deleteStale() {
$log = [];
$this->state
->delete('system.css_cache_files');
Cache::invalidateTags([
'library_info',
]);
$delete_stale = function ($uri) use (&$log) {
if (REQUEST_TIME - fileatime($uri) > $this->systemConfig
->get('stale_file_threshold')) {
file_unmanaged_delete($uri);
$log[] = $uri;
}
};
file_scan_directory($this->dumper
->preparePath('css'), '/.*/', [
'callback' => $delete_stale,
]);
return $log;
}
public function deleteOld() {
$log = [];
$this->state
->delete('system.css_cache_files');
Cache::invalidateTags([
'library_info',
]);
$delete_old = function ($uri) use (&$log) {
if (REQUEST_TIME - filemtime($uri) > $this->systemConfig
->get('stale_file_threshold') * 3) {
file_unmanaged_delete($uri);
$log[] = $uri;
}
};
file_scan_directory($this->dumper
->preparePath('css'), '/.*/', [
'callback' => $delete_old,
]);
return $log;
}
}