annotate core/lib/Drupal/Core/EventSubscriber/Fast404ExceptionHtmlSubscriber.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\EventSubscriber;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 6 use Drupal\Component\Utility\Html;
Chris@0 7 use Symfony\Component\HttpFoundation\Response;
Chris@0 8 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
Chris@0 9 use Symfony\Component\HttpKernel\HttpKernelInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * High-performance 404 exception subscriber.
Chris@0 13 *
Chris@0 14 * This subscriber will return a minimalist 404 response for HTML requests
Chris@0 15 * without running a full page theming operation.
Chris@0 16 */
Chris@0 17 class Fast404ExceptionHtmlSubscriber extends HttpExceptionSubscriberBase {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * The HTTP kernel.
Chris@0 21 *
Chris@0 22 * @var \Symfony\Component\HttpKernel\HttpKernelInterface
Chris@0 23 */
Chris@0 24 protected $httpKernel;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * The config factory.
Chris@0 28 *
Chris@0 29 * @var \Drupal\Core\Config\ConfigFactoryInterface
Chris@0 30 */
Chris@0 31 protected $configFactory;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Constructs a new Fast404ExceptionHtmlSubscriber.
Chris@0 35 *
Chris@0 36 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 37 * The configuration factory.
Chris@0 38 * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
Chris@0 39 * The HTTP Kernel service.
Chris@0 40 */
Chris@0 41 public function __construct(ConfigFactoryInterface $config_factory, HttpKernelInterface $http_kernel) {
Chris@0 42 $this->configFactory = $config_factory;
Chris@0 43 $this->httpKernel = $http_kernel;
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@0 47 * {@inheritdoc}
Chris@0 48 */
Chris@0 49 protected static function getPriority() {
Chris@0 50 // A very high priority so that it can take precedent over anything else,
Chris@0 51 // and thus be fast.
Chris@0 52 return 200;
Chris@0 53 }
Chris@0 54
Chris@0 55 /**
Chris@0 56 * {@inheritdoc}
Chris@0 57 */
Chris@0 58 protected function getHandledFormats() {
Chris@0 59 return ['html'];
Chris@0 60 }
Chris@0 61
Chris@0 62 /**
Chris@0 63 * Handles a 404 error for HTML.
Chris@0 64 *
Chris@0 65 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
Chris@0 66 * The event to process.
Chris@0 67 */
Chris@0 68 public function on404(GetResponseForExceptionEvent $event) {
Chris@0 69 $request = $event->getRequest();
Chris@0 70
Chris@0 71 $config = $this->configFactory->get('system.performance');
Chris@0 72 $exclude_paths = $config->get('fast_404.exclude_paths');
Chris@0 73 if ($config->get('fast_404.enabled') && $exclude_paths && !preg_match($exclude_paths, $request->getPathInfo())) {
Chris@0 74 $fast_paths = $config->get('fast_404.paths');
Chris@0 75 if ($fast_paths && preg_match($fast_paths, $request->getPathInfo())) {
Chris@0 76 $fast_404_html = strtr($config->get('fast_404.html'), ['@path' => Html::escape($request->getUri())]);
Chris@0 77 $response = new Response($fast_404_html, Response::HTTP_NOT_FOUND);
Chris@0 78 $event->setResponse($response);
Chris@0 79 }
Chris@0 80 }
Chris@0 81 }
Chris@0 82
Chris@0 83 }