Skip to content
Snippets Groups Projects

Implementation using a subscriber

Open Björn Brala requested to merge issue/drupal-2430335:2430335-vary-new-branch into 11.x
6 unresolved threads

Closes #2430335

Merge request reports

Members who can merge are allowed to add commits.
Code Quality is loading
Test summary results are being parsed
Ready to merge by members who can write to the target branch.
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
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\EventDispatcher\EventSubscriberInterface;
8 use Symfony\Component\HttpKernel\Event\ResponseEvent;
9 use Symfony\Component\HttpKernel\KernelEvents;
10
11 class VaryHeaderResponseSubscriber implements EventSubscriberInterface {
12
13 protected array $vary = [];
14
15 public function addVaryHeader(string $header): void {
  • 6 use Drupal\Core\Cache\CacheableResponseInterface;
    7 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    8 use Symfony\Component\HttpKernel\Event\ResponseEvent;
    9 use Symfony\Component\HttpKernel\KernelEvents;
    10
    11 class VaryHeaderResponseSubscriber implements EventSubscriberInterface {
    12
    13 protected array $vary = [];
    14
    15 public function addVaryHeader(string $header): void {
    16 if (!in_array($header, $this->vary)) {
    17 $this->vary[] = $header;
    18 }
    19 }
    20
    21 public function onRespond(ResponseEvent $event) {
  • 1 <?php
    2
    3 namespace Drupal\Core\EventSubscriber;
  • 26 if ($this->vary) {
    27 $response = $event->getResponse();
    28 $response->setVary($this->vary, FALSE);
    29 if ($response instanceof CacheableResponseInterface) {
    30 $metadata = new CacheableMetadata();
    31 $contexts = [];
    32 foreach ($this->vary as $vary_header) {
    33 $contexts[] = 'headers:' . $vary_header;
    34 }
    35 $metadata->addCacheContexts($contexts);
    36 $response->addCacheableDependency($metadata);
    37 }
    38 }
    39 }
    40
    41 public static function getSubscribedEvents(): array {
  • 342 369 *
    343 370 * @param \Symfony\Component\HttpFoundation\Request $request
    344 371 * A request object.
    372 * @param array $vary_headers
    373 * Optional array of vary headers in response object.
    345 374 *
    346 375 * @return string
    347 376 * The cache ID for this request.
    348 377 */
    349 protected function getCacheId(Request $request) {
    378 protected function getCacheId(Request $request, array $vary_headers = []) {
  • 7 use Symfony\Component\DependencyInjection\ContainerInterface;
    8 use Symfony\Component\HttpFoundation\Request;
    9
    10 /**
    11 * Page controller that is aware of Vary header.
    12 *
    13 * @internal
    14 */
    15 class TestVaryController extends ControllerBase {
    16
    17 /**
    18 * The vary policy.
    19 *
    20 * @var \Drupal\Core\EventSubscriber\VaryHeaderResponseSubscriber
    21 */
    22 protected $varyResponseSubscriber;
  • Björn Brala added 395 commits

    added 395 commits

    Compare with previous version

  • Please register or sign in to reply
    Loading