5 * Contains Drupal\Console\Command\Shared\ExportTrait.
8 namespace Drupal\Console\Command\Shared;
10 use Drupal\Component\Serialization\Yaml;
11 use Symfony\Component\Console\Exception\InvalidOptionException;
14 * Class ConfigExportTrait
16 * @package Drupal\Console\Command
22 * @param bool|false $uuid
25 protected function getConfiguration($configName, $uuid = false, $hash = false, $collection = '')
27 $config = $this->configStorage->createCollection($collection)->read($configName);
28 // Exclude uuid base in parameter, useful to share configurations.
30 unset($config['uuid']);
33 // Exclude default_config_hash inside _core is site-specific.
35 unset($config['_core']['default_config_hash']);
37 // Remove empty _core to match core's output.
38 if (empty($config['_core'])) {
39 unset($config['_core']);
47 * @param string $directory
48 * @param string $message
50 protected function exportConfig($directory, $message)
52 $directory = realpath($directory);
53 $this->getIo()->info($message);
55 foreach ($this->configExport as $fileName => $config) {
56 $yamlConfig = Yaml::encode($config['data']);
58 $configFile = sprintf(
64 $this->getIo()->writeln('- ' . $configFile);
66 // Create directory if doesn't exist
67 if (!file_exists($directory)) {
68 mkdir($directory, 0755, true);
79 * @param string $moduleName
80 * @param string $message
82 protected function exportConfigToModule($moduleName, $message)
84 $this->getIo()->info($message);
86 $module = $this->extensionManager->getModule($moduleName);
89 throw new InvalidOptionException(sprintf('The module %s does not exist.', $moduleName));
92 foreach ($this->configExport as $fileName => $config) {
93 $yamlConfig = Yaml::encode($config['data']);
95 if ($config['optional']) {
96 $configDirectory = $module->getConfigOptionalDirectory(false);
98 $configDirectory = $module->getConfigInstallDirectory(false);
101 $configFile = sprintf(
107 $this->getIo()->info('- ' . $configFile);
109 // Create directory if doesn't exist
110 if (!file_exists($configDirectory)) {
111 mkdir($configDirectory, 0755, true);
121 protected function fetchDependencies($config, $type = 'config')
123 if (isset($config['dependencies'][$type])) {
124 return $config['dependencies'][$type];
130 protected function resolveDependencies($dependencies, $optional = false)
132 foreach ($dependencies as $dependency) {
133 if (!array_key_exists($dependency, $this->configExport)) {
134 $this->configExport[$dependency] = ['data' => $this->getConfiguration($dependency), 'optional' => $optional];
135 if ($dependencies = $this->fetchDependencies($this->configExport[$dependency], 'config')) {
136 $this->resolveDependencies($dependencies, $optional);
142 protected function exportModuleDependencies($module, $dependencies)
144 $module = $this->extensionManager->getModule($module);
145 $info_yaml = $module->info;
147 if (empty($info_yaml['dependencies'])) {
148 $info_yaml['dependencies'] = $dependencies;
150 $info_yaml['dependencies'] = array_unique(array_merge($info_yaml['dependencies'], $dependencies));
153 if (file_put_contents($module->getPathname(), Yaml::encode($info_yaml))) {
154 $this->getIo()->info(
157 $this->trans('commands.config.export.view.messages.depencies-included'),
158 $module->getPathname()
162 foreach ($dependencies as $dependency) {
163 $this->getIo()->info(
164 ' [-] ' . $dependency
168 $this->getIo()->error($this->trans('commands.site.mode.messages.error-writing-file') . ': ' . $this->getApplication()->getSite()->getModuleInfoFile($module));