CustomPageExceptionHtmlSubscriber.php 2.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php

/**
 * @file
 * Contains \Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber.
 */

namespace Drupal\Core\EventSubscriber;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
18
 * Exception subscriber for handling core custom HTML error pages.
19
 */
20
class CustomPageExceptionHtmlSubscriber extends DefaultExceptionHtmlSubscriber {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

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

  /**
   * The page alias manager.
   *
   * @var \Drupal\Core\Path\AliasManagerInterface
   */
  protected $aliasManager;

  /**
   * Constructs a new CustomPageExceptionHtmlSubscriber.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
   *   The alias manager service.
   * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
   *   The HTTP Kernel service.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, AliasManagerInterface $alias_manager, HttpKernelInterface $http_kernel, LoggerInterface $logger) {
49
    parent::__construct($http_kernel, $logger);
50 51 52 53 54 55 56 57 58 59 60 61
    $this->configFactory = $config_factory;
    $this->aliasManager = $alias_manager;
  }

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

  /**
62
   * {@inheritdoc}
63 64 65 66 67 68 69
   */
  public function on403(GetResponseForExceptionEvent $event) {
    $path = $this->aliasManager->getPathByAlias($this->configFactory->get('system.site')->get('page.403'));
    $this->makeSubrequest($event, $path, Response::HTTP_FORBIDDEN);
  }

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

}