ConfigLister.php in Configuration Update Manager 8
File
src/ConfigLister.php
View source
<?php
namespace Drupal\config_update;
use Drupal\Core\Config\ExtensionInstallStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Extension\Extension;
class ConfigLister implements ConfigListInterface {
protected $typesByPrefix = [];
protected $definitions = [];
protected $entityManager;
protected $activeConfigStorage;
protected $extensionConfigStorage;
protected $extensionOptionalConfigStorage;
public function __construct(EntityTypeManagerInterface $entity_manager, StorageInterface $active_config_storage, ExtensionInstallStorage $extension_config_storage, ExtensionInstallStorage $extension_optional_config_storage) {
$this->entityManager = $entity_manager;
$this->activeConfigStorage = $active_config_storage;
$this->extensionConfigStorage = $extension_config_storage;
$this->extensionOptionalConfigStorage = $extension_optional_config_storage;
}
public function listTypes() {
if (count($this->definitions)) {
return $this->definitions;
}
foreach ($this->entityManager
->getDefinitions() as $entity_type => $definition) {
if ($definition
->entityClassImplements('Drupal\\Core\\Config\\Entity\\ConfigEntityInterface')) {
$this->definitions[$entity_type] = $definition;
$prefix = $definition
->getConfigPrefix();
$this->typesByPrefix[$prefix] = $entity_type;
}
}
ksort($this->definitions);
return $this->definitions;
}
public function getType($name) {
$definitions = $this
->listTypes();
return isset($definitions[$name]) ? $definitions[$name] : NULL;
}
public function getTypeByPrefix($prefix) {
$definitions = $this
->listTypes();
return isset($this->typesByPrefix[$prefix]) ? $definitions[$this->typesByPrefix[$prefix]] : NULL;
}
public function getTypeNameByConfigName($name) {
$definitions = $this
->listTypes();
foreach ($this->typesByPrefix as $prefix => $entity_type) {
if (strpos($name, $prefix . '.') === 0) {
return $entity_type;
}
}
return NULL;
}
public function listConfig($list_type, $name) {
$active_list = [];
$install_list = [];
$optional_list = [];
$definitions = $this
->listTypes();
switch ($list_type) {
case 'type':
if ($name == 'system.all') {
$active_list = $this->activeConfigStorage
->listAll();
$install_list = $this->extensionConfigStorage
->listAll();
$optional_list = $this->extensionOptionalConfigStorage
->listAll();
}
elseif ($name == 'system.simple') {
$active_list = $this
->omitKnownPrefixes($this->activeConfigStorage
->listAll());
$install_list = $this
->omitKnownPrefixes($this->extensionConfigStorage
->listAll());
$optional_list = $this
->omitKnownPrefixes($this->extensionOptionalConfigStorage
->listAll());
}
elseif (isset($this->definitions[$name])) {
$definition = $this->definitions[$name];
$prefix = $definition
->getConfigPrefix();
$active_list = $this->activeConfigStorage
->listAll($prefix);
$install_list = $this->extensionConfigStorage
->listAll($prefix);
$optional_list = $this->extensionOptionalConfigStorage
->listAll($prefix);
}
break;
case 'profile':
$name = $this
->getProfileName();
case 'module':
case 'theme':
$active_list = $this->activeConfigStorage
->listAll();
$install_list = $this
->listProvidedItems($list_type, $name);
$optional_list = $this
->listProvidedItems($list_type, $name, TRUE);
break;
}
if (!is_array($optional_list)) {
$optional_list = [];
}
return [
$active_list,
$install_list,
$optional_list,
];
}
protected function listProvidedItems($type, $name, $do_optional = FALSE) {
$pathname = drupal_get_filename($type, $name);
$component = new Extension(\Drupal::root(), $type, $pathname);
if ($do_optional) {
$names = $this->extensionOptionalConfigStorage
->getComponentNames([
$component,
]);
}
else {
$names = $this->extensionConfigStorage
->getComponentNames([
$component,
]);
}
return array_keys($names);
}
protected function omitKnownPrefixes($list) {
$prefixes = array_keys($this->typesByPrefix);
$list = array_combine($list, $list);
foreach ($list as $name) {
foreach ($prefixes as $prefix) {
if (strpos($name, $prefix . '.') === 0) {
unset($list[$name]);
}
}
}
return array_values($list);
}
protected function getProfileName() {
$config = $this->activeConfigStorage
->read('core.extension');
if (!empty($config['profile'])) {
return $config['profile'];
}
else {
return Settings::get('install_profile');
}
}
}