5 * Contains \Drupal\Console\Command\State\OverrideCommand.
8 namespace Drupal\Console\Command\State;
10 use Symfony\Component\Console\Input\InputArgument;
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Command\Command;
14 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
15 use Drupal\Core\State\StateInterface;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18 use Drupal\Component\Serialization\Yaml;
23 * @package Drupal\Console\Command\State
25 class OverrideCommand extends Command
35 * @var KeyValueFactoryInterface
40 * OverrideCommand constructor.
42 * @param StateInterface $state
43 * @param KeyValueFactoryInterface $keyValue
45 public function __construct(
46 StateInterface $state,
47 KeyValueFactoryInterface $keyValue
49 $this->state = $state;
50 $this->keyValue = $keyValue;
51 parent::__construct();
58 protected function configure()
61 ->setName('state:override')
62 ->setDescription($this->trans('commands.state.override.description'))
65 InputArgument::OPTIONAL,
66 $this->trans('commands.state.override.arguments.key')
70 InputArgument::OPTIONAL,
71 $this->trans('commands.state.override.arguments.value')
77 protected function interact(InputInterface $input, OutputInterface $output)
79 $io = new DrupalStyle($input, $output);
80 $key = $input->getArgument('key');
81 $value = $input->getArgument('value');
84 $names = array_keys($this->keyValue->get('state')->getAll());
85 $key = $io->choiceNoList(
86 $this->trans('commands.state.override.arguments.key'),
89 $input->setArgument('key', $key);
93 $this->trans('commands.state.override.arguments.value')
95 $input->setArgument('value', $value);
101 protected function execute(InputInterface $input, OutputInterface $output)
103 $io = new DrupalStyle($input, $output);
104 $key = $input->getArgument('key');
105 $value = $input->getArgument('value');
108 $io->error($this->trans('commands.state.override.errors.no-key'));
114 $io->error($this->trans('commands.state.override.errors.no-value'));
119 if ($key && $value) {
120 $originalValue = Yaml::encode($this->state->get($key));
121 $overrideValue = is_array($value)?Yaml::encode($value):$value;
122 $this->state->set($key, $overrideValue);
124 $this->trans('commands.state.override.messages.key'),
125 $this->trans('commands.state.override.messages.original'),
126 $this->trans('commands.state.override.messages.override')
129 $tableRows[] = [$key, $originalValue, $overrideValue];