CssCollectionRenderer.php in Critical CSS 8
File
src/Asset/CssCollectionRenderer.php
View source
<?php
namespace Drupal\critical_css\Asset;
use Drupal\Core\Asset\AssetCollectionRendererInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Render\Markup;
use Drupal\Component\Utility\Html;
use Drupal\Core\Template\TwigEnvironment;
class CssCollectionRenderer implements AssetCollectionRendererInterface {
protected $cssCollectionRenderer;
protected $configFactory;
protected $twig;
protected $criticalCssProvider;
public function __construct(AssetCollectionRendererInterface $css_collection_renderer, ConfigFactoryInterface $config_factory, TwigEnvironment $twig, CriticalCssProviderInterface $critical_css_provider) {
$this->cssCollectionRenderer = $css_collection_renderer;
$this->configFactory = $config_factory;
$this->twig = $twig;
$this->criticalCssProvider = $critical_css_provider;
}
public function render(array $css_assets) {
if (empty($css_assets)) {
return [];
}
$css_assets = $this->cssCollectionRenderer
->render($css_assets);
$config = $this->configFactory
->get('critical_css.settings');
if (!$this->criticalCssProvider
->isEnabled() || $this->criticalCssProvider
->isAlreadyProcessed()) {
return $css_assets;
}
$new_css_assets = [];
$criticalCssAsset = $this
->getCriticalCssAsset();
if ($criticalCssAsset) {
$new_css_assets[] = $criticalCssAsset;
}
$criticalCss = $this->criticalCssProvider
->getCriticalCss();
if ($criticalCss) {
$asyncAssets = $this
->makeAssetsAsync($css_assets);
$new_css_assets = array_merge($new_css_assets, $asyncAssets);
}
else {
$new_css_assets = array_merge($new_css_assets, $css_assets);
}
return $new_css_assets;
}
protected function getCriticalCssAsset() {
$criticalCssAsset = NULL;
$debugInfo = $this
->getDebugInfo();
$criticalCss = $this->criticalCssProvider
->getCriticalCss();
if ($criticalCss || $debugInfo['start'] || $debugInfo['end']) {
$criticalCssAsset = [
'#type' => 'html_tag',
'#tag' => 'style',
'#attributes' => [
'id' => 'critical-css',
],
'#value' => Markup::create($debugInfo['start'] . $criticalCss . $debugInfo['end']),
];
}
return $criticalCssAsset;
}
protected function makeAssetsAsync(array $css_assets) {
$asyncAssets = [];
foreach ($css_assets as $asset) {
if ($asset['#attributes']['media'] === 'print') {
$asyncAssets[] = $asset;
}
else {
if ($this->configFactory
->get('critical_css.settings')
->get('preload_non_critical_css')) {
$preloadAsset = $asset;
$preloadAsset['#attributes']['rel'] = 'preload';
$preloadAsset['#attributes']['as'] = 'style';
unset($preloadAsset['#attributes']['media']);
$asyncAssets[] = $preloadAsset;
}
$onLoadAsset = $asset;
$onLoadAsset['#attributes']['media'] = 'print';
$onLoadAsset['#attributes']['data-onload-media'] = 'all';
$onLoadAsset['#attributes']['onload'] = 'this.onload=null;this.media=this.dataset.onloadMedia';
$asyncAssets[] = $onLoadAsset;
$noScriptAsset = $asset;
$noScriptAsset['#noscript'] = TRUE;
$asyncAssets[] = $noScriptAsset;
}
}
return $asyncAssets;
}
protected function getDebugInfo() {
$matchedFilePath = $this->criticalCssProvider
->getMatchedFilePath();
$debugInfoStart = NULL;
$debugInfoEnd = NULL;
if ($this->twig
->isDebug()) {
$filePaths = $this->criticalCssProvider
->getFilePaths();
$debugInfoStart = "\n/* CRITICAL CSS DEBUG */\n";
$debugInfoStart .= "/* FILE NAME SUGGESTIONS:\n";
foreach ($filePaths as $filePath) {
$flag = $filePath === $matchedFilePath ? 'x' : '*';
$debugInfoStart .= "\t {$flag} {$filePath}\n";
}
$debugInfoStart .= "*/\n";
if ($matchedFilePath) {
$debugInfoStart .= '/* BEGIN OUTPUT from ' . Html::escape($matchedFilePath) . " */\n";
$debugInfoEnd = "\n/* END OUTPUT from " . Html::escape($matchedFilePath) . " */\n";
}
else {
$debugInfoStart .= "/* NONE MATCHED. THIS USUALLY HAPPENS WHEN YOU ARE LOGGED IN. LOG OUT AND TRY AGAIN AS AN ANONYMOUS USER. */\n";
}
}
return [
'start' => $debugInfoStart,
'end' => $debugInfoEnd,
];
}
}