class CartCacheContext in Commerce Core 8.2
Defines the CartCacheContext service, for "per cart" caching.
Cache context ID: 'cart'.
Hierarchy
- class \Drupal\commerce_cart\Cache\Context\CartCacheContext implements CacheContextInterface
Expanded class hierarchy of CartCacheContext
1 file declares its use of CartCacheContext
- CartCacheContextTest.php in modules/
cart/ tests/ src/ Unit/ CartCacheContextTest.php
1 string reference to 'CartCacheContext'
- commerce_cart.services.yml in modules/
cart/ commerce_cart.services.yml - modules/cart/commerce_cart.services.yml
1 service uses CartCacheContext
File
- modules/
cart/ src/ Cache/ Context/ CartCacheContext.php, line 15
Namespace
Drupal\commerce_cart\Cache\ContextView source
class CartCacheContext implements CacheContextInterface {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* The cart provider service.
*
* @var \Drupal\commerce_cart\CartProviderInterface
*/
protected $cartProvider;
/**
* Constructs a new CartCacheContext object.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The current user account.
* @param \Drupal\commerce_cart\CartProviderInterface $cart_provider
* The cart provider service.
*/
public function __construct(AccountInterface $account, CartProviderInterface $cart_provider) {
$this->account = $account;
$this->cartProvider = $cart_provider;
}
/**
* {@inheritdoc}
*/
public static function getLabel() {
return t('Current cart IDs');
}
/**
* {@inheritdoc}
*/
public function getContext() {
return implode(':', $this->cartProvider
->getCartIds($this->account));
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata() {
$metadata = new CacheableMetadata();
foreach ($this->cartProvider
->getCarts($this->account) as $cart) {
$metadata
->addCacheableDependency($cart);
}
return $metadata;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CartCacheContext:: |
protected | property | The current user. | |
CartCacheContext:: |
protected | property | The cart provider service. | |
CartCacheContext:: |
public | function |
Gets the cacheability metadata for the context. Overrides CacheContextInterface:: |
|
CartCacheContext:: |
public | function |
Returns the string representation of the cache context. Overrides CacheContextInterface:: |
|
CartCacheContext:: |
public static | function |
Returns the label of the cache context. Overrides CacheContextInterface:: |
|
CartCacheContext:: |
public | function | Constructs a new CartCacheContext object. |