You are here

class CurrentStore in Commerce Core 8.2

Same name in this branch
  1. 8.2 modules/store/src/CurrentStore.php \Drupal\commerce_store\CurrentStore
  2. 8.2 modules/store/src/Plugin/views/argument_default/CurrentStore.php \Drupal\commerce_store\Plugin\views\argument_default\CurrentStore

Holds a reference to the current store, resolved on demand.

The ChainStoreResolver runs the registered store resolvers one by one until one of them returns the store. The DefaultStoreResolver runs last, and will select the default store. Custom resolvers can choose based on the url, the user's country, etc.

Note that this functionality is optional, since not every site will be limited to having only one active store at the time.

Hierarchy

Expanded class hierarchy of CurrentStore

See also

\Drupal\commerce_store\Resolver\ChainStoreResolver

\Drupal\commerce_store\Resolver\DefaultStoreResolver

1 string reference to 'CurrentStore'
commerce_store.services.yml in modules/store/commerce_store.services.yml
modules/store/commerce_store.services.yml
1 service uses CurrentStore
commerce_store.current_store in modules/store/commerce_store.services.yml
Drupal\commerce_store\CurrentStore

File

modules/store/src/CurrentStore.php, line 22

Namespace

Drupal\commerce_store
View source
class CurrentStore implements CurrentStoreInterface {

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * The chain resolver.
   *
   * @var \Drupal\commerce_store\Resolver\ChainStoreResolverInterface
   */
  protected $chainResolver;

  /**
   * Static cache of resolved stores. One per request.
   *
   * @var \SplObjectStorage
   */
  protected $stores;

  /**
   * Constructs a new CurrentStore object.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\commerce_store\Resolver\ChainStoreResolverInterface $chain_resolver
   *   The chain resolver.
   */
  public function __construct(RequestStack $request_stack, ChainStoreResolverInterface $chain_resolver) {
    $this->requestStack = $request_stack;
    $this->chainResolver = $chain_resolver;
    $this->stores = new \SplObjectStorage();
  }

  /**
   * {@inheritdoc}
   */
  public function getStore() {
    $request = $this->requestStack
      ->getCurrentRequest();
    if (!$this->stores
      ->contains($request)) {
      $this->stores[$request] = $this->chainResolver
        ->resolve();
    }
    return $this->stores[$request];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrentStore::$chainResolver protected property The chain resolver.
CurrentStore::$requestStack protected property The request stack.
CurrentStore::$stores protected property Static cache of resolved stores. One per request.
CurrentStore::getStore public function Gets the active store for the current request. Overrides CurrentStoreInterface::getStore
CurrentStore::__construct public function Constructs a new CurrentStore object.