Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Emulation.php
Go to the documentation of this file.
1 <?php
10 namespace Magento\Store\Model\App;
11 
13 
19 {
23  protected $_storeManager;
24 
28  protected $_translate;
29 
35  protected $_scopeConfig;
36 
40  protected $_localeResolver;
41 
45  protected $_design;
46 
50  protected $inlineConfig;
51 
55  protected $inlineTranslation;
56 
62  private $initialEnvironmentInfo;
63 
67  private $logger;
68 
82  public function __construct(
84  \Magento\Framework\View\DesignInterface $viewDesign,
85  \Magento\Framework\App\DesignInterface $design,
86  \Magento\Framework\TranslateInterface $translate,
87  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
89  \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
90  \Magento\Framework\Locale\ResolverInterface $localeResolver,
91  \Psr\Log\LoggerInterface $logger,
92  array $data = []
93  ) {
94  $this->_localeResolver = $localeResolver;
95  parent::__construct($data);
96  $this->_storeManager = $storeManager;
97  $this->_viewDesign = $viewDesign;
98  $this->_design = $design;
99  $this->_translate = $translate;
100  $this->_scopeConfig = $scopeConfig;
101  $this->inlineConfig = $inlineConfig;
102  $this->inlineTranslation = $inlineTranslation;
103  $this->logger = $logger;
104  }
105 
116  public function startEnvironmentEmulation(
117  $storeId,
118  $area = \Magento\Framework\App\Area::AREA_FRONTEND,
119  $force = false
120  ) {
121  // Only allow a single level of emulation
122  if ($this->initialEnvironmentInfo !== null) {
123  $this->logger->error(__('Environment emulation nesting is not allowed.'));
124  return;
125  }
126 
127  if ($storeId == $this->_storeManager->getStore()->getStoreId() && !$force) {
128  return;
129  }
131 
132  // emulate inline translations
133  $this->inlineTranslation->suspend($this->inlineConfig->isActive($storeId));
134 
135  // emulate design
136  $storeTheme = $this->_viewDesign->getConfigurationDesignTheme($area, ['store' => $storeId]);
137  $this->_viewDesign->setDesignTheme($storeTheme, $area);
138 
139  if ($area == \Magento\Framework\App\Area::AREA_FRONTEND) {
140  $designChange = $this->_design->loadChange($storeId);
141  if ($designChange->getData()) {
142  $this->_viewDesign->setDesignTheme($designChange->getDesign(), $area);
143  }
144  }
145 
146  // Current store needs to be changed right before locale change and after design change
147  $this->_storeManager->setCurrentStore($storeId);
148 
149  // emulate locale
150  $newLocaleCode = $this->_scopeConfig->getValue(
151  $this->_localeResolver->getDefaultLocalePath(),
153  $storeId
154  );
155  $this->_localeResolver->setLocale($newLocaleCode);
156  $this->_translate->setLocale($newLocaleCode);
157  $this->_translate->loadData($area);
158 
159  return;
160  }
161 
169  public function stopEnvironmentEmulation()
170  {
171  if ($this->initialEnvironmentInfo === null) {
172  return $this;
173  }
174 
175  $this->_restoreInitialInlineTranslation($this->initialEnvironmentInfo->getInitialTranslateInline());
176  $initialDesign = $this->initialEnvironmentInfo->getInitialDesign();
177  $this->_restoreInitialDesign($initialDesign);
178  // Current store needs to be changed right before locale change and after design change
179  $this->_storeManager->setCurrentStore($initialDesign['store']);
180  $this->_restoreInitialLocale($this->initialEnvironmentInfo->getInitialLocaleCode(), $initialDesign['area']);
181 
182  $this->initialEnvironmentInfo = null;
183  return $this;
184  }
185 
191  public function storeCurrentEnvironmentInfo()
192  {
193  $this->initialEnvironmentInfo = new \Magento\Framework\DataObject();
194  $this->initialEnvironmentInfo->setInitialTranslateInline(
195  $this->inlineTranslation->isEnabled()
196  )->setInitialDesign(
197  [
198  'area' => $this->_viewDesign->getArea(),
199  'theme' => $this->_viewDesign->getDesignTheme(),
200  'store' => $this->_storeManager->getStore()->getStoreId(),
201  ]
202  )->setInitialLocaleCode(
203  $this->_localeResolver->getLocale()
204  );
205  }
206 
213  protected function _restoreInitialInlineTranslation($initialTranslate)
214  {
215  $this->inlineTranslation->resume($initialTranslate);
216  return $this;
217  }
218 
225  protected function _restoreInitialDesign(array $initialDesign)
226  {
227  $this->_viewDesign->setDesignTheme($initialDesign['theme'], $initialDesign['area']);
228  return $this;
229  }
230 
238  protected function _restoreInitialLocale(
239  $initialLocaleCode,
240  $initialArea = \Magento\Framework\App\Area::AREA_ADMINHTML
241  ) {
242  $this->_localeResolver->setLocale($initialLocaleCode);
243  $this->_translate->setLocale($initialLocaleCode);
244  $this->_translate->loadData($initialArea);
245 
246  return $this;
247  }
248 }
_restoreInitialDesign(array $initialDesign)
Definition: Emulation.php:225
_restoreInitialLocale( $initialLocaleCode, $initialArea=\Magento\Framework\App\Area::AREA_ADMINHTML)
Definition: Emulation.php:238
$storeManager
__()
Definition: __.php:13
$logger
_restoreInitialInlineTranslation($initialTranslate)
Definition: Emulation.php:213
__construct(\Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\View\DesignInterface $viewDesign, \Magento\Framework\App\DesignInterface $design, \Magento\Framework\TranslateInterface $translate, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, ConfigInterface $inlineConfig, \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation, \Magento\Framework\Locale\ResolverInterface $localeResolver, \Psr\Log\LoggerInterface $logger, array $data=[])
Definition: Emulation.php:82
startEnvironmentEmulation( $storeId, $area=\Magento\Framework\App\Area::AREA_FRONTEND, $force=false)
Definition: Emulation.php:116