5 * Contains \Drupal\Console\Command\Config\EditCommand.
8 namespace Drupal\Console\Command\Config;
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;
25 class EditCommand extends Command
32 protected $configFactory;
37 protected $configStorage;
40 * @var ConfigurationManager
42 protected $configurationManager;
45 * EditCommand constructor.
47 * @param ConfigFactory $configFactory
48 * @param CachedStorage $configStorage
49 * @param ConfigurationManager $configurationManager
51 public function __construct(
52 ConfigFactory $configFactory,
53 CachedStorage $configStorage,
54 ConfigurationManager $configurationManager
56 $this->configFactory = $configFactory;
57 $this->configStorage = $configStorage;
58 $this->configurationManager = $configurationManager;
59 parent::__construct();
64 protected function configure()
67 ->setName('config:edit')
68 ->setDescription($this->trans('commands.config.edit.description'))
71 InputArgument::REQUIRED,
72 $this->trans('commands.config.edit.arguments.config-name')
76 InputArgument::OPTIONAL,
77 $this->trans('commands.config.edit.arguments.editor')
84 protected function execute(InputInterface $input, OutputInterface $output)
86 $io = new DrupalStyle($input, $output);
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();
98 $io->error($this->trans('commands.config.edit.messages.no-config'));
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());
112 $editor = $this->getEditor();
114 $processBuilder = new ProcessBuilder([$editor, $configFile]);
115 $process = $processBuilder->getProcess();
116 $process->setTty('true');
119 if ($process->isSuccessful()) {
120 $value = $ymlFile->parse(file_get_contents($configFile));
121 $config->setData($value);
123 $fileSystem->remove($configFile);
126 if (!$process->isSuccessful()) {
127 $io->error($process->getErrorOutput());
134 protected function interact(InputInterface $input, OutputInterface $output)
136 $io = new DrupalStyle($input, $output);
138 $configName = $input->getArgument('config-name');
140 $configNames = $this->configFactory->listAll();
141 $configName = $io->choice(
142 'Choose a configuration',
146 $input->setArgument('config-name', $configName);
151 * @param $config_name String
155 protected function getYamlConfig($config_name)
157 if ($this->configStorage->exists($config_name)) {
158 $configuration = $this->configStorage->read($config_name);
159 $configurationEncoded = Yaml::encode($configuration);
162 return $configurationEncoded;
168 protected function getEditor()
170 $config = $this->configurationManager->getConfiguration();
171 $editor = $config->get('application.editor', 'vi');
174 return trim($editor);
177 $processBuilder = new ProcessBuilder(['bash']);
178 $process = $processBuilder->getProcess();
179 $process->setCommandLine('echo ${EDITOR:-${VISUAL:-vi}}');
181 $editor = $process->getOutput();
184 return trim($editor);