You are here

class Ip2CountryCacheContext in IP-based Determination of a Visitor's Country 8

Defines the Ip2CountryCacheContext service, for "per country" caching.

Cache context ID: 'ip.country'.

Hierarchy

Expanded class hierarchy of Ip2CountryCacheContext

1 string reference to 'Ip2CountryCacheContext'
ip2country.services.yml in ./ip2country.services.yml
ip2country.services.yml
1 service uses Ip2CountryCacheContext
cache_context.ip.country in ./ip2country.services.yml
Drupal\ip2country\Cache\Context\Ip2CountryCacheContext

File

src/Cache/Context/Ip2CountryCacheContext.php, line 16

Namespace

Drupal\ip2country\Cache\Context
View source
class Ip2CountryCacheContext extends RequestStackCacheContextBase implements CacheContextInterface {

  /**
   * The ip2country.lookup service.
   *
   * @var \Drupal\ip2country\Ip2CountryLookupInterface
   */
  protected $ip2countryLookup;

  /**
   * Constructs an Ip2CountryCacheContext.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\ip2country\Ip2CountryLookupInterface $ip2countryLookup
   *   The ip2country.lookup service.
   */
  public function __construct(RequestStack $request_stack, Ip2CountryLookupInterface $ip2countryLookup) {
    parent::__construct($request_stack);
    $this->ip2countryLookup = $ip2countryLookup;
  }

  /**
   * {@inheritdoc}
   */
  public static function getLabel() {
    return t('Country based on IP address');
  }

  /**
   * {@inheritdoc}
   */
  public function getContext() {
    return $this->ip2countryLookup
      ->getCountry($this->requestStack
      ->getCurrentRequest()
      ->getClientIp());
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata() {
    return new CacheableMetadata();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Ip2CountryCacheContext::$ip2countryLookup protected property The ip2country.lookup service.
Ip2CountryCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context. Overrides CacheContextInterface::getCacheableMetadata
Ip2CountryCacheContext::getContext public function Returns the string representation of the cache context. Overrides CacheContextInterface::getContext
Ip2CountryCacheContext::getLabel public static function Returns the label of the cache context. Overrides CacheContextInterface::getLabel
Ip2CountryCacheContext::__construct public function Constructs an Ip2CountryCacheContext. Overrides RequestStackCacheContextBase::__construct
RequestStackCacheContextBase::$requestStack protected property The request stack.