Version 1
[yaffs-website] / vendor / drupal / console / src / Command / Config / OverrideCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Config\OverrideCommand.
6  */
7
8 namespace Drupal\Console\Command\Config;
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\Config\CachedStorage;
15 use Drupal\Core\Config\ConfigFactory;
16 use Drupal\Console\Core\Command\Shared\CommandTrait;
17 use Drupal\Console\Core\Style\DrupalStyle;
18
19 class OverrideCommand extends Command
20 {
21     use CommandTrait;
22
23     /**
24      * @var CachedStorage
25      */
26     protected $configStorage;
27
28     /**
29      * @var ConfigFactory
30      */
31     protected $configFactory;
32
33     /**
34      * OverrideCommand constructor.
35      *
36      * @param CachedStorage $configStorage
37      * @param ConfigFactory $configFactory
38      */
39     public function __construct(
40         CachedStorage $configStorage,
41         ConfigFactory $configFactory
42     ) {
43         $this->configStorage = $configStorage;
44         $this->configFactory = $configFactory;
45         parent::__construct();
46     }
47
48     protected function configure()
49     {
50         $this
51             ->setName('config:override')
52             ->setDescription($this->trans('commands.config.override.description'))
53             ->addArgument(
54                 'name',
55                 InputArgument::REQUIRED,
56                 $this->trans('commands.config.override.arguments.name')
57             )
58             ->addArgument('key', InputArgument::REQUIRED, $this->trans('commands.config.override.arguments.key'))
59             ->addArgument('value', InputArgument::REQUIRED, $this->trans('commands.config.override.arguments.value'));
60     }
61
62     /**
63      * {@inheritdoc}
64      */
65     protected function interact(InputInterface $input, OutputInterface $output)
66     {
67         $io = new DrupalStyle($input, $output);
68         $name = $input->getArgument('name');
69         $names = $this->configFactory->listAll();
70         if ($name) {
71             if (!in_array($name, $names)) {
72                 $io->warning(
73                     sprintf(
74                         $this->trans('commands.config.override.messages.invalid-name'),
75                         $name
76                     )
77                 );
78                 $name = null;
79             }
80         }
81         if (!$name) {
82             $name = $io->choiceNoList(
83                 $this->trans('commands.config.override.questions.name'),
84                 $names
85             );
86             $input->setArgument('name', $name);
87         }
88         $key = $input->getArgument('key');
89         if (!$key) {
90             if ($this->configStorage->exists($name)) {
91                 $configuration = $this->configStorage->read($name);
92             }
93             $key = $io->choiceNoList(
94                 $this->trans('commands.config.override.questions.key'),
95                 array_keys($configuration)
96             );
97             $input->setArgument('key', $key);
98         }
99         $value = $input->getArgument('value');
100         if (!$value) {
101             $value = $io->ask(
102                 $this->trans('commands.config.override.questions.value')
103             );
104             $input->setArgument('value', $value);
105         }
106     }
107     /**
108      * {@inheritdoc}
109      */
110     protected function execute(InputInterface $input, OutputInterface $output)
111     {
112         $io = new DrupalStyle($input, $output);
113
114         $configName = $input->getArgument('name');
115         $key = $input->getArgument('key');
116         $value = $input->getArgument('value');
117
118         $config = $this->configFactory->getEditable($configName);
119
120         $configurationOverrideResult = $this->overrideConfiguration($config, $key, $value);
121
122         $config->save();
123
124         $io->info($this->trans('commands.config.override.messages.configuration'), false);
125         $io->comment($configName);
126
127         $tableHeader = [
128             $this->trans('commands.config.override.messages.configuration-key'),
129             $this->trans('commands.config.override.messages.original'),
130             $this->trans('commands.config.override.messages.updated'),
131         ];
132         $tableRows = $configurationOverrideResult;
133         $io->table($tableHeader, $tableRows);
134
135         $config->save();
136     }
137
138
139     protected function overrideConfiguration($config, $key, $value)
140     {
141         $result[] = [
142             'configuration' => $key,
143             'original' => $config->get($key),
144             'updated' => $value,
145         ];
146         $config->set($key, $value);
147
148         return $result;
149     }
150 }