Skip to content

Commit

Permalink
[console] Support for configurations has multilanguage (#3605)
Browse files Browse the repository at this point in the history
  • Loading branch information
josmera01 authored and jmolivas committed Jan 7, 2018
1 parent 8f47242 commit d1b230f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 45 deletions.
2 changes: 1 addition & 1 deletion config/services/config.yml
Expand Up @@ -26,7 +26,7 @@ services:
- { name: drupal.command }
console.config_export_single:
class: Drupal\Console\Command\Config\ExportSingleCommand
arguments: ['@entity_type.manager', '@config.storage', '@console.extension_manager']
arguments: ['@entity_type.manager', '@config.storage', '@console.extension_manager','@language_manager']
tags:
- { name: drupal.command }
console.config_export_view:
Expand Down
122 changes: 80 additions & 42 deletions src/Command/Config/ExportSingleCommand.php
Expand Up @@ -18,6 +18,7 @@
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Command\Shared\ExportTrait;
use Drupal\Console\Extension\Manager;
use Drupal\Core\Language\LanguageManagerInterface;
use Webmozart\PathUtil\Path;

class ExportSingleCommand extends Command
Expand All @@ -44,23 +45,34 @@ class ExportSingleCommand extends Command
*/
protected $extensionManager;

/**
* @var Configuration.
*/
protected $configExport;

/**
* @var LanguageManagerInterface
*/
protected $languageManager;

/**
* ExportSingleCommand constructor.
*
* @param EntityTypeManagerInterface $entityTypeManager
* @param CachedStorage $configStorage
* @param Manager $extensionManager
* @param languageManager $languageManager
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
CachedStorage $configStorage,
Manager $extensionManager
Manager $extensionManager,
LanguageManagerInterface $languageManager
) {
$this->entityTypeManager = $entityTypeManager;
$this->configStorage = $configStorage;
$this->extensionManager = $extensionManager;
$this->languageManager = $languageManager;
parent::__construct();
}

Expand Down Expand Up @@ -248,57 +260,83 @@ protected function execute(InputInterface $input, OutputInterface $output)
$removeHash = $input->getOption('remove-config-hash');
$includeDependencies = $input->getOption('include-dependencies');

foreach ($name as $nameItem) {
$config = $this->getConfiguration(
$nameItem,
$removeUuid,
$removeHash
);

if ($config) {
$this->configExport[$nameItem] = [
'data' => $config,
'optional' => $optional
];
foreach ($this->getLanguage() as $value) {
foreach ($name as $nameItem) {
$config = $this->getConfiguration(
$nameItem,
$removeUuid,
$removeHash,
$value
);

if ($includeDependencies) {
// Include config dependencies in export files
if ($dependencies = $this->fetchDependencies($config, 'config')) {
$this->resolveDependencies($dependencies, $optional);
if ($config) {
$this->configExport[$nameItem] = [
'data' => $config,
'optional' => $optional
];

if ($includeDependencies) {
// Include config dependencies in export files
if ($dependencies = $this->fetchDependencies($config, 'config')) {
$this->resolveDependencies($dependencies, $optional);
}
}
} else {
$io->error($this->trans('commands.config.export.single.messages.config-not-found'));
}
} else {
$io->error($this->trans('commands.config.export.single.messages.config-not-found'));
}
}

if ($module) {
$this->exportConfigToModule(
$module,
$io,
$this->trans(
'commands.config.export.single.messages.config-exported'
)
);
if ($module) {
$this->exportConfigToModule(
$module,
$io,
$this->trans(
'commands.config.export.single.messages.config-exported'
)
);

return 0;
}
return 0;
}

if (!is_dir($directory)) {
$directory = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
} else {
$directory = Path::canonicalize($directory);
if (!file_exists($directory)) {
mkdir($directory, 0755, true);
if (!is_dir($directory)) {
$directory = $directory_copy = config_get_config_directory(CONFIG_SYNC_DIRECTORY);
if ($value) {
$directory = $directory_copy .'/' . str_replace('.', '/', $value);
}
} else {
$directory = $directory_copy .'/' . str_replace('.', '/', $value);
$directory = Path::canonicalize($directory);
if (!file_exists($directory)) {
mkdir($directory, 0755, true);
}
}
}

$this->exportConfig(
$directory,
$io,
$this->trans('commands.config.export.single.messages.config-exported')
);
$this->exportConfig(
$directory,
$io,
$this->trans('commands.config.export.single.messages.config-exported')
);
}

return 0;
}

/**
* Get the languague enable.
*/
protected function getLanguage()
{
$output = [];
// Get the language that be for default.
$default_id = $this->languageManager->getDefaultLanguage()->getId();
foreach ($this->languageManager->getLanguages() as $key => $value) {
if ($default_id == $key) {
$output[] = '';
}
else {
$output[] = 'language.' . $value->getId();
}
}
return $output;
}
}
4 changes: 2 additions & 2 deletions src/Command/Shared/ExportTrait.php
Expand Up @@ -22,10 +22,10 @@ trait ExportTrait
* @param bool|false $uuid
* @return mixed
*/
protected function getConfiguration($configName, $uuid = false, $hash = false)
protected function getConfiguration($configName, $uuid = false, $hash = false, $collection = '')
{
$config = $this->configStorage->read($configName);

$config = $this->configStorage->createCollection($collection)->read($configName);
// Exclude uuid base in parameter, useful to share configurations.
if ($uuid) {
unset($config['uuid']);
Expand Down

0 comments on commit d1b230f

Please sign in to comment.