View source
<?php
namespace Drupal\advagg\Asset;
use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
use Drupal\Core\Asset\AssetResolver as CoreAssetResolver;
use Drupal\Core\Asset\AssetResolverInterface;
use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Asset\LibraryDiscoveryInterface;
use Drupal\Core\Asset\LibraryDependencyResolverInterface;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\State\StateInterface;
class AssetResolver extends CoreAssetResolver implements AssetResolverInterface {
protected $cssCollectionOptimizer;
protected $jsCollectionOptimizer;
protected $advaggFiles;
public function __construct(LibraryDiscoveryInterface $library_discovery, LibraryDependencyResolverInterface $library_dependency_resolver, ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager, LanguageManagerInterface $language_manager, CacheBackendInterface $cache, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer, StateInterface $advagg_files) {
$this->libraryDiscovery = $library_discovery;
$this->libraryDependencyResolver = $library_dependency_resolver;
$this->moduleHandler = $module_handler;
$this->themeManager = $theme_manager;
$this->languageManager = $language_manager;
$this->cache = $cache;
$this->cssCollectionOptimizer = $css_collection_optimizer;
$this->jsCollectionOptimizer = $js_collection_optimizer;
$this->advaggFiles = $advagg_files;
}
public function getCssAssets(AttachedAssetsInterface $assets, $optimize) {
$theme_info = $this->themeManager
->getActiveTheme();
$libraries_to_load = $this
->getLibrariesToLoad($assets);
$cid = 'css:' . $theme_info
->getName() . ':' . Crypt::hashBase64(serialize($libraries_to_load)) . (int) $optimize;
if ($cached = $this->cache
->get($cid)) {
return $cached->data;
}
$css = [];
$default_options = [
'type' => 'file',
'group' => CSS_AGGREGATE_DEFAULT,
'weight' => 0,
'media' => 'all',
'preprocess' => TRUE,
'browsers' => [],
];
foreach ($libraries_to_load as $library) {
list($extension, $name) = explode('/', $library, 2);
$definition = $this->libraryDiscovery
->getLibraryByName($extension, $name);
if (isset($definition['css'])) {
foreach ($definition['css'] as $options) {
$options += $default_options;
$options['browsers'] += [
'IE' => TRUE,
'!IE' => TRUE,
];
if ($options['type'] === 'file' && $options['preprocess'] && strpos($options['data'], '?') !== FALSE) {
$options['preprocess'] = FALSE;
}
$options['weight'] += count($css) / 1000;
$css[$options['data']] = $options;
}
}
}
$this->moduleHandler
->alter('css', $css, $assets);
$this->themeManager
->alter('css', $css, $assets);
$this->advaggFiles
->getMultiple(array_column($css, 'data'));
uasort($css, 'static::sort');
if ($stylesheet_remove = $theme_info
->getStyleSheetsRemove()) {
foreach ($css as $key => $options) {
if (isset($stylesheet_remove[$key])) {
unset($css[$key]);
}
}
}
if ($optimize) {
$css = $this->cssCollectionOptimizer
->optimize($css);
}
$this->cache
->set($cid, $css, CacheBackendInterface::CACHE_PERMANENT, [
'library_info',
]);
return $css;
}
public function getJsAssets(AttachedAssetsInterface $assets, $optimize) {
$theme_info = $this->themeManager
->getActiveTheme();
$libraries_to_load = $this
->getLibrariesToLoad($assets);
$cid = 'js:' . $theme_info
->getName() . ':' . $this->languageManager
->getCurrentLanguage()
->getId() . ':' . Crypt::hashBase64(serialize($libraries_to_load)) . (int) (count($assets
->getSettings()) > 0) . (int) $optimize;
if ($cached = $this->cache
->get($cid)) {
list($js_assets_header, $js_assets_footer, $settings, $settings_in_header) = $cached->data;
}
else {
$javascript = [];
$default_options = [
'type' => 'file',
'group' => JS_DEFAULT,
'weight' => 0,
'cache' => TRUE,
'preprocess' => TRUE,
'attributes' => [],
'version' => NULL,
'browsers' => [],
];
$header_js_libraries = [];
foreach ($libraries_to_load as $library) {
list($extension, $name) = explode('/', $library, 2);
$definition = $this->libraryDiscovery
->getLibraryByName($extension, $name);
if (isset($definition['js']) && !empty($definition['header'])) {
$header_js_libraries[] = $library;
}
}
$header_js_libraries = $this->libraryDependencyResolver
->getLibrariesWithDependencies($header_js_libraries);
foreach ($libraries_to_load as $library) {
list($extension, $name) = explode('/', $library, 2);
$definition = $this->libraryDiscovery
->getLibraryByName($extension, $name);
if (isset($definition['js'])) {
foreach ($definition['js'] as $options) {
$options += $default_options;
$options['scope'] = in_array($library, $header_js_libraries) ? 'header' : 'footer';
$options['preprocess'] = $options['cache'] && empty($options['attributes']) ? $options['preprocess'] : FALSE;
$options['weight'] += count($javascript) / 1000;
$javascript[$options['data']] = $options;
}
}
}
$this->moduleHandler
->alter('js', $javascript, $assets);
$this->themeManager
->alter('js', $javascript, $assets);
$this->advaggFiles
->getMultiple(array_column($javascript, 'data'));
uasort($javascript, 'static::sort');
$js_assets_header = [];
$js_assets_footer = [];
foreach ($javascript as $key => $item) {
if ($item['scope'] == 'header') {
$js_assets_header[$key] = $item;
}
elseif ($item['scope'] == 'footer') {
$js_assets_footer[$key] = $item;
}
}
if ($optimize) {
$js_assets_header = $this->jsCollectionOptimizer
->optimize($js_assets_header);
$js_assets_footer = $this->jsCollectionOptimizer
->optimize($js_assets_footer);
}
$libraries_to_load = $this
->getLibrariesToLoad($assets);
$settings_required = in_array('core/drupalSettings', $libraries_to_load) || in_array('core/drupalSettings', $this->libraryDependencyResolver
->getLibrariesWithDependencies($assets
->getAlreadyLoadedLibraries()));
$settings_have_changed = count($libraries_to_load) > 0 || count($assets
->getSettings()) > 0;
$settings = FALSE;
if ($settings_required && $settings_have_changed) {
$settings = $this
->getJsSettingsAssets($assets);
foreach ($this->moduleHandler
->getImplementations('js_settings_build') as $module) {
$function = $module . '_js_settings_build';
$function($settings, $assets);
}
}
$settings_in_header = in_array('core/drupalSettings', $header_js_libraries);
$this->cache
->set($cid, [
$js_assets_header,
$js_assets_footer,
$settings,
$settings_in_header,
], CacheBackendInterface::CACHE_PERMANENT, [
'library_info',
]);
}
if ($settings !== FALSE) {
$settings = NestedArray::mergeDeepArray([
$settings,
$assets
->getSettings(),
], TRUE);
$this->moduleHandler
->alter('js_settings', $settings, $assets);
$this->themeManager
->alter('js_settings', $settings, $assets);
$assets
->setSettings($settings);
$settings_as_inline_javascript = [
'type' => 'setting',
'group' => JS_SETTING,
'weight' => 0,
'browsers' => [],
'data' => $settings,
];
$settings_js_asset = [
'drupalSettings' => $settings_as_inline_javascript,
];
if ($settings_in_header) {
$js_assets_header = $settings_js_asset + $js_assets_header;
}
else {
$js_assets_footer = $settings_js_asset + $js_assets_footer;
}
}
return [
$js_assets_header,
$js_assets_footer,
];
}
}