You are here

class CurrentLocale in Price 8

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

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

The ChainLocaleResolver runs the registered locale resolvers one by one until one of them returns the locale. The DefaultLocaleResolver runs last, and contains the default logic which assembles the locale based on the current language and country.

Hierarchy

Expanded class hierarchy of CurrentLocale

See also

\Drupal\price\Resolver\ChainLocaleResolver

\Drupal\price\Resolver\DefaultLocaleResolver

1 string reference to 'CurrentLocale'
price.services.yml in ./price.services.yml
price.services.yml
1 service uses CurrentLocale
price.current_locale in ./price.services.yml
Drupal\price\CurrentLocale

File

src/CurrentLocale.php, line 19

Namespace

Drupal\price
View source
class CurrentLocale implements CurrentLocaleInterface {

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

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

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

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

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

}

Members

Namesort descending Modifiers Type Description Overrides
CurrentLocale::$chainResolver protected property The chain resolver.
CurrentLocale::$locales protected property Static cache of resolved locales. One per request.
CurrentLocale::$requestStack protected property The request stack.
CurrentLocale::getLocale public function Gets the locale for the current request. Overrides CurrentLocaleInterface::getLocale
CurrentLocale::__construct public function Constructs a new CurrentLocale object.