50385ceba13b0642f304195a96b53ab15fe5876f
[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 Drupal\Console\Core\Command\Command;
14 use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
15 use Drupal\Core\State\StateInterface;
16 use Drupal\Component\Serialization\Yaml;
17
18 /**
19  * Class DebugCommand
20  *
21  * @package Drupal\Console\Command\State
22  */
23 class OverrideCommand extends Command
24 {
25     /**
26      * @var StateInterface
27      */
28     protected $state;
29
30     /**
31      * @var KeyValueFactoryInterface
32      */
33     protected $keyValue;
34
35     /**
36      * OverrideCommand constructor.
37      *
38      * @param StateInterface           $state
39      * @param KeyValueFactoryInterface $keyValue
40      */
41     public function __construct(
42         StateInterface $state,
43         KeyValueFactoryInterface $keyValue
44     ) {
45         $this->state = $state;
46         $this->keyValue = $keyValue;
47         parent::__construct();
48     }
49
50
51     /**
52      * {@inheritdoc}
53      */
54     protected function configure()
55     {
56         $this
57             ->setName('state:override')
58             ->setDescription($this->trans('commands.state.override.description'))
59             ->addArgument(
60                 'key',
61                 InputArgument::OPTIONAL,
62                 $this->trans('commands.state.override.arguments.key')
63             )
64             ->addArgument(
65                 'value',
66                 InputArgument::OPTIONAL,
67                 $this->trans('commands.state.override.arguments.value')
68             )->setAliases(['sto']);
69     }
70     /**
71      * {@inheritdoc}
72      */
73     protected function interact(InputInterface $input, OutputInterface $output)
74     {
75         $key = $input->getArgument('key');
76         $value = $input->getArgument('value');
77
78         if (!$key) {
79             $names = array_keys($this->keyValue->get('state')->getAll());
80             $key = $this->getIo()->choiceNoList(
81                 $this->trans('commands.state.override.arguments.key'),
82                 $names
83             );
84             $input->setArgument('key', $key);
85         }
86         if (!$value) {
87             $value = $this->getIo()->ask(
88                 $this->trans('commands.state.override.arguments.value')
89             );
90             $input->setArgument('value', $value);
91         }
92     }
93     /**
94      * {@inheritdoc}
95      */
96     protected function execute(InputInterface $input, OutputInterface $output)
97     {
98         $key = $input->getArgument('key');
99         $value = $input->getArgument('value');
100
101         if (!$key) {
102             $this->getIo()->error($this->trans('commands.state.override.errors.no-key'));
103
104             return 1;
105         }
106
107         if (!$value) {
108             $this->getIo()->error($this->trans('commands.state.override.errors.no-value'));
109
110             return 1;
111         }
112
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);
117             $tableHeaders = [
118                 $this->trans('commands.state.override.messages.key'),
119                 $this->trans('commands.state.override.messages.original'),
120                 $this->trans('commands.state.override.messages.override')
121             ];
122
123             $tableRows[] = [$key, $originalValue, $overrideValue];
124
125             $this->getIo()->table(
126                 $tableHeaders,
127                 $tableRows
128             );
129         }
130
131         return 0;
132     }
133 }