You are here

class CartCacheContext in Commerce Core 8.2

Defines the CartCacheContext service, for "per cart" caching.

Cache context ID: 'cart'.

Hierarchy

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
cache_context.cart in modules/cart/commerce_cart.services.yml
Drupal\commerce_cart\Cache\Context\CartCacheContext

File

modules/cart/src/Cache/Context/CartCacheContext.php, line 15

Namespace

Drupal\commerce_cart\Cache\Context
View 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

Namesort descending Modifiers Type Description Overrides
CartCacheContext::$account protected property The current user.
CartCacheContext::$cartProvider protected property The cart provider service.
CartCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context. Overrides CacheContextInterface::getCacheableMetadata
CartCacheContext::getContext public function Returns the string representation of the cache context. Overrides CacheContextInterface::getContext
CartCacheContext::getLabel public static function Returns the label of the cache context. Overrides CacheContextInterface::getLabel
CartCacheContext::__construct public function Constructs a new CartCacheContext object.