Version 1
[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 Symfony\Component\Console\Command\Command;
16 use Drupal\Core\Entity\EntityTypeManagerInterface;
17 use Drupal\Core\Config\CachedStorage;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Core\Command\Shared\CommandTrait;
20 use Drupal\Console\Command\Shared\ExportTrait;
21
22 class ExportSingleCommand extends Command
23 {
24     use CommandTrait;
25     use ExportTrait;
26
27     /**
28      * @var []
29      */
30     protected $definitions;
31
32     /**
33      * @var EntityTypeManagerInterface
34      */
35     protected $entityTypeManager;
36
37     /**
38      * @var CachedStorage
39      */
40     protected $configStorage;
41
42     protected $configExport;
43
44     /**
45      * ExportSingleCommand constructor.
46      *
47      * @param EntityTypeManagerInterface $entityTypeManager
48      * @param CachedStorage              $configStorage
49      */
50     public function __construct(
51         EntityTypeManagerInterface $entityTypeManager,
52         CachedStorage $configStorage
53     ) {
54         $this->entityTypeManager = $entityTypeManager;
55         $this->configStorage = $configStorage;
56         parent::__construct();
57     }
58
59     /**
60      * {@inheritdoc}
61      */
62     protected function configure()
63     {
64         $this
65             ->setName('config:export:single')
66             ->setDescription($this->trans('commands.config.export.single.description'))
67             ->addOption(
68                 'name',
69                 '',
70                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
71                 $this->trans('commands.config.export.single.options.name')
72             )->addOption(
73                 'directory',
74                 '',
75                 InputOption::VALUE_OPTIONAL,
76                 $this->trans('commands.config.export.arguments.directory')
77             )->addOption(
78                 'module',
79                 '',
80                 InputOption::VALUE_OPTIONAL,
81                 $this->trans('commands.common.options.module')
82             )->addOption(
83                 'include-dependencies',
84                 '',
85                 InputOption::VALUE_NONE,
86                 $this->trans('commands.config.export.single.options.include-dependencies')
87             )->addOption(
88                 'optional',
89                 '',
90                 InputOption::VALUE_NONE,
91                 $this->trans('commands.config.export.single.options.optional')
92             )->addOption(
93                 'remove-uuid',
94                 '',
95                 InputOption::VALUE_NONE,
96                 $this->trans('commands.config.export.single.options.remove-uuid')
97             )->addOption(
98                 'remove-config-hash',
99                 '',
100                 InputOption::VALUE_NONE,
101                 $this->trans('commands.config.export.single.options.remove-config-hash')
102             );
103     }
104
105     /*
106      * Return config types
107      */
108     protected function getConfigTypes()
109     {
110         foreach ($this->entityTypeManager->getDefinitions() as $entity_type => $definition) {
111             if ($definition->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
112                 $this->definitions[$entity_type] = $definition;
113             }
114         }
115         $entity_types = array_map(
116             function ($definition) {
117                 return $definition->getLabel();
118             }, $this->definitions
119         );
120
121         uasort($entity_types, 'strnatcasecmp');
122         $config_types = [
123             'system.simple' => $this->trans('commands.config.export.single.options.simple-configuration'),
124           ] + $entity_types;
125
126         return $config_types;
127     }
128
129     /*
130      * Return config types
131      */
132     protected function getConfigNames($config_type)
133     {
134         $names = [];
135         // For a given entity type, load all entities.
136         if ($config_type && $config_type !== 'system.simple') {
137             $entity_storage = $this->entityTypeManager->getStorage($config_type);
138             foreach ($entity_storage->loadMultiple() as $entity) {
139                 $entity_id = $entity->id();
140                 $label = $entity->label() ?: $entity_id;
141                 $names[$entity_id] = $label;
142             }
143         }
144         // Handle simple configuration.
145         else {
146             // Gather the config entity prefixes.
147             $config_prefixes = array_map(
148                 function ($definition) {
149                     return $definition->getConfigPrefix() . '.';
150                 }, $this->definitions
151             );
152
153             // Find all config, and then filter our anything matching a config prefix.
154             $names = $this->configStorage->listAll();
155             $names = array_combine($names, $names);
156             foreach ($names as $config_name) {
157                 foreach ($config_prefixes as $config_prefix) {
158                     if (strpos($config_name, $config_prefix) === 0) {
159                         unset($names[$config_name]);
160                     }
161                 }
162             }
163         }
164
165         return $names;
166     }
167
168     /**
169      * {@inheritdoc}
170      */
171     protected function interact(InputInterface $input, OutputInterface $output)
172     {
173         $io = new DrupalStyle($input, $output);
174
175         $config_types = $this->getConfigTypes();
176
177         $name = $input->getOption('name');
178         if (!$name) {
179             $type = $io->choiceNoList(
180                 $this->trans('commands.config.export.single.questions.config-type'),
181                 array_keys($config_types),
182                 'system.simple'
183             );
184             $names = $this->getConfigNames($type);
185
186             $name = $io->choiceNoList(
187                 $this->trans('commands.config.export.single.questions.name'),
188                 array_keys($names)
189             );
190
191             if ($type !== 'system.simple') {
192                 $definition = $this->entityTypeManager->getDefinition($type);
193                 $name = $definition->getConfigPrefix() . '.' . $name;
194             }
195             $input->setOption('name', $name);
196         }
197
198         $module = $input->getOption('module');
199         if ($module) {
200             $optionalConfig = $input->getOption('optional');
201             if (!$optionalConfig) {
202                 $optionalConfig = $io->confirm(
203                     $this->trans('commands.config.export.single.questions.optional'),
204                     true
205                 );
206                 $input->setOption('optional', $optionalConfig);
207             }
208         }
209
210         if (!$input->getOption('remove-uuid')) {
211             $removeUuid = $io->confirm(
212                 $this->trans('commands.config.export.single.questions.remove-uuid'),
213                 true
214             );
215             $input->setOption('remove-uuid', $removeUuid);
216         }
217         if (!$input->getOption('remove-config-hash')) {
218             $removeHash = $io->confirm(
219                 $this->trans('commands.config.export.single.questions.remove-config-hash'),
220                 true
221             );
222             $input->setOption('remove-config-hash', $removeHash);
223         }
224     }
225
226
227     /**
228      * {@inheritdoc}
229      */
230     protected function execute(InputInterface $input, OutputInterface $output)
231     {
232         $io = new DrupalStyle($input, $output);
233
234         $directory = $input->getOption('directory');
235         $module = $input->getOption('module');
236         $ame = $input->getOption('name');
237         $optional = $input->getOption('optional');
238         $removeUuid = $input->getOption('remove-uuid');
239         $removeHash = $input->getOption('remove-config-hash');
240
241         foreach ($ame as $nameItem) {
242             $config = $this->getConfiguration(
243                 $nameItem,
244                 $removeUuid,
245                 $removeHash
246             );
247             
248             if ($config) {
249                 $this->configExport[$nameItem] = [
250                     'data' => $config,
251                     'optional' => $optional
252                 ];
253
254                 if ($input->getOption('include-dependencies')) {
255                     // Include config dependencies in export files
256                     if ($dependencies = $this->fetchDependencies($config, 'config')) {
257                         $this->resolveDependencies($dependencies, $optional);
258                     }
259                 }
260             } else {
261                 $io->error($this->trans('commands.config.export.single.messages.config-not-found'));
262             }
263         }
264
265         if ($module) {
266             $this->exportConfigToModule(
267                 $module,
268                 $io,
269                 $this->trans(
270                     'commands.config.export.single.messages.config-exported'
271                 )
272             );
273
274             return 0;
275         }
276
277         if (!$directory) {
278             $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
279         }
280
281         $this->exportConfig(
282             $directory,
283             $io,
284             $this->trans('commands.config.export.single.messages.config-exported')
285         );
286
287         return 0;
288     }
289 }