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