Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigSetCommand.php
Go to the documentation of this file.
1 <?php
8 
10 use Magento\Config\Console\Command\ConfigSet\ProcessorFacadeFactory;
16 use Symfony\Component\Console\Command\Command;
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Input\InputOption;
20 use Symfony\Component\Console\Output\OutputInterface;
21 
29 class ConfigSetCommand extends Command
30 {
34  const ARG_PATH = 'path';
35  const ARG_VALUE = 'value';
36  const OPTION_SCOPE = 'scope';
37  const OPTION_SCOPE_CODE = 'scope-code';
38  const OPTION_LOCK = 'lock';
39  const OPTION_LOCK_ENV = 'lock-env';
40  const OPTION_LOCK_CONFIG = 'lock-config';
44  private $emulatedAreaProcessor;
45 
51  private $changeDetector;
52 
58  private $processorFacadeFactory;
59 
65  private $deploymentConfig;
66 
73  public function __construct(
74  EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor,
75  ChangeDetector $changeDetector,
76  ProcessorFacadeFactory $processorFacadeFactory,
77  DeploymentConfig $deploymentConfig
78  ) {
79  $this->emulatedAreaProcessor = $emulatedAreaProcessor;
80  $this->changeDetector = $changeDetector;
81  $this->processorFacadeFactory = $processorFacadeFactory;
82  $this->deploymentConfig = $deploymentConfig;
83 
84  parent::__construct();
85  }
86 
91  protected function configure()
92  {
93  $this->setName('config:set')
94  ->setDescription('Change system configuration')
95  ->setDefinition([
96  new InputArgument(
97  static::ARG_PATH,
98  InputArgument::REQUIRED,
99  'Configuration path in format section/group/field_name'
100  ),
101  new InputArgument(static::ARG_VALUE, InputArgument::REQUIRED, 'Configuration value'),
102  new InputOption(
103  static::OPTION_SCOPE,
104  null,
105  InputArgument::OPTIONAL,
106  'Configuration scope (default, website, or store)',
108  ),
109  new InputOption(
110  static::OPTION_SCOPE_CODE,
111  null,
112  InputArgument::OPTIONAL,
113  'Scope code (required only if scope is not \'default\')'
114  ),
115  new InputOption(
116  static::OPTION_LOCK_ENV,
117  'le',
118  InputOption::VALUE_NONE,
119  'Lock value which prevents modification in the Admin (will be saved in app/etc/env.php)'
120  ),
121  new InputOption(
122  static::OPTION_LOCK_CONFIG,
123  'lc',
124  InputOption::VALUE_NONE,
125  'Lock and share value with other installations, prevents modification in the Admin '
126  . '(will be saved in app/etc/config.php)'
127  ),
128  new InputOption(
129  static::OPTION_LOCK,
130  'l',
131  InputOption::VALUE_NONE,
132  'Deprecated, use the --' . static::OPTION_LOCK_ENV . ' option instead.'
133  ),
134  ]);
135 
136  parent::configure();
137  }
138 
145  protected function execute(InputInterface $input, OutputInterface $output)
146  {
147  if (!$this->deploymentConfig->isAvailable()) {
148  $output->writeln(
149  '<error>You cannot run this command because the Magento application is not installed.</error>'
150  );
151  return Cli::RETURN_FAILURE;
152  }
153  if ($this->changeDetector->hasChanges(System::CONFIG_TYPE)) {
154  $output->writeln(
155  '<error>'
156  . 'This command is unavailable right now. '
157  . 'To continue working with it please run app:config:import or setup:upgrade command before.'
158  . '</error>'
159  );
160 
161  return Cli::RETURN_FAILURE;
162  }
163 
164  try {
165  $message = $this->emulatedAreaProcessor->process(function () use ($input) {
166 
167  $lock = $input->getOption(static::OPTION_LOCK_ENV)
168  || $input->getOption(static::OPTION_LOCK_CONFIG)
169  || $input->getOption(static::OPTION_LOCK);
170 
171  $lockTargetPath = ConfigFilePool::APP_ENV;
172  if ($input->getOption(static::OPTION_LOCK_CONFIG)) {
173  $lockTargetPath = ConfigFilePool::APP_CONFIG;
174  }
175 
176  return $this->processorFacadeFactory->create()->processWithLockTarget(
177  $input->getArgument(static::ARG_PATH),
178  $input->getArgument(static::ARG_VALUE),
179  $input->getOption(static::OPTION_SCOPE),
180  $input->getOption(static::OPTION_SCOPE_CODE),
181  $lock,
182  $lockTargetPath
183  );
184  });
185 
186  $output->writeln('<info>' . $message . '</info>');
187 
188  return Cli::RETURN_SUCCESS;
189  } catch (\Exception $exception) {
190  $output->writeln('<error>' . $exception->getMessage() . '</error>');
191 
192  return Cli::RETURN_FAILURE;
193  }
194  }
195 }
__construct(EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor, ChangeDetector $changeDetector, ProcessorFacadeFactory $processorFacadeFactory, DeploymentConfig $deploymentConfig)
$message
$deploymentConfig
execute(InputInterface $input, OutputInterface $output)