Commit d3d4eae8 authored by willzyx's avatar willzyx

Issue #2832882 by willzyx: Split DevelEventSubscriber into multiple classes

parent e693a591
services:
devel.event_subscriber:
class: Drupal\devel\EventSubscriber\DevelEventSubscriber
arguments: ['@config.factory', '@current_user', '@module_handler', '@url_generator']
devel.error_subscriber:
class: Drupal\devel\EventSubscriber\ErrorHandlerSubscriber
arguments: ['@current_user']
tags:
- { name: event_subscriber }
devel.theme_rebuild_subscriber:
class: Drupal\devel\EventSubscriber\ThemeInfoRebuildSubscriber
arguments: ['@config.factory', '@current_user', '@theme_handler']
tags:
- { name: event_subscriber }
devel.route_subscriber:
class: Drupal\devel\Routing\RouteSubscriber
arguments: ['@entity_type.manager']
tags:
- { name: event_subscriber }
......@@ -13,12 +25,6 @@ services:
class: Drupal\devel\DevelDumperManager
arguments: ['@config.factory', '@current_user', '@plugin.manager.devel_dumper']
devel.route_subscriber:
class: Drupal\devel\Routing\RouteSubscriber
arguments: ['@entity_type.manager']
tags:
- { name: event_subscriber }
devel.twig.debug_extension:
class: Drupal\devel\Twig\Extension\Debug
arguments: ['@devel.dumper']
......
<?php
namespace Drupal\devel\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class DevelEventSubscriber implements EventSubscriberInterface {
/**
* The devel.settings config object.
*
* @var \Drupal\Core\Config\Config;
*/
protected $config;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* @var \Drupal\Core\Routing\UrlGeneratorInterface
*/
protected $urlGenerator;
/**
* Constructs a DevelEventSubscriber object.
*/
public function __construct(ConfigFactoryInterface $config, AccountInterface $account, ModuleHandlerInterface $module_handler, UrlGeneratorInterface $url_generator) {
$this->config = $config->get('devel.settings');
$this->account = $account;
$this->moduleHandler = $module_handler;
$this->urlGenerator = $url_generator;
}
/**
* Register the devel error handler.
*
* @param \Symfony\Component\EventDispatcher\Event $event
* The event to process.
*/
public function registerErrorHandler(Event $event = NULL) {
if ($this->account && $this->account->hasPermission('access devel information')) {
devel_set_handler(devel_get_handlers());
}
}
/**
* Initializes devel module requirements.
*/
public function onRequest(GetResponseEvent $event) {
if ($this->config->get('rebuild_theme')) {
drupal_theme_rebuild();
// Ensure that the active theme object is cleared.
$theme_name = \Drupal::theme()->getActiveTheme()->getName();
\Drupal::state()->delete('theme.active_theme.' . $theme_name);
\Drupal::theme()->resetActiveTheme();
/** @var \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler*/
$theme_handler = \Drupal::service('theme_handler');
$theme_handler->refreshInfo();
// @todo This is not needed after https://www.drupal.org/node/2330755
$list = $theme_handler->listInfo();
$theme_handler->addTheme($list[$theme_name]);
if (\Drupal::service('flood')->isAllowed('devel.rebuild_theme_warning', 1)) {
\Drupal::service('flood')->register('devel.rebuild_theme_warning');
if ($this->account->hasPermission('access devel information')) {
drupal_set_message(t('The theme information is being rebuilt on every request. Remember to <a href=":url">turn off</a> this feature on production websites.', array(':url' => $this->urlGenerator->generateFromRoute('devel.admin_settings'))));
}
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// Set a low value to start as early as possible.
$events[KernelEvents::REQUEST][] = ['onRequest', -100];
// Runs as soon as possible in the request but after
// AuthenticationSubscriber (priority 300) because you need to access to
// the current user for determine whether register the devel error handler
// or not.
$events[KernelEvents::REQUEST][] = ['registerErrorHandler', 256];
return $events;
}
}
<?php
namespace Drupal\devel\EventSubscriber;
use Drupal\Core\Session\AccountProxyInterface;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Listener for handling PHP errors.
*/
class ErrorHandlerSubscriber implements EventSubscriberInterface {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $account;
/**
* ErrorHandlerSubscriber constructor.
*
* @param \Drupal\Core\Session\AccountProxyInterface $account
* The current user.
*/
public function __construct(AccountProxyInterface $account) {
$this->account = $account;
}
/**
* Register devel error handler.
*
* @param \Symfony\Component\EventDispatcher\Event $event
* The event to process.
*/
public function registerErrorHandler(Event $event = NULL) {
if ($this->account && $this->account->hasPermission('access devel information')) {
devel_set_handler(devel_get_handlers());
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// Runs as soon as possible in the request but after
// AuthenticationSubscriber (priority 300) because you need to access to
// the current user for determine whether register the devel error handler
// or not.
$events[KernelEvents::REQUEST][] = ['registerErrorHandler', 256];
return $events;
}
}
<?php
namespace Drupal\devel\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Subscriber for force the system to rebuild the theme registry.
*/
class ThemeInfoRebuildSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* 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();
// 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->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()]);
drupal_set_message($message, 'warning', TRUE);
}
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// Set high priority value to start as early as possible.
$events[KernelEvents::REQUEST][] = ['rebuildThemeInfo', 256];
return $events;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment