diff --git a/modules/acm_exception/acm_exception.services.yml b/modules/acm_exception/acm_exception.services.yml index 2de98a6..b1c4f44 100644 --- a/modules/acm_exception/acm_exception.services.yml +++ b/modules/acm_exception/acm_exception.services.yml @@ -1,12 +1,14 @@ services: acm_exception.route_exception_subscriber: class: Drupal\acm_exception\EventSubscriber\RouteExceptionSubscriber + arguments: ['@acm_exception.route_exception_handler'] tags: - { name: 'event_subscriber' } acm_exception.route_exception_event_subscriber: class: Drupal\acm_exception\EventSubscriber\RouteExceptionEventSubscriber + arguments: ['@acm_exception.route_exception_handler'] tags: - { name: 'event_subscriber' } acm_exception.route_exception_handler: class: Drupal\acm_exception\RouteExceptionHandler - arguments: ['@config.factory', '@logger.factory'] + arguments: ['@config.factory', '@logger.factory', '@messenger'] diff --git a/modules/acm_exception/src/EventSubscriber/RouteExceptionEventSubscriber.php b/modules/acm_exception/src/EventSubscriber/RouteExceptionEventSubscriber.php index 75c17c1..5113a53 100644 --- a/modules/acm_exception/src/EventSubscriber/RouteExceptionEventSubscriber.php +++ b/modules/acm_exception/src/EventSubscriber/RouteExceptionEventSubscriber.php @@ -3,6 +3,7 @@ namespace Drupal\acm_exception\EventSubscriber; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Drupal\acm_exception\RouteExceptionHandler; use Drupal\acm\RouteExceptionEvent; /** @@ -10,6 +11,23 @@ */ class RouteExceptionEventSubscriber implements EventSubscriberInterface { + /** + * Route exception handler. + * + * @var \Drupal\acm_exception\RouteExceptionHandler + */ + protected $routeExceptionHandler; + + /** + * RouteExceptionEventSubscriber constructor. + * + * @param \Drupal\acm_exception\RouteExceptionHandler $route_exception_handler + * Route exception handler. + */ + public function __construct(RouteExceptionHandler $route_exception_handler) { + $this->routeExceptionHandler = $route_exception_handler; + } + /** * User messaging for RouteExceptions. * @@ -18,9 +36,8 @@ class RouteExceptionEventSubscriber implements EventSubscriberInterface { */ public function onException(RouteExceptionEvent $event) { $exception = $event->getException(); - $handler = \Drupal::service('acm_exception.route_exception_handler'); - $handler->message($exception); - $handler->log($exception); + $this->routeExceptionHandler->message($exception); + $this->routeExceptionHandler->log($exception); } /** diff --git a/modules/acm_exception/src/EventSubscriber/RouteExceptionSubscriber.php b/modules/acm_exception/src/EventSubscriber/RouteExceptionSubscriber.php index 2d3592c..dfb8a34 100644 --- a/modules/acm_exception/src/EventSubscriber/RouteExceptionSubscriber.php +++ b/modules/acm_exception/src/EventSubscriber/RouteExceptionSubscriber.php @@ -2,6 +2,7 @@ namespace Drupal\acm_exception\EventSubscriber; +use Drupal\acm_exception\RouteExceptionHandler; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; use Symfony\Component\HttpKernel\KernelEvents; @@ -13,6 +14,23 @@ */ class RouteExceptionSubscriber implements EventSubscriberInterface { + /** + * Route exception handler. + * + * @var \Drupal\acm_exception\RouteExceptionHandler + */ + protected $routeExceptionHandler; + + /** + * RouteExceptionSubscriber constructor. + * + * @param \Drupal\acm_exception\RouteExceptionHandler $route_exception_handler + * Route exception handler. + */ + public function __construct(RouteExceptionHandler $route_exception_handler) { + $this->routeExceptionHandler = $route_exception_handler; + } + /** * Catch all uncaught RouteException exceptions. * @@ -25,8 +43,7 @@ public function onException(GetResponseForExceptionEvent $event) { return; } - $handler = \Drupal::service('acm_exception.route_exception_handler'); - $redirect = $handler->getRedirect($exception); + $redirect = $this->routeExceptionHandler->getRedirect($exception); $event->setResponse(RedirectResponse::create($redirect, 302)); } diff --git a/modules/acm_exception/src/RouteExceptionHandler.php b/modules/acm_exception/src/RouteExceptionHandler.php index 2170912..6d27537 100644 --- a/modules/acm_exception/src/RouteExceptionHandler.php +++ b/modules/acm_exception/src/RouteExceptionHandler.php @@ -5,6 +5,7 @@ use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Logger\LoggerChannelFactory; use Drupal\Core\Url; +use Drupal\Core\Messenger\MessengerInterface; use Drupal\acm\Connector\RouteException; /** @@ -26,6 +27,13 @@ class RouteExceptionHandler { */ private $logger; + /** + * The Messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + private $messenger; + /** * Constructor. * @@ -33,10 +41,13 @@ class RouteExceptionHandler { * ConfigFactoryInterface object. * @param \Drupal\Core\Logger\LoggerChannelFactory $logger_factory * LoggerChannelFactory object. + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + * The Messenger service. */ - public function __construct(ConfigFactoryInterface $config_factory, LoggerChannelFactory $logger_factory) { + public function __construct(ConfigFactoryInterface $config_factory, LoggerChannelFactory $logger_factory, MessengerInterface $messenger) { $this->config = $config_factory->get('acm_exception.settings'); $this->logger = $logger_factory->get('acm_exception'); + $this->messenger = $messenger; } /** @@ -48,7 +59,7 @@ public function __construct(ConfigFactoryInterface $config_factory, LoggerChanne public function message(RouteException $e) { $message = $this->getConfig('message', $e); if (!empty($message) && PHP_SAPI !== 'cli') { - drupal_set_message($message, 'error'); + $this->messenger->addError($message); } }