More tidying.
[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 Drupal\Console\Core\Style\DrupalStyle;
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)
26     {
27         $config = $this->configStorage->read($configName);
28
29         // Exclude uuid base in parameter, useful to share configurations.
30         if ($uuid) {
31             unset($config['uuid']);
32         }
33         
34         // Exclude default_config_hash inside _core is site-specific.
35         if ($hash) {
36             unset($config['_core']['default_config_hash']);
37         }
38         
39         return $config;
40     }
41
42     /**
43      * @param string      $directory
44      * @param DrupalStyle $io
45      */
46     protected function exportConfig($directory, DrupalStyle $io, $message)
47     {
48         $io->info($message);
49
50         foreach ($this->configExport as $fileName => $config) {
51             $yamlConfig = Yaml::encode($config['data']);
52
53             $configFile = sprintf(
54                 '%s/%s.yml',
55                 $directory,
56                 $fileName
57             );
58
59             $io->info('- ' . $configFile);
60
61             // Create directory if doesn't exist
62             if (!file_exists($directory)) {
63                 mkdir($directory, 0755, true);
64             }
65
66             file_put_contents(
67                 $configFile,
68                 $yamlConfig
69             );
70         }
71     }
72
73     /**
74      * @param string      $module
75      * @param DrupalStyle $io
76      */
77     protected function exportConfigToModule($module, DrupalStyle $io, $message)
78     {
79         $io->info($message);
80
81         $module = $this->extensionManager->getModule($module);
82
83         foreach ($this->configExport as $fileName => $config) {
84             $yamlConfig = Yaml::encode($config['data']);
85
86             if ($config['optional']) {
87                 $configDirectory = $module->getConfigOptionalDirectory(false);
88             } else {
89                 $configDirectory = $module->getConfigInstallDirectory(false);
90             }
91
92             $configFile = sprintf(
93                 '%s/%s.yml',
94                 $configDirectory,
95                 $fileName
96             );
97
98             $io->info('- ' . $configFile);
99
100             // Create directory if doesn't exist
101             if (!file_exists($configDirectory)) {
102                 mkdir($configDirectory, 0755, true);
103             }
104
105             file_put_contents(
106                 $configFile,
107                 $yamlConfig
108             );
109         }
110     }
111
112     protected function fetchDependencies($config, $type = 'config')
113     {
114         if (isset($config['dependencies'][$type])) {
115             return $config['dependencies'][$type];
116         }
117
118         return null;
119     }
120
121     protected function resolveDependencies($dependencies, $optional = false)
122     {
123         foreach ($dependencies as $dependency) {
124             if (!array_key_exists($dependency, $this->configExport)) {
125                 $this->configExport[$dependency] = ['data' => $this->getConfiguration($dependency), 'optional' => $optional];
126                 if ($dependencies = $this->fetchDependencies($this->configExport[$dependency], 'config')) {
127                     $this->resolveDependencies($dependencies, $optional);
128                 }
129             }
130         }
131     }
132
133     protected function exportModuleDependencies($io, $module, $dependencies)
134     {
135         $module = $this->extensionManager->getModule($module);
136         $info_yaml = $module->info;
137
138         if (empty($info_yaml['dependencies'])) {
139             $info_yaml['dependencies'] = $dependencies;
140         } else {
141             $info_yaml['dependencies'] = array_unique(array_merge($info_yaml['dependencies'], $dependencies));
142         }
143
144         if (file_put_contents($module->getPathname(), Yaml::encode($info_yaml))) {
145             $io->info(
146                 '[+] ' .
147                 sprintf(
148                     $this->trans('commands.config.export.view.messages.depencies-included'),
149                     $module->getPathname()
150                 )
151             );
152
153             foreach ($dependencies as $dependency) {
154                 $io->info(
155                     '   [-] ' . $dependency
156                 );
157             }
158         } else {
159             $io->error($this->trans('commands.site.mode.messages.error-writing-file') . ': ' . $this->getApplication()->getSite()->getModuleInfoFile($module));
160
161             return [];
162         }
163     }
164 }