3 namespace Drupal\devel\EventSubscriber;
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Extension\ThemeHandlerInterface;
7 use Drupal\Core\Session\AccountProxyInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11 use Symfony\Component\HttpFoundation\Request;
12 use Symfony\Component\HttpKernel\Event\GetResponseEvent;
13 use Symfony\Component\HttpKernel\KernelEvents;
16 * Subscriber for force the system to rebuild the theme registry.
18 class ThemeInfoRebuildSubscriber implements EventSubscriberInterface {
20 use StringTranslationTrait;
23 * Internal flag for handle user notification.
27 protected $notificationFlag = 'devel.rebuild_theme_warning';
32 * @var \Drupal\Core\Config\Config;
39 * @var \Drupal\Core\Session\AccountProxyInterface
46 * @var \Drupal\Core\Extension\ThemeHandlerInterface
48 protected $themeHandler;
51 * Constructs a ThemeInfoRebuildSubscriber object.
53 * @param \Drupal\Core\Config\ConfigFactoryInterface $config
55 * @param \Drupal\Core\Session\AccountProxyInterface $account
57 * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
60 public function __construct(ConfigFactoryInterface $config, AccountProxyInterface $account, ThemeHandlerInterface $theme_handler) {
61 $this->config = $config->get('devel.settings');
62 $this->account = $account;
63 $this->themeHandler = $theme_handler;
67 * Forces the system to rebuild the theme registry.
69 * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
70 * The event to process.
72 public function rebuildThemeInfo(GetResponseEvent $event) {
73 if ($this->config->get('rebuild_theme')) {
74 // Update the theme registry.
75 drupal_theme_rebuild();
76 // Refresh theme data.
77 $this->themeHandler->refreshInfo();
78 // Notify the user that the theme info are rebuilt on every request.
79 $this->triggerWarningIfNeeded($event->getRequest());
84 * Notifies the user that the theme info are rebuilt on every request.
86 * The warning message is shown only to users with adequate permissions and
87 * only once per session.
89 * @param \Symfony\Component\HttpFoundation\Request $request
92 protected function triggerWarningIfNeeded(Request $request) {
93 if ($this->account && $this->account->hasPermission('access devel information')) {
94 $session = $request->getSession();
95 if (!$session->has($this->notificationFlag)) {
96 $session->set($this->notificationFlag, TRUE);
97 $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()]);
98 drupal_set_message($message, 'warning', TRUE);
106 public static function getSubscribedEvents() {
107 // Set high priority value to start as early as possible.
108 $events[KernelEvents::REQUEST][] = ['rebuildThemeInfo', 256];