class ThemeInfoRebuildSubscriber

Same name and namespace in other branches
  1. 4.x src/EventSubscriber/ThemeInfoRebuildSubscriber.php \Drupal\devel\EventSubscriber\ThemeInfoRebuildSubscriber

Subscriber for force the system to rebuild the theme registry.

Hierarchy

Expanded class hierarchy of ThemeInfoRebuildSubscriber

1 string reference to 'ThemeInfoRebuildSubscriber'
devel.services.yml in ./devel.services.yml
devel.services.yml
1 service uses ThemeInfoRebuildSubscriber
devel.theme_rebuild_subscriber in ./devel.services.yml
Drupal\devel\EventSubscriber\ThemeInfoRebuildSubscriber

File

src/EventSubscriber/ThemeInfoRebuildSubscriber.php, line 22

Namespace

Drupal\devel\EventSubscriber
View source
class ThemeInfoRebuildSubscriber implements EventSubscriberInterface {
    use StringTranslationTrait;
    
    /**
     * Internal flag for handle user notification.
     */
    protected string $notificationFlag = 'devel.rebuild_theme_warning';
    
    /**
     * The devel config.
     */
    protected Config $config;
    
    /**
     * The current user.
     */
    protected AccountProxyInterface $account;
    
    /**
     * The theme handler.
     */
    protected ThemeHandlerInterface $themeHandler;
    
    /**
     * The messenger.
     */
    protected MessengerInterface $messenger;
    
    /**
     * The theme registry.
     */
    protected Registry $themeRegistry;
    
    /**
     * Constructs a ThemeInfoRebuildSubscriber object.
     *
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config
     *   The config factory.
     * @param \Drupal\Core\Session\AccountProxyInterface $account
     *   The current user.
     * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
     *   The theme handler.
     * @param \Drupal\Core\Messenger\MessengerInterface $messenger
     *   The messenger.
     * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
     *   The translation manager.
     * @param \Drupal\Core\Theme\Registry $theme_registry
     *   The theme registry.
     */
    public function __construct(ConfigFactoryInterface $config, AccountProxyInterface $account, ThemeHandlerInterface $theme_handler, MessengerInterface $messenger, TranslationInterface $string_translation, Registry $theme_registry) {
        $this->config = $config->get('devel.settings');
        $this->account = $account;
        $this->themeHandler = $theme_handler;
        $this->messenger = $messenger;
        $this->stringTranslation = $string_translation;
        $this->themeRegistry = $theme_registry;
    }
    
    /**
     * Forces the system to rebuild the theme registry.
     *
     * @param \Symfony\Component\HttpKernel\Event\RequestEvent $event
     *   The event to process.
     */
    public function rebuildThemeInfo(RequestEvent $event) : void {
        if ($this->config
            ->get('rebuild_theme')) {
            // Update the theme registry.
            $this->themeRegistry
                ->reset();
            // Refresh theme data.
            $this->themeHandler
                ->refreshInfo();
            // Resets the internal state of the theme handler and clear the 'system
            // list' cache; this allow to properly register, if needed, PSR-4
            // namespaces for theme extensions after refreshing the info data.
            $this->themeHandler
                ->reset();
            // Notify the user that the theme info are rebuilt on every request.
            $this->triggerWarningIfNeeded($event->getRequest());
        }
    }
    
    /**
     * Notifies the user that the theme info are rebuilt on every request.
     *
     * The warning message is shown only to users with adequate permissions and
     * only once per session.
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   The request.
     */
    protected function triggerWarningIfNeeded(Request $request) {
        if ($this->account
            ->hasPermission('access devel information')) {
            $session = $request->getSession();
            if (!$session->has($this->notificationFlag)) {
                $session->set($this->notificationFlag, TRUE);
                $message = $this->t('The theme information is being rebuilt on every request. Remember to <a href=":url">turn off</a> this feature on production websites.', [
                    ':url' => Url::fromRoute('devel.admin_settings')->toString(),
                ]);
                $this->messenger
                    ->addWarning($message);
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        // Set high priority value to start as early as possible.
        $events[KernelEvents::REQUEST][] = [
            'rebuildThemeInfo',
            256,
        ];
        return $events;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
ThemeInfoRebuildSubscriber::$account protected property The current user.
ThemeInfoRebuildSubscriber::$config protected property The devel config.
ThemeInfoRebuildSubscriber::$messenger protected property The messenger.
ThemeInfoRebuildSubscriber::$notificationFlag protected property Internal flag for handle user notification.
ThemeInfoRebuildSubscriber::$themeHandler protected property The theme handler.
ThemeInfoRebuildSubscriber::$themeRegistry protected property The theme registry.
ThemeInfoRebuildSubscriber::getSubscribedEvents public static function
ThemeInfoRebuildSubscriber::rebuildThemeInfo public function Forces the system to rebuild the theme registry.
ThemeInfoRebuildSubscriber::triggerWarningIfNeeded protected function Notifies the user that the theme info are rebuilt on every request.
ThemeInfoRebuildSubscriber::__construct public function Constructs a ThemeInfoRebuildSubscriber object.