Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Shared / ExportTrait.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\Console\Command\Shared\ExportTrait.
6  */
7
8 namespace Drupal\Console\Command\Shared;
9
10 use Drupal\Component\Serialization\Yaml;
11 use Symfony\Component\Console\Exception\InvalidOptionException;
12
13 /**
14  * Class ConfigExportTrait
15  *
16  * @package Drupal\Console\Command
17  */
18 trait ExportTrait
19 {
20     /**
21      * @param $configName
22      * @param bool|false $uuid
23      * @return mixed
24      */
25     protected function getConfiguration($configName, $uuid = false, $hash = false, $collection = '')
26     {
27         $config = $this->configStorage->createCollection($collection)->read($configName);
28         // Exclude uuid base in parameter, useful to share configurations.
29         if ($uuid) {
30             unset($config['uuid']);
31         }
32
33         // Exclude default_config_hash inside _core is site-specific.
34         if ($hash) {
35             unset($config['_core']['default_config_hash']);
36
37             // Remove empty _core to match core's output.
38             if (empty($config['_core'])) {
39                 unset($config['_core']);
40             }
41         }
42
43         return $config;
44     }
45
46     /**
47      * @param string      $directory
48      * @param string      $message
49      */
50     protected function exportConfig($directory, $message)
51     {
52         $directory = realpath($directory);
53         $this->getIo()->info($message);
54
55         foreach ($this->configExport as $fileName => $config) {
56             $yamlConfig = Yaml::encode($config['data']);
57
58             $configFile = sprintf(
59                 '%s/%s.yml',
60                 $directory,
61                 $fileName
62             );
63
64             $this->getIo()->writeln('- ' . $configFile);
65
66             // Create directory if doesn't exist
67             if (!file_exists($directory)) {
68                 mkdir($directory, 0755, true);
69             }
70
71             file_put_contents(
72                 $configFile,
73                 $yamlConfig
74             );
75         }
76     }
77
78     /**
79      * @param string      $moduleName
80      * @param string      $message
81      */
82     protected function exportConfigToModule($moduleName, $message)
83     {
84         $this->getIo()->info($message);
85
86         $module = $this->extensionManager->getModule($moduleName);
87
88         if (empty($module)) {
89             throw new InvalidOptionException(sprintf('The module %s does not exist.', $moduleName));
90         }
91
92         foreach ($this->configExport as $fileName => $config) {
93             $yamlConfig = Yaml::encode($config['data']);
94
95             if ($config['optional']) {
96                 $configDirectory = $module->getConfigOptionalDirectory(false);
97             } else {
98                 $configDirectory = $module->getConfigInstallDirectory(false);
99             }
100
101             $configFile = sprintf(
102                 '%s/%s.yml',
103                 $configDirectory,
104                 $fileName
105             );
106
107             $this->getIo()->info('- ' . $configFile);
108
109             // Create directory if doesn't exist
110             if (!file_exists($configDirectory)) {
111                 mkdir($configDirectory, 0755, true);
112             }
113
114             file_put_contents(
115                 $configFile,
116                 $yamlConfig
117             );
118         }
119     }
120
121     protected function fetchDependencies($config, $type = 'config')
122     {
123         if (isset($config['dependencies'][$type])) {
124             return $config['dependencies'][$type];
125         }
126
127         return null;
128     }
129
130     protected function resolveDependencies($dependencies, $optional = false)
131     {
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);
137                 }
138             }
139         }
140     }
141
142     protected function exportModuleDependencies($module, $dependencies)
143     {
144         $module = $this->extensionManager->getModule($module);
145         $info_yaml = $module->info;
146
147         if (empty($info_yaml['dependencies'])) {
148             $info_yaml['dependencies'] = $dependencies;
149         } else {
150             $info_yaml['dependencies'] = array_unique(array_merge($info_yaml['dependencies'], $dependencies));
151         }
152
153         if (file_put_contents($module->getPathname(), Yaml::encode($info_yaml))) {
154             $this->getIo()->info(
155                 '[+] ' .
156                 sprintf(
157                     $this->trans('commands.config.export.view.messages.depencies-included'),
158                     $module->getPathname()
159                 )
160             );
161
162             foreach ($dependencies as $dependency) {
163                 $this->getIo()->info(
164                     '   [-] ' . $dependency
165                 );
166             }
167         } else {
168             $this->getIo()->error($this->trans('commands.site.mode.messages.error-writing-file') . ': ' . $this->getApplication()->getSite()->getModuleInfoFile($module));
169
170             return [];
171         }
172     }
173 }