Mode.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Deploy\Model;
  7. use Magento\Deploy\App\Mode\ConfigProvider;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Framework\App\Console\MaintenanceModeEnabler;
  10. use Magento\Framework\App\DeploymentConfig\Reader;
  11. use Magento\Framework\App\DeploymentConfig\Writer;
  12. use Magento\Framework\App\Filesystem\DirectoryList;
  13. use Magento\Framework\App\MaintenanceMode;
  14. use Magento\Framework\App\State;
  15. use Magento\Framework\Config\File\ConfigFilePool;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Magento\Config\Console\Command\ConfigSet\ProcessorFacadeFactory;
  19. use Magento\Config\Console\Command\EmulatedAdminhtmlAreaProcessor;
  20. use Magento\Framework\Exception\LocalizedException;
  21. use Magento\Framework\App\ObjectManager;
  22. /**
  23. * A class to manage Magento modes
  24. *
  25. * @SuppressWarnings("PMD.CouplingBetweenObjects")
  26. * @SuppressWarnings("PMD.ExcessiveParameterList")
  27. */
  28. class Mode
  29. {
  30. /**
  31. * @var InputInterface
  32. */
  33. private $input;
  34. /**
  35. * @var OutputInterface
  36. */
  37. protected $output;
  38. /**
  39. * @var Writer
  40. */
  41. private $writer;
  42. /**
  43. * @var Reader
  44. */
  45. private $reader;
  46. /**
  47. * @var Filesystem
  48. */
  49. private $filesystem;
  50. /**
  51. * @var ConfigProvider
  52. */
  53. private $configProvider;
  54. /**
  55. * The factory for processor facade.
  56. *
  57. * @var ProcessorFacadeFactory
  58. */
  59. private $processorFacadeFactory;
  60. /**
  61. * Emulator adminhtml area for CLI command.
  62. *
  63. * @var EmulatedAdminhtmlAreaProcessor
  64. */
  65. private $emulatedAreaProcessor;
  66. /**
  67. * @var MaintenanceModeEnabler
  68. */
  69. private $maintenanceModeEnabler;
  70. /**
  71. * @param InputInterface $input
  72. * @param OutputInterface $output
  73. * @param Writer $writer
  74. * @param Reader $reader
  75. * @param MaintenanceMode $maintenanceMode deprecated, use $maintenanceModeEnabler instead
  76. * @param Filesystem $filesystem
  77. * @param ConfigProvider $configProvider
  78. * @param ProcessorFacadeFactory $processorFacadeFactory
  79. * @param EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor
  80. * @param MaintenanceModeEnabler $maintenanceModeEnabler
  81. *
  82. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  83. */
  84. public function __construct(
  85. InputInterface $input,
  86. OutputInterface $output,
  87. Writer $writer,
  88. Reader $reader,
  89. MaintenanceMode $maintenanceMode,
  90. Filesystem $filesystem,
  91. ConfigProvider $configProvider = null,
  92. ProcessorFacadeFactory $processorFacadeFactory = null,
  93. EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor = null,
  94. MaintenanceModeEnabler $maintenanceModeEnabler = null
  95. ) {
  96. $this->input = $input;
  97. $this->output = $output;
  98. $this->writer = $writer;
  99. $this->reader = $reader;
  100. $this->filesystem = $filesystem;
  101. $this->configProvider =
  102. $configProvider ?: ObjectManager::getInstance()->get(ConfigProvider::class);
  103. $this->processorFacadeFactory =
  104. $processorFacadeFactory ?: ObjectManager::getInstance()->get(ProcessorFacadeFactory::class);
  105. $this->emulatedAreaProcessor =
  106. $emulatedAreaProcessor ?: ObjectManager::getInstance()->get(EmulatedAdminhtmlAreaProcessor::class);
  107. $this->maintenanceModeEnabler =
  108. $maintenanceModeEnabler ?: ObjectManager::getInstance()->get(MaintenanceModeEnabler::class);
  109. }
  110. /**
  111. * Enable production mode
  112. *
  113. * @throws LocalizedException
  114. * @return void
  115. */
  116. public function enableProductionMode()
  117. {
  118. $this->maintenanceModeEnabler->executeInMaintenanceMode(
  119. function () {
  120. $previousMode = $this->getMode();
  121. try {
  122. // We have to turn on production mode before generation.
  123. // We need this to enable generation of the "min" files.
  124. $this->setStoreMode(State::MODE_PRODUCTION);
  125. $this->filesystem->regenerateStatic($this->output);
  126. } catch (LocalizedException $e) {
  127. // We have to return store mode to previous state in case of error.
  128. $this->setStoreMode($previousMode);
  129. throw $e;
  130. }
  131. },
  132. $this->output,
  133. false
  134. );
  135. }
  136. /**
  137. * Only lock static resource locations and set store mode, without handling static content
  138. *
  139. * @return void
  140. */
  141. public function enableProductionModeMinimal()
  142. {
  143. $this->setStoreMode(State::MODE_PRODUCTION);
  144. }
  145. /**
  146. * Enable Developer mode
  147. *
  148. * @return void
  149. */
  150. public function enableDeveloperMode()
  151. {
  152. $this->filesystem->cleanupFilesystem(
  153. [
  154. DirectoryList::CACHE,
  155. DirectoryList::GENERATED_CODE,
  156. DirectoryList::GENERATED_METADATA,
  157. DirectoryList::TMP_MATERIALIZATION_DIR,
  158. DirectoryList::STATIC_VIEW,
  159. ]
  160. );
  161. $this->setStoreMode(State::MODE_DEVELOPER);
  162. }
  163. /**
  164. * Enable Default mode.
  165. *
  166. * @return void
  167. */
  168. public function enableDefaultMode()
  169. {
  170. $this->filesystem->cleanupFilesystem(
  171. [
  172. DirectoryList::CACHE,
  173. DirectoryList::GENERATED_CODE,
  174. DirectoryList::GENERATED_METADATA,
  175. DirectoryList::TMP_MATERIALIZATION_DIR,
  176. DirectoryList::STATIC_VIEW,
  177. ]
  178. );
  179. $this->setStoreMode(State::MODE_DEFAULT);
  180. }
  181. /**
  182. * Get current mode information
  183. *
  184. * @return string
  185. * @throws \Exception
  186. */
  187. public function getMode()
  188. {
  189. $env = $this->reader->load();
  190. return isset($env[State::PARAM_MODE]) ? $env[State::PARAM_MODE] : null;
  191. }
  192. /**
  193. * Store mode in env.php
  194. *
  195. * @param string $mode
  196. * @return void
  197. */
  198. protected function setStoreMode($mode)
  199. {
  200. $this->saveAppConfigs($mode);
  201. $data = [
  202. ConfigFilePool::APP_ENV => [
  203. State::PARAM_MODE => $mode
  204. ]
  205. ];
  206. $this->writer->saveConfig($data);
  207. }
  208. /**
  209. * Save application configs while switching mode
  210. *
  211. * @param string $mode
  212. * @return void
  213. */
  214. private function saveAppConfigs($mode)
  215. {
  216. $configs = $this->configProvider->getConfigs($this->getMode(), $mode);
  217. foreach ($configs as $path => $value) {
  218. $this->emulatedAreaProcessor->process(function () use ($path, $value) {
  219. $this->processorFacadeFactory->create()->processWithLockTarget(
  220. $path,
  221. $value,
  222. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  223. null,
  224. true
  225. );
  226. });
  227. $this->output->writeln('Config "' . $path . ' = ' . $value . '" has been saved.');
  228. }
  229. }
  230. }