Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Config / EditCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Config\EditCommand.
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\Filesystem\Filesystem;
14 use Symfony\Component\Process\ProcessBuilder;
15 use Symfony\Component\Yaml\Parser;
16 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
17 use Drupal\Component\Serialization\Yaml;
18 use Drupal\Core\Config\CachedStorage;
19 use Drupal\Core\Config\ConfigFactory;
20 use Drupal\Console\Core\Command\Command;
21 use Drupal\Console\Core\Utils\ConfigurationManager;
22
23 class EditCommand extends Command
24 {
25     /**
26      * @var ConfigFactory
27      */
28     protected $configFactory;
29
30     /**
31      * @var CachedStorage
32      */
33     protected $configStorage;
34
35     /**
36      * @var ConfigurationManager
37      */
38     protected $configurationManager;
39
40     /**
41      * EditCommand constructor.
42      *
43      * @param ConfigFactory        $configFactory
44      * @param CachedStorage        $configStorage
45      * @param ConfigurationManager $configurationManager
46      */
47     public function __construct(
48         ConfigFactory $configFactory,
49         CachedStorage $configStorage,
50         ConfigurationManager $configurationManager
51     ) {
52         $this->configFactory = $configFactory;
53         $this->configStorage = $configStorage;
54         $this->configurationManager = $configurationManager;
55         parent::__construct();
56     }
57     /**
58      * {@inheritdoc}
59      */
60     protected function configure()
61     {
62         $this
63             ->setName('config:edit')
64             ->setDescription($this->trans('commands.config.edit.description'))
65             ->addArgument(
66                 'config-name',
67                 InputArgument::REQUIRED,
68                 $this->trans('commands.config.edit.arguments.config-name')
69             )
70             ->addArgument(
71                 'editor',
72                 InputArgument::OPTIONAL,
73                 $this->trans('commands.config.edit.arguments.editor')
74             )
75             ->setAliases(['ced']);
76     }
77
78     /**
79      * {@inheritdoc}
80      */
81     protected function execute(InputInterface $input, OutputInterface $output)
82     {
83         $configName = $input->getArgument('config-name');
84         $editor = $input->getArgument('editor');
85         $config = $this->configFactory->getEditable($configName);
86         $configSystem = $this->configFactory->get('system.file');
87         $temporaryDirectory = $configSystem->get('path.temporary') ?: '/tmp';
88         $configFile = $temporaryDirectory.'/config-edit/'.$configName.'.yml';
89         $ymlFile = new Parser();
90         $fileSystem = new Filesystem();
91
92         if (!$configName) {
93             $this->getIo()->error($this->trans('commands.config.edit.messages.no-config'));
94
95             return 1;
96         }
97
98         try {
99             $fileSystem->mkdir($temporaryDirectory);
100             $fileSystem->dumpFile($configFile, $this->getYamlConfig($configName));
101         } catch (IOExceptionInterface $e) {
102             $this->getIo()->error($this->trans('commands.config.edit.messages.no-directory').' '.$e->getPath());
103
104             return 1;
105         }
106         if (!$editor) {
107             $editor = $this->getEditor();
108         }
109         $processBuilder = new ProcessBuilder([$editor, $configFile]);
110         $process = $processBuilder->getProcess();
111         $process->setTty('true');
112         $process->run();
113
114         if ($process->isSuccessful()) {
115             $value = $ymlFile->parse(file_get_contents($configFile));
116             $config->setData($value);
117             $config->save();
118             $fileSystem->remove($configFile);
119         }
120
121         if (!$process->isSuccessful()) {
122             $this->getIo()->error($process->getErrorOutput());
123             return 1;
124         }
125
126         return 0;
127     }
128
129     protected function interact(InputInterface $input, OutputInterface $output)
130     {
131         $configName = $input->getArgument('config-name');
132         if (!$configName) {
133             $configNames = $this->configFactory->listAll();
134             $configName = $this->getIo()->choice(
135                 $this->trans('commands.config.edit.messages.choose-configuration'),
136                 $configNames
137             );
138
139             $input->setArgument('config-name', $configName);
140         }
141     }
142
143     /**
144      * @param $config_name String
145      *
146      * @return array
147      */
148     protected function getYamlConfig($config_name)
149     {
150         if ($this->configStorage->exists($config_name)) {
151             $configuration = $this->configStorage->read($config_name);
152             $configurationEncoded = Yaml::encode($configuration);
153         }
154
155         return $configurationEncoded;
156     }
157
158     /**
159      * @return string
160      */
161     protected function getEditor()
162     {
163         $config = $this->configurationManager->getConfiguration();
164         $editor = $config->get('application.editor', '');
165
166         if ($editor != '') {
167             return trim($editor);
168         }
169
170         $processBuilder = new ProcessBuilder(['bash']);
171         $process = $processBuilder->getProcess();
172         $process->setCommandLine('echo ${EDITOR:-${VISUAL:-vi}}');
173         $process->run();
174         $editor = $process->getOutput();
175         $process->stop();
176
177         return trim($editor);
178     }
179 }