View source
<?php
namespace Drupal\lazy;
use Drupal\Core\Condition\ConditionManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Routing\AdminContext;
use Symfony\Component\HttpFoundation\RequestStack;
class Lazy implements LazyInterface {
protected $lazySettings;
protected $requestStack;
protected $conditionManager;
protected $adminContext;
protected $moduleHandler;
public function __construct(ConfigFactoryInterface $config_factory, RequestStack $request_stack, ConditionManager $condition_manager, AdminContext $admin_context, ModuleHandler $module_handler) {
$this->lazySettings = $config_factory
->get('lazy.settings')
->get();
$this->requestStack = $request_stack
->getCurrentRequest();
$this->conditionManager = $condition_manager;
$this->adminContext = $admin_context;
$this->moduleHandler = $module_handler;
}
public function getSettings() : array {
$this->moduleHandler
->alter('lazy_settings', $this->lazySettings);
return $this->lazySettings ?? [];
}
public function getPlugins() : array {
return [
'artdirect' => 'artdirect/ls.artdirect',
'aspectratio' => 'aspectratio/ls.aspectratio',
'attrchange' => 'attrchange/ls.attrchange',
'bgset' => 'bgset/ls.bgset',
'blur-up' => 'blur-up/ls.blur-up',
'custommedia' => 'custommedia/ls.custommedia',
'fix-edge-h-descriptor' => 'fix-edge-h-descriptor/ls.fix-edge-h-descriptor',
'fix-ios-sizes' => 'fix-ios-sizes/fix-ios-sizes',
'include' => 'include/ls.include',
'native-loading' => 'native-loading/ls.native-loading',
'noscript' => 'noscript/ls.noscript',
'object-fit' => 'object-fit/ls.object-fit',
'optimumx' => 'optimumx/ls.optimumx',
'parent-fit' => 'parent-fit/ls.parent-fit',
'print' => 'print/ls.print',
'progressive' => 'progressive/ls.progressive',
'respimg' => 'respimg/ls.respimg',
'rias' => 'rias/ls.rias',
'static-gecko-picture' => 'static-gecko-picture/ls.static-gecko-picture',
'twitter' => 'twitter/ls.twitter',
'unload' => 'unload/ls.unload',
'unveilhooks' => 'unveilhooks/ls.unveilhooks',
'video-embed' => 'video-embed/ls.video-embed',
];
}
public function isEnabled(array $attributes = []) : bool {
return $this
->isPathAllowed() && $this
->isNotSkipping($attributes);
}
public function isPathAllowed() : bool {
if ($this->requestStack->query
->has('amp')) {
return FALSE;
}
$settings = $this
->getSettings();
$disable_admin = !isset($settings['disable_admin']) || (bool) $settings['disable_admin'];
if ($disable_admin && $this->adminContext
->isAdminRoute()) {
return FALSE;
}
$condition = $this->conditionManager
->createInstance('request_path');
if (is_null($condition)) {
return FALSE;
}
$visibility = $settings['visibility'] ?? [
'id' => 'request_path',
'pages' => $settings['disabled_paths'] ?? '/rss.xml',
'negate' => 0,
];
$condition
->setConfiguration($visibility);
if ($condition
->isNegated()) {
return $condition
->evaluate();
}
return !$condition
->evaluate();
}
private function isNotSkipping(array $attributes = []) : bool {
$classes = $attributes['class'] ?? [];
return !in_array($this->lazySettings['skipClass'], $classes, TRUE);
}
}