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 Drupal\Console\Core\Command\Command;
21 use Drupal\Console\Core\Utils\ConfigurationManager;
23 class EditCommand extends Command
28 protected $configFactory;
33 protected $configStorage;
36 * @var ConfigurationManager
38 protected $configurationManager;
41 * EditCommand constructor.
43 * @param ConfigFactory $configFactory
44 * @param CachedStorage $configStorage
45 * @param ConfigurationManager $configurationManager
47 public function __construct(
48 ConfigFactory $configFactory,
49 CachedStorage $configStorage,
50 ConfigurationManager $configurationManager
52 $this->configFactory = $configFactory;
53 $this->configStorage = $configStorage;
54 $this->configurationManager = $configurationManager;
55 parent::__construct();
60 protected function configure()
63 ->setName('config:edit')
64 ->setDescription($this->trans('commands.config.edit.description'))
67 InputArgument::REQUIRED,
68 $this->trans('commands.config.edit.arguments.config-name')
72 InputArgument::OPTIONAL,
73 $this->trans('commands.config.edit.arguments.editor')
75 ->setAliases(['ced']);
81 protected function execute(InputInterface $input, OutputInterface $output)
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();
93 $this->getIo()->error($this->trans('commands.config.edit.messages.no-config'));
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());
107 $editor = $this->getEditor();
109 $processBuilder = new ProcessBuilder([$editor, $configFile]);
110 $process = $processBuilder->getProcess();
111 $process->setTty('true');
114 if ($process->isSuccessful()) {
115 $value = $ymlFile->parse(file_get_contents($configFile));
116 $config->setData($value);
118 $fileSystem->remove($configFile);
121 if (!$process->isSuccessful()) {
122 $this->getIo()->error($process->getErrorOutput());
129 protected function interact(InputInterface $input, OutputInterface $output)
131 $configName = $input->getArgument('config-name');
133 $configNames = $this->configFactory->listAll();
134 $configName = $this->getIo()->choice(
135 $this->trans('commands.config.edit.messages.choose-configuration'),
139 $input->setArgument('config-name', $configName);
144 * @param $config_name String
148 protected function getYamlConfig($config_name)
150 if ($this->configStorage->exists($config_name)) {
151 $configuration = $this->configStorage->read($config_name);
152 $configurationEncoded = Yaml::encode($configuration);
155 return $configurationEncoded;
161 protected function getEditor()
163 $config = $this->configurationManager->getConfiguration();
164 $editor = $config->get('application.editor', '');
167 return trim($editor);
170 $processBuilder = new ProcessBuilder(['bash']);
171 $process = $processBuilder->getProcess();
172 $process->setCommandLine('echo ${EDITOR:-${VISUAL:-vi}}');
174 $editor = $process->getOutput();
177 return trim($editor);