5 * Contains \Drupal\Console\Command\Config\ExportSingleCommand.
8 namespace Drupal\Console\Command\Config;
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;
23 class ExportSingleCommand extends Command
31 protected $definitions;
34 * @var EntityTypeManagerInterface
36 protected $entityTypeManager;
41 protected $configStorage;
43 protected $configExport;
46 * ExportSingleCommand constructor.
48 * @param EntityTypeManagerInterface $entityTypeManager
49 * @param CachedStorage $configStorage
50 * @param Manager $extensionManager
52 public function __construct(
53 EntityTypeManagerInterface $entityTypeManager,
54 CachedStorage $configStorage,
55 Manager $extensionManager
57 $this->entityTypeManager = $entityTypeManager;
58 $this->configStorage = $configStorage;
59 $this->extensionManager = $extensionManager;
60 parent::__construct();
66 protected function configure()
69 ->setName('config:export:single')
70 ->setDescription($this->trans('commands.config.export.single.description'))
74 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
75 $this->trans('commands.config.export.single.options.name')
79 InputOption::VALUE_OPTIONAL,
80 $this->trans('commands.config.export.arguments.directory')
84 InputOption::VALUE_OPTIONAL,
85 $this->trans('commands.common.options.module')
87 'include-dependencies',
89 InputOption::VALUE_NONE,
90 $this->trans('commands.config.export.single.options.include-dependencies')
94 InputOption::VALUE_NONE,
95 $this->trans('commands.config.export.single.options.optional')
99 InputOption::VALUE_NONE,
100 $this->trans('commands.config.export.single.options.remove-uuid')
102 'remove-config-hash',
104 InputOption::VALUE_NONE,
105 $this->trans('commands.config.export.single.options.remove-config-hash')
110 * Return config types
112 protected function getConfigTypes()
114 foreach ($this->entityTypeManager->getDefinitions() as $entity_type => $definition) {
115 if ($definition->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
116 $this->definitions[$entity_type] = $definition;
119 $entity_types = array_map(
120 function ($definition) {
121 return $definition->getLabel();
122 }, $this->definitions
125 uasort($entity_types, 'strnatcasecmp');
127 'system.simple' => $this->trans('commands.config.export.single.options.simple-configuration'),
130 return $config_types;
134 * Return config types
136 protected function getConfigNames($config_type)
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;
148 // Handle simple configuration.
150 // Gather the config entity prefixes.
151 $config_prefixes = array_map(
152 function ($definition) {
153 return $definition->getConfigPrefix() . '.';
154 }, $this->definitions
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]);
175 protected function interact(InputInterface $input, OutputInterface $output)
177 $io = new DrupalStyle($input, $output);
179 $config_types = $this->getConfigTypes();
181 $name = $input->getOption('name');
183 $type = $io->choiceNoList(
184 $this->trans('commands.config.export.single.questions.config-type'),
185 array_keys($config_types),
188 $names = $this->getConfigNames($type);
190 $name = $io->choiceNoList(
191 $this->trans('commands.config.export.single.questions.name'),
195 if ($type !== 'system.simple') {
196 $definition = $this->entityTypeManager->getDefinition($type);
197 $name = $definition->getConfigPrefix() . '.' . $name;
199 $input->setOption('name', $name);
202 $module = $input->getOption('module');
204 $optionalConfig = $input->getOption('optional');
205 if (!$optionalConfig) {
206 $optionalConfig = $io->confirm(
207 $this->trans('commands.config.export.single.questions.optional'),
210 $input->setOption('optional', $optionalConfig);
214 if (!$input->getOption('remove-uuid')) {
215 $removeUuid = $io->confirm(
216 $this->trans('commands.config.export.single.questions.remove-uuid'),
219 $input->setOption('remove-uuid', $removeUuid);
221 if (!$input->getOption('remove-config-hash')) {
222 $removeHash = $io->confirm(
223 $this->trans('commands.config.export.single.questions.remove-config-hash'),
226 $input->setOption('remove-config-hash', $removeHash);
234 protected function execute(InputInterface $input, OutputInterface $output)
236 $io = new DrupalStyle($input, $output);
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');
245 foreach ($ame as $nameItem) {
246 $config = $this->getConfiguration(
253 $this->configExport[$nameItem] = [
255 'optional' => $optional
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);
265 $io->error($this->trans('commands.config.export.single.messages.config-not-found'));
270 $this->exportConfigToModule(
274 'commands.config.export.single.messages.config-exported'
282 $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
288 $this->trans('commands.config.export.single.messages.config-exported')