Yaffs site version 1.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 1;
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 1;
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
126         if (!$process->isSuccessful()) {
127             $io->error($process->getErrorOutput());
128             return 1;
129         }
130
131         return 0;
132     }
133
134     protected function interact(InputInterface $input, OutputInterface $output)
135     {
136         $io = new DrupalStyle($input, $output);
137
138         $configName = $input->getArgument('config-name');
139         if (!$configName) {
140             $configNames = $this->configFactory->listAll();
141             $configName = $io->choice(
142                 'Choose a configuration',
143                 $configNames
144             );
145
146             $input->setArgument('config-name', $configName);
147         }
148     }
149
150     /**
151      * @param $config_name String
152      *
153      * @return array
154      */
155     protected function getYamlConfig($config_name)
156     {
157         if ($this->configStorage->exists($config_name)) {
158             $configuration = $this->configStorage->read($config_name);
159             $configurationEncoded = Yaml::encode($configuration);
160         }
161
162         return $configurationEncoded;
163     }
164
165     /**
166      * @return string
167      */
168     protected function getEditor()
169     {
170         $config = $this->configurationManager->getConfiguration();
171         $editor = $config->get('application.editor', 'vi');
172
173         if ($editor != '') {
174             return trim($editor);
175         }
176
177         $processBuilder = new ProcessBuilder(['bash']);
178         $process = $processBuilder->getProcess();
179         $process->setCommandLine('echo ${EDITOR:-${VISUAL:-vi}}');
180         $process->run();
181         $editor = $process->getOutput();
182         $process->stop();
183
184         return trim($editor);
185     }
186 }