Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / EventSubscriber / ClientErrorResponseSubscriber.php
1 <?php
2
3 namespace Drupal\Core\EventSubscriber;
4
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Cache\CacheableResponseInterface;
7 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
8 use Symfony\Component\HttpKernel\KernelEvents;
9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10
11 /**
12  * Response subscriber to set the '4xx-response' cache tag on 4xx responses.
13  */
14 class ClientErrorResponseSubscriber implements EventSubscriberInterface {
15
16   /**
17    * Sets the '4xx-response' cache tag on 4xx responses.
18    *
19    * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
20    *   The event to process.
21    */
22   public function onRespond(FilterResponseEvent $event) {
23     if (!$event->isMasterRequest()) {
24       return;
25     }
26
27     $response = $event->getResponse();
28     if (!$response instanceof CacheableResponseInterface) {
29       return;
30     }
31
32     if ($response->isClientError()) {
33       $http_4xx_response_cacheability = new CacheableMetadata();
34       $http_4xx_response_cacheability->setCacheTags(['4xx-response']);
35       $response->addCacheableDependency($http_4xx_response_cacheability);
36     }
37   }
38
39   /**
40    * {@inheritdoc}
41    */
42   public static function getSubscribedEvents() {
43     // Priority 10, so that it runs before FinishResponseSubscriber, which will
44     // expose the cacheability metadata in the form of headers.
45     $events[KernelEvents::RESPONSE][] = ['onRespond', 10];
46     return $events;
47   }
48
49 }