Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / EventSubscriber / AnonymousUserResponseSubscriber.php
1 <?php
2
3 namespace Drupal\Core\EventSubscriber;
4
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Cache\CacheableResponseInterface;
7 use Drupal\Core\Session\AccountInterface;
8 use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
9 use Symfony\Component\HttpKernel\KernelEvents;
10 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
11
12 /**
13  * Response subscriber to handle finished responses for the anonymous user.
14  */
15 class AnonymousUserResponseSubscriber implements EventSubscriberInterface {
16
17   /**
18    * The current user.
19    *
20    * @var \Drupal\Core\Session\AccountInterface
21    */
22   protected $currentUser;
23
24   /**
25    * Constructs an AnonymousUserResponseSubscriber object.
26    *
27    * @param \Drupal\Core\Session\AccountInterface $current_user
28    *   The current user.
29    */
30   public function __construct(AccountInterface $current_user) {
31     $this->currentUser = $current_user;
32   }
33
34   /**
35    * Adds a cache tag if the 'user.permissions' cache context is present.
36    *
37    * @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
38    *   The event to process.
39    */
40   public function onRespond(FilterResponseEvent $event) {
41     if (!$event->isMasterRequest()) {
42       return;
43     }
44
45     if (!$this->currentUser->isAnonymous()) {
46       return;
47     }
48
49     $response = $event->getResponse();
50     if (!$response instanceof CacheableResponseInterface) {
51       return;
52     }
53
54     // The 'user.permissions' cache context ensures that if the permissions for
55     // a role are modified, users are not served stale render cache content.
56     // But, when entire responses are cached in reverse proxies, the value for
57     // the cache context is never calculated, causing the stale response to not
58     // be invalidated. Therefore, when varying by permissions and the current
59     // user is the anonymous user, also add the cache tag for the 'anonymous'
60     // role.
61     if (in_array('user.permissions', $response->getCacheableMetadata()->getCacheContexts())) {
62       $per_permissions_response_for_anon = new CacheableMetadata();
63       $per_permissions_response_for_anon->setCacheTags(['config:user.role.anonymous']);
64       $response->addCacheableDependency($per_permissions_response_for_anon);
65     }
66   }
67
68   /**
69    * Registers the methods in this class that should be listeners.
70    *
71    * @return array
72    *   An array of event listener definitions.
73    */
74   public static function getSubscribedEvents() {
75     // Priority 5, so that it runs before FinishResponseSubscriber, but after
76     // event subscribers that add the associated cacheability metadata (which
77     // have priority 10). This one is conditional, so must run after those.
78     $events[KernelEvents::RESPONSE][] = ['onRespond', 5];
79     return $events;
80   }
81
82 }