Version 1
[yaffs-website] / web / modules / contrib / devel / src / EventSubscriber / ThemeInfoRebuildSubscriber.php
1 <?php
2
3 namespace Drupal\devel\EventSubscriber;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Extension\ThemeHandlerInterface;
7 use Drupal\Core\Session\AccountProxyInterface;
8 use Drupal\Core\StringTranslation\StringTranslationTrait;
9 use Drupal\Core\Url;
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;
14
15 /**
16  * Subscriber for force the system to rebuild the theme registry.
17  */
18 class ThemeInfoRebuildSubscriber implements EventSubscriberInterface {
19
20   use StringTranslationTrait;
21
22   /**
23    * Internal flag for handle user notification.
24    *
25    * @var string
26    */
27   protected $notificationFlag = 'devel.rebuild_theme_warning';
28
29   /**
30    * The devel config.
31    *
32    * @var \Drupal\Core\Config\Config;
33    */
34   protected $config;
35
36   /**
37    * The current user.
38    *
39    * @var \Drupal\Core\Session\AccountProxyInterface
40    */
41   protected $account;
42
43   /**
44    * The theme handler.
45    *
46    * @var \Drupal\Core\Extension\ThemeHandlerInterface
47    */
48   protected $themeHandler;
49
50   /**
51    * Constructs a ThemeInfoRebuildSubscriber object.
52    *
53    * @param \Drupal\Core\Config\ConfigFactoryInterface $config
54    *   The config factory.
55    * @param \Drupal\Core\Session\AccountProxyInterface $account
56    *   The current user.
57    * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
58    *   The theme handler.
59    */
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;
64   }
65
66   /**
67    * Forces the system to rebuild the theme registry.
68    *
69    * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
70    *   The event to process.
71    */
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());
80     }
81   }
82
83   /**
84    * Notifies the user that the theme info are rebuilt on every request.
85    *
86    * The warning message is shown only to users with adequate permissions and
87    * only once per session.
88    *
89    * @param \Symfony\Component\HttpFoundation\Request $request
90    *   The request.
91    */
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);
99       }
100     }
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public static function getSubscribedEvents() {
107     // Set high priority value to start as early as possible.
108     $events[KernelEvents::REQUEST][] = ['rebuildThemeInfo', 256];
109     return $events;
110   }
111
112 }