ThemeInfoRebuildSubscriber.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. namespace Drupal\devel\EventSubscriber;
  3. use Drupal\Core\Config\ConfigFactoryInterface;
  4. use Drupal\Core\Extension\ThemeHandlerInterface;
  5. use Drupal\Core\Session\AccountProxyInterface;
  6. use Drupal\Core\StringTranslation\StringTranslationTrait;
  7. use Drupal\Core\Url;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. /**
  13. * Subscriber for force the system to rebuild the theme registry.
  14. */
  15. class ThemeInfoRebuildSubscriber implements EventSubscriberInterface {
  16. use StringTranslationTrait;
  17. /**
  18. * Internal flag for handle user notification.
  19. *
  20. * @var string
  21. */
  22. protected $notificationFlag = 'devel.rebuild_theme_warning';
  23. /**
  24. * The devel config.
  25. *
  26. * @var \Drupal\Core\Config\Config;
  27. */
  28. protected $config;
  29. /**
  30. * The current user.
  31. *
  32. * @var \Drupal\Core\Session\AccountProxyInterface
  33. */
  34. protected $account;
  35. /**
  36. * The theme handler.
  37. *
  38. * @var \Drupal\Core\Extension\ThemeHandlerInterface
  39. */
  40. protected $themeHandler;
  41. /**
  42. * Constructs a ThemeInfoRebuildSubscriber object.
  43. *
  44. * @param \Drupal\Core\Config\ConfigFactoryInterface $config
  45. * The config factory.
  46. * @param \Drupal\Core\Session\AccountProxyInterface $account
  47. * The current user.
  48. * @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
  49. * The theme handler.
  50. */
  51. public function __construct(ConfigFactoryInterface $config, AccountProxyInterface $account, ThemeHandlerInterface $theme_handler) {
  52. $this->config = $config->get('devel.settings');
  53. $this->account = $account;
  54. $this->themeHandler = $theme_handler;
  55. }
  56. /**
  57. * Forces the system to rebuild the theme registry.
  58. *
  59. * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
  60. * The event to process.
  61. */
  62. public function rebuildThemeInfo(GetResponseEvent $event) {
  63. if ($this->config->get('rebuild_theme')) {
  64. // Update the theme registry.
  65. drupal_theme_rebuild();
  66. // Refresh theme data.
  67. $this->themeHandler->refreshInfo();
  68. // Resets the internal state of the theme handler and clear the 'system
  69. // list' cache; this allow to properly register, if needed, PSR-4
  70. // namespaces for theme extensions after refreshing the info data.
  71. $this->themeHandler->reset();
  72. // Notify the user that the theme info are rebuilt on every request.
  73. $this->triggerWarningIfNeeded($event->getRequest());
  74. }
  75. }
  76. /**
  77. * Notifies the user that the theme info are rebuilt on every request.
  78. *
  79. * The warning message is shown only to users with adequate permissions and
  80. * only once per session.
  81. *
  82. * @param \Symfony\Component\HttpFoundation\Request $request
  83. * The request.
  84. */
  85. protected function triggerWarningIfNeeded(Request $request) {
  86. if ($this->account && $this->account->hasPermission('access devel information')) {
  87. $session = $request->getSession();
  88. if ($session && !$session->has($this->notificationFlag)) {
  89. $session->set($this->notificationFlag, TRUE);
  90. $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()]);
  91. drupal_set_message($message, 'warning', TRUE);
  92. }
  93. }
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public static function getSubscribedEvents() {
  99. // Set high priority value to start as early as possible.
  100. $events[KernelEvents::REQUEST][] = ['rebuildThemeInfo', 256];
  101. return $events;
  102. }
  103. }