Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Emulation Class Reference
Inheritance diagram for Emulation:
DataObject

Public Member Functions

 __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=[])
 
 startEnvironmentEmulation ( $storeId, $area=\Magento\Framework\App\Area::AREA_FRONTEND, $force=false)
 
 stopEnvironmentEmulation ()
 
 storeCurrentEnvironmentInfo ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 

Protected Member Functions

 _restoreInitialInlineTranslation ($initialTranslate)
 
 _restoreInitialDesign (array $initialDesign)
 
 _restoreInitialLocale ( $initialLocaleCode, $initialArea=\Magento\Framework\App\Area::AREA_ADMINHTML)
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_storeManager
 
 $_translate
 
 $_scopeConfig
 
 $_localeResolver
 
 $_design
 
 $inlineConfig
 
 $inlineTranslation
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

@api

Since
100.0.2

Definition at line 18 of file Emulation.php.

Constructor & Destructor Documentation

◆ __construct()

__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 = [] 
)
Parameters
\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@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 82 of file Emulation.php.

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  }
$storeManager

Member Function Documentation

◆ _restoreInitialDesign()

_restoreInitialDesign ( array  $initialDesign)
protected

Restore design of the initial store

Parameters
array$initialDesign
Returns
$this

Definition at line 225 of file Emulation.php.

226  {
227  $this->_viewDesign->setDesignTheme($initialDesign['theme'], $initialDesign['area']);
228  return $this;
229  }

◆ _restoreInitialInlineTranslation()

_restoreInitialInlineTranslation (   $initialTranslate)
protected

Restore initial inline translation state

Parameters
bool$initialTranslate
Returns
$this

Definition at line 213 of file Emulation.php.

214  {
215  $this->inlineTranslation->resume($initialTranslate);
216  return $this;
217  }

◆ _restoreInitialLocale()

_restoreInitialLocale (   $initialLocaleCode,
  $initialArea = \Magento\Framework\App\Area::AREA_ADMINHTML 
)
protected

Restore locale of the initial store

Parameters
string$initialLocaleCode
string$initialArea
Returns
$this

Definition at line 238 of file Emulation.php.

241  {
242  $this->_localeResolver->setLocale($initialLocaleCode);
243  $this->_translate->setLocale($initialLocaleCode);
244  $this->_translate->loadData($initialArea);
245 
246  return $this;
247  }

◆ startEnvironmentEmulation()

startEnvironmentEmulation (   $storeId,
  $area = \Magento\Framework\App\Area::AREA_FRONTEND,
  $force = false 
)

Start environment emulation of the specified store

Function returns information about initial store environment and emulates environment of another store

Parameters
integer$storeId
string$area
bool$forceA true value will ensure that environment is always emulated, regardless of current store
Returns
void

Definition at line 116 of file Emulation.php.

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  }
__()
Definition: __.php:13

◆ stopEnvironmentEmulation()

stopEnvironmentEmulation ( )

Stop environment emulation

Function restores initial store environment

Returns
\Magento\Store\Model\App\Emulation

Definition at line 169 of file Emulation.php.

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  }
_restoreInitialDesign(array $initialDesign)
Definition: Emulation.php:225
_restoreInitialLocale( $initialLocaleCode, $initialArea=\Magento\Framework\App\Area::AREA_ADMINHTML)
Definition: Emulation.php:238
_restoreInitialInlineTranslation($initialTranslate)
Definition: Emulation.php:213

◆ storeCurrentEnvironmentInfo()

storeCurrentEnvironmentInfo ( )

Stores current environment info

Returns
void

Definition at line 191 of file Emulation.php.

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  }

Field Documentation

◆ $_design

$_design
protected

Definition at line 45 of file Emulation.php.

◆ $_localeResolver

$_localeResolver
protected

Definition at line 40 of file Emulation.php.

◆ $_scopeConfig

$_scopeConfig
protected

Definition at line 35 of file Emulation.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 23 of file Emulation.php.

◆ $_translate

$_translate
protected

Definition at line 28 of file Emulation.php.

◆ $inlineConfig

$inlineConfig
protected

Definition at line 50 of file Emulation.php.

◆ $inlineTranslation

$inlineTranslation
protected

Definition at line 55 of file Emulation.php.


The documentation for this class was generated from the following file: