Same name in other branches
  1. 5.0.x advagg_mod/src/Asset/DeferJs.php \Drupal\advagg_mod\Asset\DeferJs
  2. 8.x-3.x advagg_mod/src/Asset/DeferJs.php \Drupal\advagg_mod\Asset\DeferJs
  3. 8.x-4.x advagg_mod/src/Asset/DeferJs.php \Drupal\advagg_mod\Asset\DeferJs

Add defer tag to scripts.

Hierarchy

  • class \Drupal\advagg_mod\Asset\DeferJs

Expanded class hierarchy of DeferJs

1 file declares its use of DeferJs
InitSubscriber.php dans advagg_mod/src/EventSubscriber/InitSubscriber.php
2 string references to 'DeferJs'
advagg_mod.services.yml dans advagg_mod/advagg_mod.services.yml
advagg_mod/advagg_mod.services.yml
InitSubscriber::getSubscribedEvents dans advagg_mod/src/EventSubscriber/InitSubscriber.php
1 service uses DeferJs
advagg_mod.defer_js dans advagg_mod/advagg_mod.services.yml
Drupal\advagg_mod\Asset\DeferJs

Fichier

advagg_mod/src/Asset/DeferJs.php, line 13

Namespace

Drupal\advagg_mod\Asset
View source
class DeferJs {
    
    /**
     * The defer type to use from advagg_mod configuration.
     *
     * @var int
     */
    protected $deferType;
    
    /**
     * The global count to use from advagg configuration.
     *
     * @var int
     */
    protected $counter;
    
    /**
     * The Drupal module handler.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface
     */
    protected $moduleHandler;
    
    /**
     * The module extension list service.
     *
     * @var \Drupal\Core\Extension\ExtensionList
     */
    protected $moduleExtensionList;
    
    /**
     * A list of file cids to skip.
     *
     * @var array
     */
    protected $skipList;
    
    /**
     * DeferCss constructor.
     *
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The config factory.
     * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
     *   The Drupal module handler.
     * @param ExtensionList $module_extension_list
     *   The module extension list service.
     */
    public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, ExtensionList $module_extension_list) {
        $this->deferType = $config_factory->get('advagg_mod.settings')
            ->get('css_defer_js_code');
        $this->counter = $config_factory->get('advagg.settings')
            ->get('global_counter');
        $this->moduleHandler = $module_handler;
        $this->skipList = [];
        $this->moduleExtensionList = $module_extension_list;
        // Admin Toolbar 8x fails when deferred.
        if ($this->moduleHandler
            ->moduleExists('admin_toolbar')) {
            $this->skipList[] = Crypt::hashBase64($this->moduleExtensionList
                ->getPath('admin_toolbar') . '/js/admin_toolbar.js' . $this->counter);
        }
    }
    
    /**
     * Add defer attribute to script tags.
     *
     * @param string $content
     *   The response content.
     *
     * @return string
     *   Updated content.
     */
    public function defer($content) {
        // Admin Toolbar 8x fails when deferred.
        foreach ($this->skipList as $cid) {
            if (strstr($content, $cid)) {
                return $content;
            }
        }
        // Only defer local scripts.
        if ($this->deferType === 2) {
            $pattern = '/<script src="\\/[a-zA-Z0-0].*"/';
        }
        else {
            $pattern = '/<script src=".*"/';
        }
        return preg_replace_callback($pattern, [
            $this,
            'callback',
        ], $content);
    }
    
    /**
     * Callback to replace individual stylesheet links.
     *
     * @param array $matches
     *   Array from matches from preg_replace_callback.
     *
     * @return string
     *   Updated html string.
     */
    protected function callback(array $matches) {
        return "{$matches[0]} defer";
    }

}

Members

Titre Trier par ordre décroissant Modifiers Object type Résumé
DeferJs::$counter protected property The global count to use from advagg configuration.
DeferJs::$deferType protected property The defer type to use from advagg_mod configuration.
DeferJs::$moduleExtensionList protected property The module extension list service.
DeferJs::$moduleHandler protected property The Drupal module handler.
DeferJs::$skipList protected property A list of file cids to skip.
DeferJs::callback protected function Callback to replace individual stylesheet links.
DeferJs::defer public function Add defer attribute to script tags.
DeferJs::__construct public function DeferCss constructor.