Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
State.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\App;
7 
17 class State
18 {
22  const PARAM_MODE = 'MAGE_MODE';
23 
29  protected $_appMode;
30 
36  protected $_isDownloader = false;
37 
43  protected $_updateMode = false;
44 
50  protected $_configScope;
51 
57  protected $_areaCode;
58 
64  protected $_isAreaCodeEmulated = false;
65 
69  private $areaList;
70 
74  const MODE_DEVELOPER = 'developer';
75 
76  const MODE_PRODUCTION = 'production';
77 
78  const MODE_DEFAULT = 'default';
79 
87  public function __construct(
88  \Magento\Framework\Config\ScopeInterface $configScope,
89  $mode = self::MODE_DEFAULT
90  ) {
91  $this->_configScope = $configScope;
92  switch ($mode) {
95  case self::MODE_DEFAULT:
96  $this->_appMode = $mode;
97  break;
98  default:
99  throw new \InvalidArgumentException("Unknown application mode: {$mode}");
100  }
101  }
102 
108  public function getMode()
109  {
110  return $this->_appMode;
111  }
112 
119  public function setIsDownloader($flag = true)
120  {
121  $this->_isDownloader = $flag;
122  }
123 
131  public function setAreaCode($code)
132  {
133  $this->checkAreaCode($code);
134 
135  if (isset($this->_areaCode)) {
136  throw new \Magento\Framework\Exception\LocalizedException(
137  new \Magento\Framework\Phrase('Area code is already set')
138  );
139  }
140  $this->_configScope->setCurrentScope($code);
141  $this->_areaCode = $code;
142  }
143 
150  public function getAreaCode()
151  {
152  if (!isset($this->_areaCode)) {
153  throw new \Magento\Framework\Exception\LocalizedException(
154  new \Magento\Framework\Phrase('Area code is not set')
155  );
156  }
157  return $this->_areaCode;
158  }
159 
165  public function isAreaCodeEmulated()
166  {
168  }
169 
179  public function emulateAreaCode($areaCode, $callback, $params = [])
180  {
181  $this->checkAreaCode($areaCode);
182 
183  $currentArea = $this->_areaCode;
184  $this->_areaCode = $areaCode;
185  $this->_isAreaCodeEmulated = true;
186  try {
187  $result = call_user_func_array($callback, $params);
188  } catch (\Exception $e) {
189  $this->_areaCode = $currentArea;
190  $this->_isAreaCodeEmulated = false;
191  throw $e;
192  }
193  $this->_areaCode = $currentArea;
194  $this->_isAreaCodeEmulated = false;
195  return $result;
196  }
197 
205  private function checkAreaCode($areaCode)
206  {
207  $areaCodes = array_merge(
209  $this->getAreaListInstance()->getCodes()
210  );
211 
212  if (!in_array($areaCode, $areaCodes)) {
213  throw new \Magento\Framework\Exception\LocalizedException(
214  new \Magento\Framework\Phrase('Area code "%1" does not exist', [$areaCode])
215  );
216  }
217  }
218 
225  private function getAreaListInstance()
226  {
227  if ($this->areaList === null) {
228  $this->areaList = ObjectManager::getInstance()->get(AreaList::class);
229  }
230 
231  return $this->areaList;
232  }
233 }
__construct(\Magento\Framework\Config\ScopeInterface $configScope, $mode=self::MODE_DEFAULT)
Definition: State.php:87
emulateAreaCode($areaCode, $callback, $params=[])
Definition: State.php:179
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
setIsDownloader($flag=true)
Definition: State.php:119
$code
Definition: info.phtml:12