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