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

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
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\Cache\CacheableResponseInterface;
Chris@0 6 use Drupal\Core\Routing\AccessAwareRouterInterface;
Chris@0 7 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
Chris@0 8 use Symfony\Component\HttpKernel\KernelEvents;
Chris@0 9 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Response subscriber to bubble the route's access result's cacheability.
Chris@0 13 *
Chris@0 14 * During routing, access checking is performed. The corresponding access result
Chris@0 15 * is stored in the Request object's attributes, just like the matching route
Chris@0 16 * object is. In case of a cacheable response, the route's access result also
Chris@0 17 * determined the content of the response, and therefore the cacheability of the
Chris@0 18 * route's access result should also be applied to the resulting response.
Chris@0 19 *
Chris@0 20 * @see \Drupal\Core\Routing\AccessAwareRouterInterface::ACCESS_RESULT
Chris@0 21 * @see \Drupal\Core\Routing\AccessAwareRouter::matchRequest()
Chris@0 22 * @see \Drupal\Core\Routing\AccessAwareRouter::checkAccess()
Chris@0 23 */
Chris@0 24 class RouteAccessResponseSubscriber implements EventSubscriberInterface {
Chris@0 25
Chris@0 26 /**
Chris@0 27 * Bubbles the route's access result' cacheability metadata.
Chris@0 28 *
Chris@0 29 * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
Chris@0 30 * The event to process.
Chris@0 31 */
Chris@0 32 public function onRespond(FilterResponseEvent $event) {
Chris@0 33 if (!$event->isMasterRequest()) {
Chris@0 34 return;
Chris@0 35 }
Chris@0 36
Chris@0 37 $response = $event->getResponse();
Chris@0 38 if (!$response instanceof CacheableResponseInterface) {
Chris@0 39 return;
Chris@0 40 }
Chris@0 41
Chris@0 42 $request = $event->getRequest();
Chris@0 43 $access_result = $request->attributes->get(AccessAwareRouterInterface::ACCESS_RESULT);
Chris@0 44 $response->addCacheableDependency($access_result);
Chris@0 45 }
Chris@0 46
Chris@0 47 /**
Chris@0 48 * {@inheritdoc}
Chris@0 49 */
Chris@0 50 public static function getSubscribedEvents() {
Chris@0 51 // Priority 10, so that it runs before FinishResponseSubscriber, which will
Chris@0 52 // expose the cacheability metadata in the form of headers.
Chris@0 53 $events[KernelEvents::RESPONSE][] = ['onRespond', 10];
Chris@0 54 return $events;
Chris@0 55 }
Chris@0 56
Chris@0 57 }