class ThemeInfoRebuildSubscriber

Same name in other branches
  1. 5.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 19

Namespace

Drupal\devel\EventSubscriber
View source
class ThemeInfoRebuildSubscriber implements EventSubscriberInterface {
    use StringTranslationTrait;
    use MessengerTrait;
    
    /**
     * Internal flag for handle user notification.
     *
     * @var string
     */
    protected $notificationFlag = 'devel.rebuild_theme_warning';
    
    /**
     * The devel config.
     *
     * @var \Drupal\Core\Config\Config
     */
    protected $config;
    
    /**
     * The current user.
     *
     * @var \Drupal\Core\Session\AccountProxyInterface
     */
    protected $account;
    
    /**
     * The theme handler.
     *
     * @var \Drupal\Core\Extension\ThemeHandlerInterface
     */
    protected $themeHandler;
    
    /**
     * 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.
     */
    public function __construct(ConfigFactoryInterface $config, AccountProxyInterface $account, ThemeHandlerInterface $theme_handler) {
        $this->config = $config->get('devel.settings');
        $this->account = $account;
        $this->themeHandler = $theme_handler;
    }
    
    /**
     * Forces the system to rebuild the theme registry.
     *
     * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
     *   The event to process.
     */
    public function rebuildThemeInfo(GetResponseEvent $event) {
        if ($this->config
            ->get('rebuild_theme')) {
            // Update the theme registry.
            drupal_theme_rebuild();
            // 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 && $this->account
            ->hasPermission('access devel information')) {
            $session = $request->getSession();
            if ($session && !$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() {
        // 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
MessengerTrait::$messenger protected property The messenger. 17
MessengerTrait::messenger public function Gets the messenger. 17
MessengerTrait::setMessenger public function Sets the messenger.
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::$notificationFlag protected property Internal flag for handle user notification.
ThemeInfoRebuildSubscriber::$themeHandler protected property The theme handler.
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.