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 Drupal\Console\Core\Command\Command;
14 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
15 use Drupal\Core\State\StateInterface;
16 use Drupal\Component\Serialization\Yaml;
21 * @package Drupal\Console\Command\State
23 class OverrideCommand extends Command
31 * @var KeyValueFactoryInterface
36 * OverrideCommand constructor.
38 * @param StateInterface $state
39 * @param KeyValueFactoryInterface $keyValue
41 public function __construct(
42 StateInterface $state,
43 KeyValueFactoryInterface $keyValue
45 $this->state = $state;
46 $this->keyValue = $keyValue;
47 parent::__construct();
54 protected function configure()
57 ->setName('state:override')
58 ->setDescription($this->trans('commands.state.override.description'))
61 InputArgument::OPTIONAL,
62 $this->trans('commands.state.override.arguments.key')
66 InputArgument::OPTIONAL,
67 $this->trans('commands.state.override.arguments.value')
68 )->setAliases(['sto']);
73 protected function interact(InputInterface $input, OutputInterface $output)
75 $key = $input->getArgument('key');
76 $value = $input->getArgument('value');
79 $names = array_keys($this->keyValue->get('state')->getAll());
80 $key = $this->getIo()->choiceNoList(
81 $this->trans('commands.state.override.arguments.key'),
84 $input->setArgument('key', $key);
87 $value = $this->getIo()->ask(
88 $this->trans('commands.state.override.arguments.value')
90 $input->setArgument('value', $value);
96 protected function execute(InputInterface $input, OutputInterface $output)
98 $key = $input->getArgument('key');
99 $value = $input->getArgument('value');
102 $this->getIo()->error($this->trans('commands.state.override.errors.no-key'));
108 $this->getIo()->error($this->trans('commands.state.override.errors.no-value'));
113 if ($key && $value) {
114 $originalValue = Yaml::encode($this->state->get($key));
115 $overrideValue = is_array($value)?Yaml::encode($value):$value;
116 $this->state->set($key, $overrideValue);
118 $this->trans('commands.state.override.messages.key'),
119 $this->trans('commands.state.override.messages.original'),
120 $this->trans('commands.state.override.messages.override')
123 $tableRows[] = [$key, $originalValue, $overrideValue];
125 $this->getIo()->table(