forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigEditCommand.php
More file actions
110 lines (99 loc) · 3.53 KB
/
Copy pathConfigEditCommand.php
File metadata and controls
110 lines (99 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
* @file
* Contains \Drupal\AppConsole\Command\ConfigEditCommand.
*/
namespace Drupal\AppConsole\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\ProcessBuilder;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Drupal\Component\Serialization\Yaml;
class ConfigEditCommand extends ContainerAwareCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('config:edit')
->setDescription($this->trans('commands.config.edit.description'))
->addArgument('config-name', InputArgument::REQUIRED,
$this->trans('commands.config.edit.arguments.config-name'))
->addArgument('editor', InputArgument::OPTIONAL,
$this->trans('commands.config.edit.arguments.editor'));
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$configName = $input->getArgument('config-name');
$editor = $input->getArgument('editor');
$config = $this->getConfigFactory()->getEditable($configName);
$configSystem = $this->getConfigFactory()->get('system.file');
$temporalyDirectory = $configSystem->get('path.temporary') ?: '/tmp';
$configFile = $temporalyDirectory . '/config-edit/' . $configName . '.yml';
$ymlFile = new Parser();
$fileSystem = new Filesystem();
try {
$fileSystem->mkdir($temporalyDirectory);
$fileSystem->dumpFile($configFile, $this->getYamlConfig($configName));
} catch (IOExceptionInterface $e) {
throw new \Exception($this->trans('commands.config.edit.messages.no-directory') . " " . $e->getPath());
}
if (!$editor) {
$editor = $this->getEditor();
}
$processBuilder = new ProcessBuilder(array($editor, $configFile));
$process = $processBuilder->getProcess();
$process->setTty('true');
$process->run();
if ($process->isSuccessful()) {
$value = $ymlFile->parse(file_get_contents($configFile));
$config->setData($value);
$config->save();
$fileSystem->remove($configFile);
}
if (!$process->isSuccessful()) {
throw new \RuntimeException($process->getErrorOutput());
}
}
/**
* @param $config_name String
*
* @return array
*/
protected function getYamlConfig($config_name)
{
$configStorage = $this->getConfigStorage();
if ($configStorage->exists($config_name)) {
$configuration = $configStorage->read($config_name);
$configurationEncoded = Yaml::encode($configuration);
}
return $configurationEncoded;
}
/**
* @return string
*/
protected function getEditor()
{
$app = $this->getApplication();
$config = $app->getConfig();
$editor = $config->get('application.editor', 'vi');
if ($editor != '') {
return trim($editor);
}
$processBuilder = new ProcessBuilder(array('bash'));
$process = $processBuilder->getProcess();
$process->setCommandLine('echo ${EDITOR:-${VISUAL:-vi}}');
$process->run();
$editor = $process->getOutput();
$process->stop();
return trim($editor);
}
}