Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Making acm_exception D9 ready. #152

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion modules/acm_exception/acm_exception.services.yml
Original file line number Diff line number Diff line change
@@ -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']
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,31 @@
namespace Drupal\acm_exception\EventSubscriber;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\acm_exception\RouteExceptionHandler;
use Drupal\acm\RouteExceptionEvent;

/**
* Exception event subscriber for \Drupal\acm\RouteExceptionEvent.
*/
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.
*
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand All @@ -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));
}

Expand Down
15 changes: 13 additions & 2 deletions modules/acm_exception/src/RouteExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -26,17 +27,27 @@ class RouteExceptionHandler {
*/
private $logger;

/**
* The Messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
private $messenger;

/**
* Constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* 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;
}

/**
Expand All @@ -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);
}
}

Expand Down