CustomPageExceptionHtmlSubscriber.php 2.48 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber.
6 7 8 9 10 11
 */

namespace Drupal\Core\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\AliasManagerInterface;
12
use Drupal\Core\Routing\RedirectDestinationInterface;
13 14 15 16
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
17
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
18 19

/**
20
 * Exception subscriber for handling core custom HTML error pages.
21
 */
22
class CustomPageExceptionHtmlSubscriber extends DefaultExceptionHtmlSubscriber {
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a new CustomPageExceptionHtmlSubscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
   *   The HTTP Kernel service.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger service.
40 41
   * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
   *   The redirect destination service.
42 43
   * @param \Symfony\Component\Routing\Matcher\UrlMatcherInterface $access_unaware_router
   *   A router implementation which does not check access.
44
   */
45 46
  public function __construct(ConfigFactoryInterface $config_factory, HttpKernelInterface $http_kernel, LoggerInterface $logger, RedirectDestinationInterface $redirect_destination, UrlMatcherInterface $access_unaware_router) {
    parent::__construct($http_kernel, $logger, $redirect_destination, $access_unaware_router);
47 48 49 50 51 52 53 54 55 56 57
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  protected static function getPriority() {
    return -50;
  }

  /**
58
   * {@inheritdoc}
59 60
   */
  public function on403(GetResponseForExceptionEvent $event) {
61 62 63 64
    $custom_403_path = $this->configFactory->get('system.site')->get('page.403');
    if (!empty($custom_403_path)) {
      $this->makeSubrequest($event, $custom_403_path, Response::HTTP_FORBIDDEN);
    }
65 66 67
  }

  /**
68
   * {@inheritdoc}
69 70
   */
  public function on404(GetResponseForExceptionEvent $event) {
71 72 73 74
    $custom_404_path = $this->configFactory->get('system.site')->get('page.404');
    if (!empty($custom_404_path)) {
      $this->makeSubrequest($event, $custom_404_path, Response::HTTP_NOT_FOUND);
    }
75 76 77
  }

}