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;
22 class ExportSingleCommand extends Command
30 protected $definitions;
33 * @var EntityTypeManagerInterface
35 protected $entityTypeManager;
40 protected $configStorage;
42 protected $configExport;
45 * ExportSingleCommand constructor.
47 * @param EntityTypeManagerInterface $entityTypeManager
48 * @param CachedStorage $configStorage
50 public function __construct(
51 EntityTypeManagerInterface $entityTypeManager,
52 CachedStorage $configStorage
54 $this->entityTypeManager = $entityTypeManager;
55 $this->configStorage = $configStorage;
56 parent::__construct();
62 protected function configure()
65 ->setName('config:export:single')
66 ->setDescription($this->trans('commands.config.export.single.description'))
70 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
71 $this->trans('commands.config.export.single.options.name')
75 InputOption::VALUE_OPTIONAL,
76 $this->trans('commands.config.export.arguments.directory')
80 InputOption::VALUE_OPTIONAL,
81 $this->trans('commands.common.options.module')
83 'include-dependencies',
85 InputOption::VALUE_NONE,
86 $this->trans('commands.config.export.single.options.include-dependencies')
90 InputOption::VALUE_NONE,
91 $this->trans('commands.config.export.single.options.optional')
95 InputOption::VALUE_NONE,
96 $this->trans('commands.config.export.single.options.remove-uuid')
100 InputOption::VALUE_NONE,
101 $this->trans('commands.config.export.single.options.remove-config-hash')
106 * Return config types
108 protected function getConfigTypes()
110 foreach ($this->entityTypeManager->getDefinitions() as $entity_type => $definition) {
111 if ($definition->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
112 $this->definitions[$entity_type] = $definition;
115 $entity_types = array_map(
116 function ($definition) {
117 return $definition->getLabel();
118 }, $this->definitions
121 uasort($entity_types, 'strnatcasecmp');
123 'system.simple' => $this->trans('commands.config.export.single.options.simple-configuration'),
126 return $config_types;
130 * Return config types
132 protected function getConfigNames($config_type)
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;
144 // Handle simple configuration.
146 // Gather the config entity prefixes.
147 $config_prefixes = array_map(
148 function ($definition) {
149 return $definition->getConfigPrefix() . '.';
150 }, $this->definitions
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]);
171 protected function interact(InputInterface $input, OutputInterface $output)
173 $io = new DrupalStyle($input, $output);
175 $config_types = $this->getConfigTypes();
177 $name = $input->getOption('name');
179 $type = $io->choiceNoList(
180 $this->trans('commands.config.export.single.questions.config-type'),
181 array_keys($config_types),
184 $names = $this->getConfigNames($type);
186 $name = $io->choiceNoList(
187 $this->trans('commands.config.export.single.questions.name'),
191 if ($type !== 'system.simple') {
192 $definition = $this->entityTypeManager->getDefinition($type);
193 $name = $definition->getConfigPrefix() . '.' . $name;
195 $input->setOption('name', $name);
198 $module = $input->getOption('module');
200 $optionalConfig = $input->getOption('optional');
201 if (!$optionalConfig) {
202 $optionalConfig = $io->confirm(
203 $this->trans('commands.config.export.single.questions.optional'),
206 $input->setOption('optional', $optionalConfig);
210 if (!$input->getOption('remove-uuid')) {
211 $removeUuid = $io->confirm(
212 $this->trans('commands.config.export.single.questions.remove-uuid'),
215 $input->setOption('remove-uuid', $removeUuid);
217 if (!$input->getOption('remove-config-hash')) {
218 $removeHash = $io->confirm(
219 $this->trans('commands.config.export.single.questions.remove-config-hash'),
222 $input->setOption('remove-config-hash', $removeHash);
230 protected function execute(InputInterface $input, OutputInterface $output)
232 $io = new DrupalStyle($input, $output);
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');
241 foreach ($ame as $nameItem) {
242 $config = $this->getConfiguration(
249 $this->configExport[$nameItem] = [
251 'optional' => $optional
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);
261 $io->error($this->trans('commands.config.export.single.messages.config-not-found'));
266 $this->exportConfigToModule(
270 'commands.config.export.single.messages.config-exported'
278 $directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
284 $this->trans('commands.config.export.single.messages.config-exported')