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 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;
25 class ExportSingleCommand extends Command
33 protected $definitions;
36 * @var EntityTypeManagerInterface
38 protected $entityTypeManager;
43 protected $configStorage;
48 protected $extensionManager;
53 protected $configExport;
56 * @var LanguageManagerInterface
58 protected $languageManager;
66 * ExportSingleCommand constructor.
68 * @param EntityTypeManagerInterface $entityTypeManager
69 * @param CachedStorage $configStorage
70 * @param Manager $extensionManager
71 * @param languageManager $languageManager
72 * @param Validator $validator
74 public function __construct(
75 EntityTypeManagerInterface $entityTypeManager,
76 CachedStorage $configStorage,
77 Manager $extensionManager,
78 LanguageManagerInterface $languageManager,
81 $this->entityTypeManager = $entityTypeManager;
82 $this->configStorage = $configStorage;
83 $this->extensionManager = $extensionManager;
84 $this->languageManager = $languageManager;
85 $this->validator = $validator;
86 parent::__construct();
92 protected function configure()
95 ->setName('config:export:single')
96 ->setDescription($this->trans('commands.config.export.single.description'))
100 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
101 $this->trans('commands.config.export.single.options.name')
105 InputOption::VALUE_OPTIONAL,
106 $this->trans('commands.config.export.arguments.directory')
110 InputOption::VALUE_OPTIONAL,
111 $this->trans('commands.common.options.module')
113 'include-dependencies',
115 InputOption::VALUE_NONE,
116 $this->trans('commands.config.export.single.options.include-dependencies')
120 InputOption::VALUE_NONE,
121 $this->trans('commands.config.export.single.options.optional')
125 InputOption::VALUE_NONE,
126 $this->trans('commands.config.export.single.options.remove-uuid')
128 'remove-config-hash',
130 InputOption::VALUE_NONE,
131 $this->trans('commands.config.export.single.options.remove-config-hash')
133 ->setAliases(['ces']);
137 * Return config types
139 protected function getConfigTypes()
141 foreach ($this->entityTypeManager->getDefinitions() as $entity_type => $definition) {
142 if ($definition->isSubclassOf('Drupal\Core\Config\Entity\ConfigEntityInterface')) {
143 $this->definitions[$entity_type] = $definition;
146 $entity_types = array_map(
147 function ($definition) {
148 return $definition->getLabel();
149 }, $this->definitions
152 uasort($entity_types, 'strnatcasecmp');
154 'system.simple' => $this->trans('commands.config.export.single.options.simple-configuration'),
157 return $config_types;
161 * Return config types
163 protected function getConfigNames($config_type)
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;
175 // Handle simple configuration.
177 // Gather the config entity prefixes.
178 $config_prefixes = array_map(
179 function ($definition) {
180 return $definition->getConfigPrefix() . '.';
181 }, $this->definitions
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]);
202 protected function interact(InputInterface $input, OutputInterface $output)
204 $config_types = $this->getConfigTypes();
206 $name = $input->getOption('name');
208 $type = $this->getIo()->choiceNoList(
209 $this->trans('commands.config.export.single.questions.config-type'),
210 array_keys($config_types),
213 $names = $this->getConfigNames($type);
215 $name = $this->getIo()->choiceNoList(
216 $this->trans('commands.config.export.single.questions.name'),
220 if ($type !== 'system.simple') {
221 $definition = $this->entityTypeManager->getDefinition($type);
222 $name = $definition->getConfigPrefix() . '.' . $name;
225 $input->setOption('name', [$name]);
229 $module = $this->getModuleOption();
231 $optionalConfig = $input->getOption('optional');
232 if (!$optionalConfig) {
233 $optionalConfig = $this->getIo()->confirm(
234 $this->trans('commands.config.export.single.questions.optional'),
237 $input->setOption('optional', $optionalConfig);
241 if (!$input->getOption('remove-uuid')) {
242 $removeUuid = $this->getIo()->confirm(
243 $this->trans('commands.config.export.single.questions.remove-uuid'),
246 $input->setOption('remove-uuid', $removeUuid);
248 if (!$input->getOption('remove-config-hash')) {
249 $removeHash = $this->getIo()->confirm(
250 $this->trans('commands.config.export.single.questions.remove-config-hash'),
253 $input->setOption('remove-config-hash', $removeHash);
260 protected function execute(InputInterface $input, OutputInterface $output)
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');
270 foreach ($this->getLanguage() as $value) {
271 foreach ($name as $nameItem) {
272 $config = $this->getConfiguration(
280 $this->configExport[$nameItem] = [
282 'optional' => $optional
285 if ($includeDependencies) {
286 // Include config dependencies in export files
287 if ($dependencies = $this->fetchDependencies($config, 'config')) {
288 $this->resolveDependencies($dependencies, $optional);
292 $this->getIo()->error($this->trans('commands.config.export.single.messages.config-not-found'));
297 $this->exportConfigToModule(
300 'commands.config.export.single.messages.config-exported'
307 if (!is_dir($directory)) {
308 $directory = $directory_copy = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
310 $directory = $directory_copy .'/' . str_replace('.', '/', $value);
313 $directory = $directory_copy .'/' . str_replace('.', '/', $value);
314 $directory = Path::canonicalize($directory);
315 if (!file_exists($directory)) {
316 mkdir($directory, 0755, true);
322 $this->trans('commands.config.export.single.messages.config-exported')
330 * Get the languague enable.
332 protected function getLanguage()
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) {
341 $output[] = 'language.' . $value->getId();