1<?php
2
3namespace Drupal\Core\Cache\Context;
4
5use Drupal\Core\Cache\CacheableMetadata;
6use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
7use Drupal\Core\Pager\PagerParametersInterface;
8
9/**
10 * Defines a cache context for "per page in a pager" caching.
11 *
12 * Cache context ID: 'url.query_args.pagers' (to vary by all pagers).
13 * Calculated cache context ID: 'url.query_args.pagers:%pager_id', e.g.
14 * 'url.query_args.pagers:1' (to vary by the pager with ID 1).
15 */
16class PagersCacheContext implements CalculatedCacheContextInterface {
17
18  use DeprecatedServicePropertyTrait;
19
20  /**
21   * {@inheritdoc}
22   */
23  protected $deprecatedProperties = ['requestStack' => 'request_stack'];
24
25  /**
26   * The pager parameters.
27   *
28   * @var \Drupal\Core\Pager\PagerParametersInterface
29   */
30  protected $pagerParams;
31
32  /**
33   * Constructs a new PagersCacheContext object.
34   *
35   * @param \Drupal\Core\Pager\PagerParametersInterface $pager_params
36   *   The pager parameters.
37   */
38  public function __construct($pager_params) {
39    if (!($pager_params instanceof PagerParametersInterface)) {
40      @trigger_error('Calling ' . __METHOD__ . ' with a $pager_params argument that does not implement \Drupal\Core\Pager\PagerParametersInterface is deprecated in drupal:8.8.0 and is required in drupal:9.0.0. See https://www.drupal.org/node/2779457', E_USER_DEPRECATED);
41      $pager_params = \Drupal::service('pager.parameters');
42    }
43    $this->pagerParams = $pager_params;
44  }
45
46  /**
47   * {@inheritdoc}
48   */
49  public static function getLabel() {
50    return t('Pager');
51  }
52
53  /**
54   * {@inheritdoc}
55   *
56   * @see \Drupal\Core\Pager\PagerParametersInterface::findPage()
57   */
58  public function getContext($pager_id = NULL) {
59    // The value of the 'page' query argument contains the information that
60    // controls *all* pagers.
61    if ($pager_id === NULL) {
62      return $this->pagerParams->getPagerParameter();
63    }
64
65    return $pager_id . '.' . $this->pagerParams->findPage($pager_id);
66  }
67
68  /**
69   * {@inheritdoc}
70   */
71  public function getCacheableMetadata($pager_id = NULL) {
72    return new CacheableMetadata();
73  }
74
75}
76