Emulation.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Emulation model
  8. */
  9. namespace Magento\Store\Model\App;
  10. use Magento\Framework\Translate\Inline\ConfigInterface;
  11. /**
  12. * @api
  13. * @since 100.0.2
  14. */
  15. class Emulation extends \Magento\Framework\DataObject
  16. {
  17. /**
  18. * @var \Magento\Store\Model\StoreManagerInterface
  19. */
  20. protected $_storeManager;
  21. /**
  22. * @var \Magento\Framework\TranslateInterface
  23. */
  24. protected $_translate;
  25. /**
  26. * Core store config
  27. *
  28. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  29. */
  30. protected $_scopeConfig;
  31. /**
  32. * @var \Magento\Framework\Locale\ResolverInterface
  33. */
  34. protected $_localeResolver;
  35. /**
  36. * @var \Magento\Framework\App\DesignInterface
  37. */
  38. protected $_design;
  39. /**
  40. * @var ConfigInterface
  41. */
  42. protected $inlineConfig;
  43. /**
  44. * @var \Magento\Framework\Translate\Inline\StateInterface
  45. */
  46. protected $inlineTranslation;
  47. /**
  48. * Ini
  49. *
  50. * @var \Magento\Framework\DataObject
  51. */
  52. private $initialEnvironmentInfo;
  53. /**
  54. * @var \Psr\Log\LoggerInterface
  55. */
  56. private $logger;
  57. /**
  58. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  59. * @param \Magento\Framework\View\DesignInterface $viewDesign
  60. * @param \Magento\Framework\App\DesignInterface $design
  61. * @param \Magento\Framework\TranslateInterface $translate
  62. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  63. * @param ConfigInterface $inlineConfig
  64. * @param \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation
  65. * @param \Magento\Framework\Locale\ResolverInterface $localeResolver
  66. * @param \Psr\Log\LoggerInterface $logger
  67. * @param array $data
  68. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  69. */
  70. public function __construct(
  71. \Magento\Store\Model\StoreManagerInterface $storeManager,
  72. \Magento\Framework\View\DesignInterface $viewDesign,
  73. \Magento\Framework\App\DesignInterface $design,
  74. \Magento\Framework\TranslateInterface $translate,
  75. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  76. ConfigInterface $inlineConfig,
  77. \Magento\Framework\Translate\Inline\StateInterface $inlineTranslation,
  78. \Magento\Framework\Locale\ResolverInterface $localeResolver,
  79. \Psr\Log\LoggerInterface $logger,
  80. array $data = []
  81. ) {
  82. $this->_localeResolver = $localeResolver;
  83. parent::__construct($data);
  84. $this->_storeManager = $storeManager;
  85. $this->_viewDesign = $viewDesign;
  86. $this->_design = $design;
  87. $this->_translate = $translate;
  88. $this->_scopeConfig = $scopeConfig;
  89. $this->inlineConfig = $inlineConfig;
  90. $this->inlineTranslation = $inlineTranslation;
  91. $this->logger = $logger;
  92. }
  93. /**
  94. * Start environment emulation of the specified store
  95. *
  96. * Function returns information about initial store environment and emulates environment of another store
  97. *
  98. * @param integer $storeId
  99. * @param string $area
  100. * @param bool $force A true value will ensure that environment is always emulated, regardless of current store
  101. * @return void
  102. */
  103. public function startEnvironmentEmulation(
  104. $storeId,
  105. $area = \Magento\Framework\App\Area::AREA_FRONTEND,
  106. $force = false
  107. ) {
  108. // Only allow a single level of emulation
  109. if ($this->initialEnvironmentInfo !== null) {
  110. $this->logger->error(__('Environment emulation nesting is not allowed.'));
  111. return;
  112. }
  113. if ($storeId == $this->_storeManager->getStore()->getStoreId() && !$force) {
  114. return;
  115. }
  116. $this->storeCurrentEnvironmentInfo();
  117. // emulate inline translations
  118. $this->inlineTranslation->suspend($this->inlineConfig->isActive($storeId));
  119. // emulate design
  120. $storeTheme = $this->_viewDesign->getConfigurationDesignTheme($area, ['store' => $storeId]);
  121. $this->_viewDesign->setDesignTheme($storeTheme, $area);
  122. if ($area == \Magento\Framework\App\Area::AREA_FRONTEND) {
  123. $designChange = $this->_design->loadChange($storeId);
  124. if ($designChange->getData()) {
  125. $this->_viewDesign->setDesignTheme($designChange->getDesign(), $area);
  126. }
  127. }
  128. // Current store needs to be changed right before locale change and after design change
  129. $this->_storeManager->setCurrentStore($storeId);
  130. // emulate locale
  131. $newLocaleCode = $this->_scopeConfig->getValue(
  132. $this->_localeResolver->getDefaultLocalePath(),
  133. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  134. $storeId
  135. );
  136. $this->_localeResolver->setLocale($newLocaleCode);
  137. $this->_translate->setLocale($newLocaleCode);
  138. $this->_translate->loadData($area);
  139. return;
  140. }
  141. /**
  142. * Stop environment emulation
  143. *
  144. * Function restores initial store environment
  145. *
  146. * @return \Magento\Store\Model\App\Emulation
  147. */
  148. public function stopEnvironmentEmulation()
  149. {
  150. if ($this->initialEnvironmentInfo === null) {
  151. return $this;
  152. }
  153. $this->_restoreInitialInlineTranslation($this->initialEnvironmentInfo->getInitialTranslateInline());
  154. $initialDesign = $this->initialEnvironmentInfo->getInitialDesign();
  155. $this->_restoreInitialDesign($initialDesign);
  156. // Current store needs to be changed right before locale change and after design change
  157. $this->_storeManager->setCurrentStore($initialDesign['store']);
  158. $this->_restoreInitialLocale($this->initialEnvironmentInfo->getInitialLocaleCode(), $initialDesign['area']);
  159. $this->initialEnvironmentInfo = null;
  160. return $this;
  161. }
  162. /**
  163. * Stores current environment info
  164. *
  165. * @return void
  166. */
  167. public function storeCurrentEnvironmentInfo()
  168. {
  169. $this->initialEnvironmentInfo = new \Magento\Framework\DataObject();
  170. $this->initialEnvironmentInfo->setInitialTranslateInline(
  171. $this->inlineTranslation->isEnabled()
  172. )->setInitialDesign(
  173. [
  174. 'area' => $this->_viewDesign->getArea(),
  175. 'theme' => $this->_viewDesign->getDesignTheme(),
  176. 'store' => $this->_storeManager->getStore()->getStoreId(),
  177. ]
  178. )->setInitialLocaleCode(
  179. $this->_localeResolver->getLocale()
  180. );
  181. }
  182. /**
  183. * Restore initial inline translation state
  184. *
  185. * @param bool $initialTranslate
  186. * @return $this
  187. */
  188. protected function _restoreInitialInlineTranslation($initialTranslate)
  189. {
  190. $this->inlineTranslation->resume($initialTranslate);
  191. return $this;
  192. }
  193. /**
  194. * Restore design of the initial store
  195. *
  196. * @param array $initialDesign
  197. * @return $this
  198. */
  199. protected function _restoreInitialDesign(array $initialDesign)
  200. {
  201. $this->_viewDesign->setDesignTheme($initialDesign['theme'], $initialDesign['area']);
  202. return $this;
  203. }
  204. /**
  205. * Restore locale of the initial store
  206. *
  207. * @param string $initialLocaleCode
  208. * @param string $initialArea
  209. * @return $this
  210. */
  211. protected function _restoreInitialLocale(
  212. $initialLocaleCode,
  213. $initialArea = \Magento\Framework\App\Area::AREA_ADMINHTML
  214. ) {
  215. $this->_localeResolver->setLocale($initialLocaleCode);
  216. $this->_translate->setLocale($initialLocaleCode);
  217. $this->_translate->loadData($initialArea);
  218. return $this;
  219. }
  220. }