CustomPageExceptionHtmlSubscriber.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Drupal\Core\EventSubscriber;
  3. use Drupal\Core\Access\AccessManagerInterface;
  4. use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
  5. use Drupal\Core\Config\ConfigFactoryInterface;
  6. use Drupal\Core\Routing\AccessAwareRouterInterface;
  7. use Drupal\Core\Routing\RedirectDestinationInterface;
  8. use Drupal\Core\Url;
  9. use Psr\Log\LoggerInterface;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  12. use Symfony\Component\HttpKernel\HttpKernelInterface;
  13. use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
  14. /**
  15. * Exception subscriber for handling core custom HTML error pages.
  16. */
  17. class CustomPageExceptionHtmlSubscriber extends DefaultExceptionHtmlSubscriber {
  18. /**
  19. * The configuration factory.
  20. *
  21. * @var \Drupal\Core\Config\ConfigFactoryInterface
  22. */
  23. protected $configFactory;
  24. /**
  25. * The access manager.
  26. *
  27. * @var \Drupal\Core\Access\AccessManagerInterface
  28. */
  29. protected $accessManager;
  30. /**
  31. * Constructs a new CustomPageExceptionHtmlSubscriber.
  32. *
  33. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  34. * The configuration factory.
  35. * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
  36. * The HTTP Kernel service.
  37. * @param \Psr\Log\LoggerInterface $logger
  38. * The logger service.
  39. * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
  40. * The redirect destination service.
  41. * @param \Symfony\Component\Routing\Matcher\UrlMatcherInterface $access_unaware_router
  42. * A router implementation which does not check access.
  43. * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
  44. * The access manager.
  45. */
  46. public function __construct(ConfigFactoryInterface $config_factory, HttpKernelInterface $http_kernel, LoggerInterface $logger, RedirectDestinationInterface $redirect_destination, UrlMatcherInterface $access_unaware_router, AccessManagerInterface $access_manager) {
  47. parent::__construct($http_kernel, $logger, $redirect_destination, $access_unaware_router);
  48. $this->configFactory = $config_factory;
  49. $this->accessManager = $access_manager;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected static function getPriority() {
  55. return -50;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function on403(GetResponseForExceptionEvent $event) {
  61. $custom_403_path = $this->configFactory->get('system.site')->get('page.403');
  62. if (!empty($custom_403_path)) {
  63. $this->makeSubrequestToCustomPath($event, $custom_403_path, Response::HTTP_FORBIDDEN);
  64. }
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function on404(GetResponseForExceptionEvent $event) {
  70. $custom_404_path = $this->configFactory->get('system.site')->get('page.404');
  71. if (!empty($custom_404_path)) {
  72. $this->makeSubrequestToCustomPath($event, $custom_404_path, Response::HTTP_NOT_FOUND);
  73. }
  74. }
  75. /**
  76. * Makes a subrequest to retrieve the custom error page.
  77. *
  78. * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
  79. * The event to process.
  80. * @param string $custom_path
  81. * The custom path to which to make a subrequest for this error message.
  82. * @param int $status_code
  83. * The status code for the error being handled.
  84. */
  85. protected function makeSubrequestToCustomPath(GetResponseForExceptionEvent $event, $custom_path, $status_code) {
  86. $url = Url::fromUserInput($custom_path);
  87. if ($url->isRouted()) {
  88. $access_result = $this->accessManager->checkNamedRoute($url->getRouteName(), $url->getRouteParameters(), NULL, TRUE);
  89. $request = $event->getRequest();
  90. // Merge the custom path's route's access result's cacheability metadata
  91. // with the existing one (from the master request), otherwise create it.
  92. if (!$request->attributes->has(AccessAwareRouterInterface::ACCESS_RESULT)) {
  93. $request->attributes->set(AccessAwareRouterInterface::ACCESS_RESULT, $access_result);
  94. }
  95. else {
  96. $existing_access_result = $request->attributes->get(AccessAwareRouterInterface::ACCESS_RESULT);
  97. if ($existing_access_result instanceof RefinableCacheableDependencyInterface) {
  98. $existing_access_result->addCacheableDependency($access_result);
  99. }
  100. }
  101. // Only perform the subrequest if the custom path is actually accessible.
  102. if (!$access_result->isAllowed()) {
  103. return;
  104. }
  105. }
  106. $this->makeSubrequest($event, $custom_path, $status_code);
  107. }
  108. }