You are here

class CurrentCountry in Price 3.x

Same name and namespace in other branches
  1. 8 src/CurrentCountry.php \Drupal\price\CurrentCountry
  2. 2.0.x src/CurrentCountry.php \Drupal\price\CurrentCountry
  3. 2.x src/CurrentCountry.php \Drupal\price\CurrentCountry
  4. 3.0.x src/CurrentCountry.php \Drupal\price\CurrentCountry

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

The ChainCountryResolver runs the registered country resolvers one by one until one of them returns the country. The DefaultCountryResolver runs last, and will select the site's default country. Custom resolvers can choose based on the user profile, GeoIP, etc.

Hierarchy

Expanded class hierarchy of CurrentCountry

See also

\Drupal\price\Resolver\ChainCountryResolver

\Drupal\price\Resolver\DefaultCountryResolver

1 file declares its use of CurrentCountry
CountryCacheContext.php in src/Cache/Context/CountryCacheContext.php
1 string reference to 'CurrentCountry'
price.services.yml in ./price.services.yml
price.services.yml
1 service uses CurrentCountry
price.current_country in ./price.services.yml
Drupal\price\CurrentCountry

File

src/CurrentCountry.php, line 19

Namespace

Drupal\price
View source
class CurrentCountry implements CurrentCountryInterface {

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

  /**
   * The chain resolver.
   *
   * @var \Drupal\price\Resolver\ChainCountryResolverInterface
   */
  protected $chainResolver;

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

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

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

}

Members

Namesort descending Modifiers Type Description Overrides
CurrentCountry::$chainResolver protected property The chain resolver.
CurrentCountry::$countries protected property Static cache of resolved countries. One per request.
CurrentCountry::$requestStack protected property The request stack.
CurrentCountry::getCountry public function Gets the country for the current request. Overrides CurrentCountryInterface::getCountry
CurrentCountry::__construct public function Constructs a new CurrentCountry object.