State.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App;
  7. /**
  8. * Application state flags.
  9. * Can be used to retrieve current application mode and current area.
  10. *
  11. * Note: Area code communication and emulation will be removed from this class.
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class State
  17. {
  18. /**
  19. * Application run code
  20. */
  21. const PARAM_MODE = 'MAGE_MODE';
  22. /**
  23. * Application mode
  24. *
  25. * @var string
  26. */
  27. protected $_appMode;
  28. /**
  29. * Is downloader flag
  30. *
  31. * @var bool
  32. */
  33. protected $_isDownloader = false;
  34. /**
  35. * Update mode flag
  36. *
  37. * @var bool
  38. */
  39. protected $_updateMode = false;
  40. /**
  41. * Config scope model
  42. *
  43. * @var \Magento\Framework\Config\ScopeInterface
  44. */
  45. protected $_configScope;
  46. /**
  47. * Area code
  48. *
  49. * @var string
  50. */
  51. protected $_areaCode;
  52. /**
  53. * Is area code being emulated
  54. *
  55. * @var bool
  56. */
  57. protected $_isAreaCodeEmulated = false;
  58. /**
  59. * @var AreaList
  60. */
  61. private $areaList;
  62. /**#@+
  63. * Application modes
  64. */
  65. const MODE_DEVELOPER = 'developer';
  66. const MODE_PRODUCTION = 'production';
  67. const MODE_DEFAULT = 'default';
  68. /**#@-*/
  69. /**
  70. * @param \Magento\Framework\Config\ScopeInterface $configScope
  71. * @param string $mode
  72. * @throws \LogicException
  73. */
  74. public function __construct(
  75. \Magento\Framework\Config\ScopeInterface $configScope,
  76. $mode = self::MODE_DEFAULT
  77. ) {
  78. $this->_configScope = $configScope;
  79. switch ($mode) {
  80. case self::MODE_DEVELOPER:
  81. case self::MODE_PRODUCTION:
  82. case self::MODE_DEFAULT:
  83. $this->_appMode = $mode;
  84. break;
  85. default:
  86. throw new \InvalidArgumentException("Unknown application mode: {$mode}");
  87. }
  88. }
  89. /**
  90. * Return current app mode
  91. *
  92. * @return string
  93. */
  94. public function getMode()
  95. {
  96. return $this->_appMode;
  97. }
  98. /**
  99. * Set is downloader flag
  100. *
  101. * @param bool $flag
  102. * @return void
  103. */
  104. public function setIsDownloader($flag = true)
  105. {
  106. $this->_isDownloader = $flag;
  107. }
  108. /**
  109. * Set area code
  110. *
  111. * @param string $code
  112. * @return void
  113. * @throws \Magento\Framework\Exception\LocalizedException
  114. */
  115. public function setAreaCode($code)
  116. {
  117. $this->checkAreaCode($code);
  118. if (isset($this->_areaCode)) {
  119. throw new \Magento\Framework\Exception\LocalizedException(
  120. new \Magento\Framework\Phrase('Area code is already set')
  121. );
  122. }
  123. $this->_configScope->setCurrentScope($code);
  124. $this->_areaCode = $code;
  125. }
  126. /**
  127. * Get area code
  128. *
  129. * @return string
  130. * @throws \Magento\Framework\Exception\LocalizedException
  131. */
  132. public function getAreaCode()
  133. {
  134. if (!isset($this->_areaCode)) {
  135. throw new \Magento\Framework\Exception\LocalizedException(
  136. new \Magento\Framework\Phrase('Area code is not set')
  137. );
  138. }
  139. return $this->_areaCode;
  140. }
  141. /**
  142. * Checks whether area code is being emulated
  143. *
  144. * @return bool
  145. */
  146. public function isAreaCodeEmulated()
  147. {
  148. return $this->_isAreaCodeEmulated;
  149. }
  150. /**
  151. * Emulate callback inside some area code
  152. *
  153. * @param string $areaCode
  154. * @param callable $callback
  155. * @param array $params
  156. * @return mixed
  157. * @throws \Exception
  158. */
  159. public function emulateAreaCode($areaCode, $callback, $params = [])
  160. {
  161. $this->checkAreaCode($areaCode);
  162. $currentArea = $this->_areaCode;
  163. $this->_areaCode = $areaCode;
  164. $this->_isAreaCodeEmulated = true;
  165. try {
  166. $result = call_user_func_array($callback, $params);
  167. } catch (\Exception $e) {
  168. $this->_areaCode = $currentArea;
  169. $this->_isAreaCodeEmulated = false;
  170. throw $e;
  171. }
  172. $this->_areaCode = $currentArea;
  173. $this->_isAreaCodeEmulated = false;
  174. return $result;
  175. }
  176. /**
  177. * Check that area code exists
  178. *
  179. * @param string $areaCode
  180. * @throws \Magento\Framework\Exception\LocalizedException
  181. * @return void
  182. */
  183. private function checkAreaCode($areaCode)
  184. {
  185. $areaCodes = array_merge(
  186. [Area::AREA_GLOBAL],
  187. $this->getAreaListInstance()->getCodes()
  188. );
  189. if (!in_array($areaCode, $areaCodes)) {
  190. throw new \Magento\Framework\Exception\LocalizedException(
  191. new \Magento\Framework\Phrase('Area code "%1" does not exist', [$areaCode])
  192. );
  193. }
  194. }
  195. /**
  196. * Get Instance of AreaList
  197. *
  198. * @return AreaList
  199. * @deprecated 101.0.0
  200. */
  201. private function getAreaListInstance()
  202. {
  203. if ($this->areaList === null) {
  204. $this->areaList = ObjectManager::getInstance()->get(AreaList::class);
  205. }
  206. return $this->areaList;
  207. }
  208. }