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