Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / State / OverrideCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\State\OverrideCommand.
6  */
7
8 namespace Drupal\Console\Command\State;
9
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;
19
20 /**
21  * Class DebugCommand
22  *
23  * @package Drupal\Console\Command\State
24  */
25 class OverrideCommand extends Command
26 {
27     use CommandTrait;
28
29     /**
30      * @var StateInterface
31      */
32     protected $state;
33
34     /**
35      * @var KeyValueFactoryInterface
36      */
37     protected $keyValue;
38
39     /**
40      * OverrideCommand constructor.
41      *
42      * @param StateInterface           $state
43      * @param KeyValueFactoryInterface $keyValue
44      */
45     public function __construct(
46         StateInterface $state,
47         KeyValueFactoryInterface $keyValue
48     ) {
49         $this->state = $state;
50         $this->keyValue = $keyValue;
51         parent::__construct();
52     }
53
54
55     /**
56      * {@inheritdoc}
57      */
58     protected function configure()
59     {
60         $this
61             ->setName('state:override')
62             ->setDescription($this->trans('commands.state.override.description'))
63             ->addArgument(
64                 'key',
65                 InputArgument::OPTIONAL,
66                 $this->trans('commands.state.override.arguments.key')
67             )
68             ->addArgument(
69                 'value',
70                 InputArgument::OPTIONAL,
71                 $this->trans('commands.state.override.arguments.value')
72             );
73     }
74     /**
75      * {@inheritdoc}
76      */
77     protected function interact(InputInterface $input, OutputInterface $output)
78     {
79         $io = new DrupalStyle($input, $output);
80         $key = $input->getArgument('key');
81         $value = $input->getArgument('value');
82
83         if (!$key) {
84             $names = array_keys($this->keyValue->get('state')->getAll());
85             $key = $io->choiceNoList(
86                 $this->trans('commands.state.override.arguments.key'),
87                 $names
88             );
89             $input->setArgument('key', $key);
90         }
91         if (!$value) {
92             $value = $io->ask(
93                 $this->trans('commands.state.override.arguments.value')
94             );
95             $input->setArgument('value', $value);
96         }
97     }
98     /**
99      * {@inheritdoc}
100      */
101     protected function execute(InputInterface $input, OutputInterface $output)
102     {
103         $io = new DrupalStyle($input, $output);
104         $key = $input->getArgument('key');
105         $value = $input->getArgument('value');
106
107         if (!$key) {
108             $io->error($this->trans('commands.state.override.errors.no-key'));
109
110             return 1;
111         }
112
113         if (!$value) {
114             $io->error($this->trans('commands.state.override.errors.no-value'));
115
116             return 1;
117         }
118
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);
123             $tableHeaders = [
124                 $this->trans('commands.state.override.messages.key'),
125                 $this->trans('commands.state.override.messages.original'),
126                 $this->trans('commands.state.override.messages.override')
127             ];
128
129             $tableRows[] = [$key, $originalValue, $overrideValue];
130
131             $io->table(
132                 $tableHeaders,
133                 $tableRows
134             );
135         }
136
137         return 0;
138     }
139 }