Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/EventSubscriber/HttpExceptionSubscriberBase.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Core\EventSubscriber; | |
4 | |
5 use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
6 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; | |
7 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; | |
8 use Symfony\Component\HttpKernel\KernelEvents; | |
9 | |
10 /** | |
11 * Utility base class for exception subscribers. | |
12 * | |
13 * A subscriber may extend this class and implement getHandledFormats() to | |
14 * indicate which request formats it will respond to. Then implement an on*() | |
15 * method for any error code (HTTP response code) that should be handled. For | |
16 * example, to handle a specific error code like 404 Not Found messages add the | |
17 * method: | |
18 * | |
19 * @code | |
20 * public function on404(GetResponseForExceptionEvent $event) {} | |
21 * @endcode | |
22 * | |
23 * To implement a fallback for the entire 4xx class of codes, implement the | |
24 * method: | |
25 * | |
26 * @code | |
27 * public function on4xx(GetResponseForExceptionEvent $event) {} | |
28 * @endcode | |
29 * | |
30 * That method should then call $event->setResponse() to set the response object | |
31 * for the exception. Alternatively, it may opt not to do so and then other | |
32 * listeners will have the opportunity to handle the exception. | |
33 * | |
34 * Note: Core provides several important exception listeners by default. In most | |
35 * cases, setting the priority of a contrib listener to the default of 0 will | |
36 * do what you expect and handle the exceptions you'd expect it to handle. | |
37 * If a custom priority is set, be aware of the following core-registered | |
38 * listeners. | |
39 * | |
40 * - Fast404ExceptionHtmlSubscriber: 200. This subscriber will return a | |
41 * minimalist, high-performance 404 page for HTML requests. It is not | |
42 * recommended to have a priority higher than this one as it will only slow | |
43 * down that use case. | |
44 * - ExceptionLoggingSubscriber: 50. This subscriber logs all exceptions but | |
45 * does not handle them. Do not register a listener with a higher priority | |
46 * unless you want exceptions to not get logged, which makes debugging more | |
47 * difficult. | |
48 * - DefaultExceptionSubscriber: -256. The subscriber of last resort, this will | |
49 * provide generic handling for any exception. A listener with a lower | |
50 * priority will never get called. | |
51 * | |
52 * All other core-provided exception handlers have negative priorities so most | |
53 * module-provided listeners will naturally take precedence over them. | |
54 */ | |
55 abstract class HttpExceptionSubscriberBase implements EventSubscriberInterface { | |
56 | |
57 /** | |
58 * Specifies the request formats this subscriber will respond to. | |
59 * | |
60 * @return array | |
61 * An indexed array of the format machine names that this subscriber will | |
62 * attempt to process, such as "html" or "json". Returning an empty array | |
63 * will apply to all formats. | |
64 * | |
65 * @see \Symfony\Component\HttpFoundation\Request | |
66 */ | |
67 abstract protected function getHandledFormats(); | |
68 | |
69 /** | |
70 * Specifies the priority of all listeners in this class. | |
71 * | |
72 * The default priority is 1, which is very low. To have listeners that have | |
73 * a "first attempt" at handling exceptions return a higher priority. | |
74 * | |
75 * @return int | |
76 * The event priority of this subscriber. | |
77 */ | |
78 protected static function getPriority() { | |
79 return 0; | |
80 } | |
81 | |
82 /** | |
83 * Handles errors for this subscriber. | |
84 * | |
85 * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event | |
86 * The event to process. | |
87 */ | |
88 public function onException(GetResponseForExceptionEvent $event) { | |
89 $exception = $event->getException(); | |
90 | |
91 // Make the exception available for example when rendering a block. | |
92 $request = $event->getRequest(); | |
93 $request->attributes->set('exception', $exception); | |
94 | |
95 $handled_formats = $this->getHandledFormats(); | |
96 | |
97 $format = $request->query->get(MainContentViewSubscriber::WRAPPER_FORMAT, $request->getRequestFormat()); | |
98 | |
99 if ($exception instanceof HttpExceptionInterface && (empty($handled_formats) || in_array($format, $handled_formats))) { | |
100 $method = 'on' . $exception->getStatusCode(); | |
101 // Keep just the leading number of the status code to produce either a | |
102 // on400 or a 500 method callback. | |
103 $method_fallback = 'on' . substr($exception->getStatusCode(), 0, 1) . 'xx'; | |
104 // We want to allow the method to be called and still not set a response | |
105 // if it has additional filtering logic to determine when it will apply. | |
106 // It is therefore the method's responsibility to set the response on the | |
107 // event if appropriate. | |
108 if (method_exists($this, $method)) { | |
109 $this->$method($event); | |
110 } | |
111 elseif (method_exists($this, $method_fallback)) { | |
112 $this->$method_fallback($event); | |
113 } | |
114 } | |
115 } | |
116 | |
117 /** | |
118 * Registers the methods in this class that should be listeners. | |
119 * | |
120 * @return array | |
121 * An array of event listener definitions. | |
122 */ | |
123 public static function getSubscribedEvents() { | |
124 $events[KernelEvents::EXCEPTION][] = ['onException', static::getPriority()]; | |
125 return $events; | |
126 } | |
127 | |
128 } |