Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Resolver.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class Resolver implements ResolverInterface
11 {
15  const DEFAULT_LOCALE = 'en_US';
16 
22  protected $defaultLocale;
23 
29  protected $scopeType;
30 
36  protected $locale;
37 
41  protected $scopeConfig;
42 
48  protected $emulatedLocales = [];
49 
53  private $defaultLocalePath;
54 
61  public function __construct(
63  $defaultLocalePath,
64  $scopeType,
65  $locale = null
66  ) {
67  $this->scopeConfig = $scopeConfig;
68  $this->defaultLocalePath = $defaultLocalePath;
69  $this->scopeType = $scopeType;
70  $this->setLocale($locale);
71  }
72 
76  public function getDefaultLocalePath()
77  {
78  return $this->defaultLocalePath;
79  }
80 
84  public function setDefaultLocale($locale)
85  {
86  $this->defaultLocale = $locale;
87  return $this;
88  }
89 
93  public function getDefaultLocale()
94  {
95  if (!$this->defaultLocale) {
96  $locale = $this->scopeConfig->getValue($this->getDefaultLocalePath(), $this->scopeType);
97  if (!$locale) {
99  }
100  $this->defaultLocale = $locale;
101  }
102  return $this->defaultLocale;
103  }
104 
108  public function setLocale($locale = null)
109  {
110  if ($locale !== null && is_string($locale)) {
111  $this->locale = $locale;
112  } else {
113  $this->locale = $this->getDefaultLocale();
114  }
115  return $this;
116  }
117 
121  public function getLocale()
122  {
123  if ($this->locale === null) {
124  $this->setLocale();
125  }
126  return $this->locale;
127  }
128 
132  public function emulate($scopeId)
133  {
134  $result = null;
135  if ($scopeId) {
136  $this->emulatedLocales[] = $this->getLocale();
137  $this->locale = $this->scopeConfig->getValue(
138  $this->getDefaultLocalePath(),
139  $this->scopeType,
140  $scopeId
141  );
143  } else {
144  $this->emulatedLocales[] = false;
145  }
146  return $result;
147  }
148 
152  public function revert()
153  {
154  $result = null;
155  $localeCode = array_pop($this->emulatedLocales);
156  if ($localeCode) {
157  $this->locale = $localeCode;
159  }
160  return $result;
161  }
162 }
__construct(ScopeConfigInterface $scopeConfig, $defaultLocalePath, $scopeType, $locale=null)
Definition: Resolver.php:61