Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console / src / Command / Config / ExportSingleCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Config\ExportSingleCommand.
6  */
7
8 namespace Drupal\Console\Command\Config;
9
10 use Drupal\Component\Serialization\Yaml;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Input\InputOption;
14 use Symfony\Component\Console\Output\OutputInterface;
15 use Drupal\Console\Core\Command\Command;
16 use Drupal\Console\Utils\Validator;
17 use Drupal\Core\Entity\EntityTypeManagerInterface;
18 use Drupal\Core\Config\CachedStorage;
19 use Drupal\Console\Command\Shared\ExportTrait;
20 use Drupal\Console\Command\Shared\ModuleTrait;
21 use Drupal\Console\Extension\Manager;
22 use Drupal\Core\Language\LanguageManagerInterface;
23 use Webmozart\PathUtil\Path;
24
25 class ExportSingleCommand extends Command
26 {
27     use ModuleTrait;
28     use ExportTrait;
29
30     /**
31      * @var []
32      */
33     protected $definitions;
34
35     /**
36      * @var EntityTypeManagerInterface
37      */
38     protected $entityTypeManager;
39
40     /**
41      * @var CachedStorage
42      */
43     protected $configStorage;
44
45     /**
46      * @var Manager
47      */
48     protected $extensionManager;
49
50     /**
51      * @var Configuration.
52      */
53     protected $configExport;
54
55     /**
56      * @var LanguageManagerInterface
57      */
58     protected $languageManager;
59
60     /**
61      * @var Validator
62      */
63     protected $validator;
64
65     /**
66      * ExportSingleCommand constructor.
67      *
68      * @param EntityTypeManagerInterface $entityTypeManager
69      * @param CachedStorage              $configStorage
70      * @param Manager                    $extensionManager
71      * @param languageManager            $languageManager
72      * @param Validator                  $validator
73      */
74     public function __construct(
75         EntityTypeManagerInterface $entityTypeManager,
76         CachedStorage $configStorage,
77         Manager $extensionManager,
78         LanguageManagerInterface $languageManager,
79         Validator $validator
80     ) {
81         $this->entityTypeManager = $entityTypeManager;
82         $this->configStorage = $configStorage;
83         $this->extensionManager = $extensionManager;
84         $this->languageManager = $languageManager;
85         $this->validator = $validator;
86         parent::__construct();
87     }
88
89     /**
90      * {@inheritdoc}
91      */
92     protected function configure()
93     {
94         $this
95             ->setName('config:export:single')
96             ->setDescription($this->trans('commands.config.export.single.description'))
97             ->addOption(
98                 'name',
99                 null,
100                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
101                 $this->trans('commands.config.export.single.options.name')
102             )->addOption(
103                 'directory',
104                 null,
105                 InputOption::VALUE_OPTIONAL,
106                 $this->trans('commands.config.export.arguments.directory')
107             )->addOption(
108                 'module',
109                 null,
110                 InputOption::VALUE_OPTIONAL,
111                 $this->trans('commands.common.options.module')
112             )->addOption(
113                 'include-dependencies',
114                 null,
115                 InputOption::VALUE_NONE,
116                 $this->trans('commands.config.export.single.options.include-dependencies')
117             )->addOption(
118                 'optional',
119                 null,
120                 InputOption::VALUE_NONE,
121                 $this->trans('commands.config.export.single.options.optional')
122             )->addOption(
123                 'remove-uuid',
124                 null,
125                 InputOption::VALUE_NONE,
126                 $this->trans('commands.config.export.single.options.remove-uuid')
127             )->addOption(
128                 'remove-config-hash',
129                 null,
130                 InputOption::VALUE_NONE,
131                 $this->trans('commands.config.export.single.options.remove-config-hash')
132             )
133             ->setAliases(['ces']);
134     }
135
136     /*
137      * Return config types
138      */
139     protected function getConfigTypes()
140     {
141         foreach ($this->entityTypeManager->getDefinitions() as $entity_type => $definition) {
142             if ($definition->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
143                 $this->definitions[$entity_type] = $definition;
144             }
145         }
146         $entity_types = array_map(
147             function ($definition) {
148                 return $definition->getLabel();
149             }, $this->definitions
150         );
151
152         uasort($entity_types, 'strnatcasecmp');
153         $config_types = [
154                 'system.simple' => $this->trans('commands.config.export.single.options.simple-configuration'),
155             ] + $entity_types;
156
157         return $config_types;
158     }
159
160     /*
161      * Return config types
162      */
163     protected function getConfigNames($config_type)
164     {
165         $names = [];
166         // For a given entity type, load all entities.
167         if ($config_type && $config_type !== 'system.simple') {
168             $entity_storage = $this->entityTypeManager->getStorage($config_type);
169             foreach ($entity_storage->loadMultiple() as $entity) {
170                 $entity_id = $entity->id();
171                 $label = $entity->label() ?: $entity_id;
172                 $names[$entity_id] = $label;
173             }
174         }
175         // Handle simple configuration.
176         else {
177             // Gather the config entity prefixes.
178             $config_prefixes = array_map(
179                 function ($definition) {
180                     return $definition->getConfigPrefix() . '.';
181                 }, $this->definitions
182             );
183
184             // Find all config, and then filter our anything matching a config prefix.
185             $names = $this->configStorage->listAll();
186             $names = array_combine($names, $names);
187             foreach ($names as $config_name) {
188                 foreach ($config_prefixes as $config_prefix) {
189                     if (strpos($config_name, $config_prefix) === 0) {
190                         unset($names[$config_name]);
191                     }
192                 }
193             }
194         }
195
196         return $names;
197     }
198
199     /**
200      * {@inheritdoc}
201      */
202     protected function interact(InputInterface $input, OutputInterface $output)
203     {
204         $config_types = $this->getConfigTypes();
205
206         $name = $input->getOption('name');
207         if (!$name) {
208             $type = $this->getIo()->choiceNoList(
209                 $this->trans('commands.config.export.single.questions.config-type'),
210                 array_keys($config_types),
211                 'system.simple'
212             );
213             $names = $this->getConfigNames($type);
214
215             $name = $this->getIo()->choiceNoList(
216                 $this->trans('commands.config.export.single.questions.name'),
217                 array_keys($names)
218             );
219
220             if ($type !== 'system.simple') {
221                 $definition = $this->entityTypeManager->getDefinition($type);
222                 $name = $definition->getConfigPrefix() . '.' . $name;
223             }
224
225             $input->setOption('name', [$name]);
226         }
227
228         // --module option
229         $module = $this->getModuleOption();
230         if ($module) {
231             $optionalConfig = $input->getOption('optional');
232             if (!$optionalConfig) {
233                 $optionalConfig = $this->getIo()->confirm(
234                     $this->trans('commands.config.export.single.questions.optional'),
235                     true
236                 );
237                 $input->setOption('optional', $optionalConfig);
238             }
239         }
240
241         if (!$input->getOption('remove-uuid')) {
242             $removeUuid = $this->getIo()->confirm(
243                 $this->trans('commands.config.export.single.questions.remove-uuid'),
244                 true
245             );
246             $input->setOption('remove-uuid', $removeUuid);
247         }
248         if (!$input->getOption('remove-config-hash')) {
249             $removeHash = $this->getIo()->confirm(
250                 $this->trans('commands.config.export.single.questions.remove-config-hash'),
251                 true
252             );
253             $input->setOption('remove-config-hash', $removeHash);
254         }
255     }
256
257     /**
258      * {@inheritdoc}
259      */
260     protected function execute(InputInterface $input, OutputInterface $output)
261     {
262         $directory = $input->getOption('directory');
263         $module = $input->getOption('module');
264         $name = $input->getOption('name');
265         $optional = $input->getOption('optional');
266         $removeUuid = $input->getOption('remove-uuid');
267         $removeHash = $input->getOption('remove-config-hash');
268         $includeDependencies = $input->getOption('include-dependencies');
269
270         foreach ($this->getLanguage() as $value) {
271             foreach ($name as $nameItem) {
272                 $config = $this->getConfiguration(
273                     $nameItem,
274                     $removeUuid,
275                     $removeHash,
276                     $value
277                 );
278
279                 if ($config) {
280                     $this->configExport[$nameItem] = [
281                         'data' => $config,
282                         'optional' => $optional
283                     ];
284
285                     if ($includeDependencies) {
286                         // Include config dependencies in export files
287                         if ($dependencies = $this->fetchDependencies($config, 'config')) {
288                             $this->resolveDependencies($dependencies, $optional);
289                         }
290                     }
291                 } else {
292                     $this->getIo()->error($this->trans('commands.config.export.single.messages.config-not-found'));
293                 }
294             }
295
296             if ($module) {
297                 $this->exportConfigToModule(
298                     $module,
299                     $this->trans(
300                         'commands.config.export.single.messages.config-exported'
301                     )
302                 );
303
304                 return 0;
305             }
306
307             if (!is_dir($directory)) {
308                 $directory = $directory_copy = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
309                 if ($value) {
310                     $directory = $directory_copy .'/' . str_replace('.', '/', $value);
311                 }
312             } else {
313                 $directory = $directory_copy .'/' . str_replace('.', '/', $value);
314                 $directory = Path::canonicalize($directory);
315                 if (!file_exists($directory)) {
316                     mkdir($directory, 0755, true);
317                 }
318             }
319
320             $this->exportConfig(
321                 $directory,
322                 $this->trans('commands.config.export.single.messages.config-exported')
323             );
324         }
325
326         return 0;
327     }
328
329     /**
330      * Get the languague enable.
331      */
332     protected function getLanguage()
333     {
334         $output = [];
335         // Get the language that be for default.
336         $default_id = $this->languageManager->getDefaultLanguage()->getId();
337         foreach ($this->languageManager->getLanguages() as $key => $value) {
338             if ($default_id == $key) {
339                 $output[] = '';
340             } else {
341                 $output[] = 'language.' . $value->getId();
342             }
343         }
344         return $output;
345     }
346 }