ConfigSetCommand.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Console\Command;
  7. use Magento\Config\App\Config\Type\System;
  8. use Magento\Config\Console\Command\ConfigSet\ProcessorFacadeFactory;
  9. use Magento\Deploy\Model\DeploymentConfig\ChangeDetector;
  10. use Magento\Framework\App\DeploymentConfig;
  11. use Magento\Framework\App\Config\ScopeConfigInterface;
  12. use Magento\Framework\Config\File\ConfigFilePool;
  13. use Magento\Framework\Console\Cli;
  14. use Symfony\Component\Console\Command\Command;
  15. use Symfony\Component\Console\Input\InputArgument;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use Symfony\Component\Console\Output\OutputInterface;
  19. /**
  20. * Command provides possibility to change system configuration.
  21. *
  22. * @api
  23. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  24. * @since 101.0.0
  25. */
  26. class ConfigSetCommand extends Command
  27. {
  28. /**#@+
  29. * Constants for arguments and options.
  30. */
  31. const ARG_PATH = 'path';
  32. const ARG_VALUE = 'value';
  33. const OPTION_SCOPE = 'scope';
  34. const OPTION_SCOPE_CODE = 'scope-code';
  35. const OPTION_LOCK = 'lock';
  36. const OPTION_LOCK_ENV = 'lock-env';
  37. const OPTION_LOCK_CONFIG = 'lock-config';
  38. /**#@-*/
  39. /**#@-*/
  40. private $emulatedAreaProcessor;
  41. /**
  42. * The config change detector.
  43. *
  44. * @var ChangeDetector
  45. */
  46. private $changeDetector;
  47. /**
  48. * The factory for processor facade.
  49. *
  50. * @var ProcessorFacadeFactory
  51. */
  52. private $processorFacadeFactory;
  53. /**
  54. * Application deployment configuration
  55. *
  56. * @var DeploymentConfig
  57. */
  58. private $deploymentConfig;
  59. /**
  60. * @param EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor Emulator adminhtml area for CLI command
  61. * @param ChangeDetector $changeDetector The config change detector
  62. * @param ProcessorFacadeFactory $processorFacadeFactory The factory for processor facade
  63. * @param DeploymentConfig $deploymentConfig Application deployment configuration
  64. */
  65. public function __construct(
  66. EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor,
  67. ChangeDetector $changeDetector,
  68. ProcessorFacadeFactory $processorFacadeFactory,
  69. DeploymentConfig $deploymentConfig
  70. ) {
  71. $this->emulatedAreaProcessor = $emulatedAreaProcessor;
  72. $this->changeDetector = $changeDetector;
  73. $this->processorFacadeFactory = $processorFacadeFactory;
  74. $this->deploymentConfig = $deploymentConfig;
  75. parent::__construct();
  76. }
  77. /**
  78. * @inheritdoc
  79. * @since 101.0.0
  80. */
  81. protected function configure()
  82. {
  83. $this->setName('config:set')
  84. ->setDescription('Change system configuration')
  85. ->setDefinition([
  86. new InputArgument(
  87. static::ARG_PATH,
  88. InputArgument::REQUIRED,
  89. 'Configuration path in format section/group/field_name'
  90. ),
  91. new InputArgument(static::ARG_VALUE, InputArgument::REQUIRED, 'Configuration value'),
  92. new InputOption(
  93. static::OPTION_SCOPE,
  94. null,
  95. InputArgument::OPTIONAL,
  96. 'Configuration scope (default, website, or store)',
  97. ScopeConfigInterface::SCOPE_TYPE_DEFAULT
  98. ),
  99. new InputOption(
  100. static::OPTION_SCOPE_CODE,
  101. null,
  102. InputArgument::OPTIONAL,
  103. 'Scope code (required only if scope is not \'default\')'
  104. ),
  105. new InputOption(
  106. static::OPTION_LOCK_ENV,
  107. 'le',
  108. InputOption::VALUE_NONE,
  109. 'Lock value which prevents modification in the Admin (will be saved in app/etc/env.php)'
  110. ),
  111. new InputOption(
  112. static::OPTION_LOCK_CONFIG,
  113. 'lc',
  114. InputOption::VALUE_NONE,
  115. 'Lock and share value with other installations, prevents modification in the Admin '
  116. . '(will be saved in app/etc/config.php)'
  117. ),
  118. new InputOption(
  119. static::OPTION_LOCK,
  120. 'l',
  121. InputOption::VALUE_NONE,
  122. 'Deprecated, use the --' . static::OPTION_LOCK_ENV . ' option instead.'
  123. ),
  124. ]);
  125. parent::configure();
  126. }
  127. /**
  128. * Creates and run appropriate processor, depending on input options.
  129. *
  130. * {@inheritdoc}
  131. * @since 101.0.0
  132. */
  133. protected function execute(InputInterface $input, OutputInterface $output)
  134. {
  135. if (!$this->deploymentConfig->isAvailable()) {
  136. $output->writeln(
  137. '<error>You cannot run this command because the Magento application is not installed.</error>'
  138. );
  139. return Cli::RETURN_FAILURE;
  140. }
  141. if ($this->changeDetector->hasChanges(System::CONFIG_TYPE)) {
  142. $output->writeln(
  143. '<error>'
  144. . 'This command is unavailable right now. '
  145. . 'To continue working with it please run app:config:import or setup:upgrade command before.'
  146. . '</error>'
  147. );
  148. return Cli::RETURN_FAILURE;
  149. }
  150. try {
  151. $message = $this->emulatedAreaProcessor->process(function () use ($input) {
  152. $lock = $input->getOption(static::OPTION_LOCK_ENV)
  153. || $input->getOption(static::OPTION_LOCK_CONFIG)
  154. || $input->getOption(static::OPTION_LOCK);
  155. $lockTargetPath = ConfigFilePool::APP_ENV;
  156. if ($input->getOption(static::OPTION_LOCK_CONFIG)) {
  157. $lockTargetPath = ConfigFilePool::APP_CONFIG;
  158. }
  159. return $this->processorFacadeFactory->create()->processWithLockTarget(
  160. $input->getArgument(static::ARG_PATH),
  161. $input->getArgument(static::ARG_VALUE),
  162. $input->getOption(static::OPTION_SCOPE),
  163. $input->getOption(static::OPTION_SCOPE_CODE),
  164. $lock,
  165. $lockTargetPath
  166. );
  167. });
  168. $output->writeln('<info>' . $message . '</info>');
  169. return Cli::RETURN_SUCCESS;
  170. } catch (\Exception $exception) {
  171. $output->writeln('<error>' . $exception->getMessage() . '</error>');
  172. return Cli::RETURN_FAILURE;
  173. }
  174. }
  175. }