Command.php in Config Importer and Tools 8.2
File
src/Drush/Command.php
View source
<?php
namespace Drupal\config_import\Drush;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
class Command {
private $info = [];
private $alias = '';
public function __construct($alias, array $info) {
$this->info = $info;
$this->alias = $alias;
$this->info['class'] = static::class;
$this->info['aliases'] = [
$this->alias,
];
$this
->options();
$this
->examples();
}
public function getDefinition() {
return $this->info;
}
protected function options() {
$this->info += [
__FUNCTION__ => [],
];
$options = [
'type' => [
'example-value' => 'menu',
'description' => dt('Config type.'),
],
'name' => [
'example-value' => 'main',
'description' => dt('Config name.'),
],
'destination' => [
'example-value' => 'sites/default/config/prod',
'description' => dt('Destination path to put configuration file to. Name of configuration folder can be used as well.'),
],
];
foreach ($this->info[__FUNCTION__] as $option => $is_required) {
if (isset($options[$option])) {
$this->info[__FUNCTION__][$option] = $options[$option] + [
'required' => $is_required,
];
}
}
}
protected function examples() {
$this->info += [
__FUNCTION__ => [],
];
foreach ($this->info[__FUNCTION__] as $example => $description) {
$this->info[__FUNCTION__][sprintf('drush %s %s', $this->alias, $example)] = $description;
unset($this->info[__FUNCTION__][$example]);
}
}
public static function processOptions(array $options) {
if (!empty($options['type'])) {
$options['prefix'] = static::getConfigPrefix($options['type']);
if (!empty($options['name'])) {
$options['name'] = $options['prefix'] . $options['name'];
if (!empty($options['destination'])) {
$options['destination'] = static::getDestination($options['destination'], $options['name']);
}
}
}
return $options;
}
protected static function getConfigPrefix($type) {
if ('system.simple' === $type) {
return '';
}
$definition = \Drupal::entityTypeManager()
->getDefinition($type);
if (!$definition instanceof ConfigEntityTypeInterface) {
throw new \RuntimeException(dt('Export is available only for entities of "@type" type.', [
'@type' => ConfigEntityTypeInterface::class,
]));
}
return $definition
->getConfigPrefix() . '.';
}
protected static function getDestination($destination, $name) {
$destination = rtrim($destination, '/');
if (!is_dir($destination)) {
$destination = config_get_config_directory($destination);
}
if (!is_dir($destination)) {
throw new \RuntimeException(dt('Destination directory "@destination" does not exists!', [
'@destination' => $destination,
]));
}
return "{$destination}/{$name}.yml";
}
}