<?php
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigException;
use Drupal\Core\Config\ConfigInstaller;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\StorageComparer;
use Symfony\Component\Yaml\Dumper;
function config_install_default_config($type, $name) {
$config_dir = drupal_get_path($type, $name) . '/config';
if (is_dir($config_dir)) {
$source_storage = new FileStorage($config_dir);
$storage_comparer = new StorageComparer($source_storage, Drupal::service('config.storage'));
$storage_comparer
->addChangelistCreate();
$installer = new ConfigInstaller($storage_comparer, Drupal::service('event_dispatcher'), Drupal::service('config.factory'), Drupal::entityManager(), Drupal::lock());
$installer
->import();
}
}
function config_uninstall_default_config($type, $name) {
$storage = drupal_container()
->get('config.storage');
$config_names = $storage
->listAll($name . '.');
foreach ($config_names as $config_name) {
config($config_name)
->delete();
}
}
function config_get_storage_names_with_prefix($prefix = '') {
return drupal_container()
->get('config.storage')
->listAll($prefix);
}
function config($name) {
return drupal_container()
->get('config.factory')
->get($name);
}
function config_context_enter($context_name) {
if (drupal_container()
->has($context_name)) {
$context = drupal_container()
->get($context_name);
}
elseif (class_exists($context_name) && in_array('Drupal\\Core\\Config\\Context\\ContextInterface', class_implements($context_name))) {
$context = drupal_container()
->get('config.context.factory')
->get($context_name);
}
else {
throw new ConfigException(sprintf('Unknown config context service or class: %s', $context_name));
}
drupal_container()
->get('config.factory')
->enterContext($context);
return $context;
}
function config_context_leave() {
drupal_container()
->get('config.factory')
->leaveContext();
}
function config_get_module_config_entities($module) {
$info = entity_get_info();
return array_filter($info, function ($entity_info) use ($module) {
return $entity_info['module'] == $module && is_subclass_of($entity_info['class'], 'Drupal\\Core\\Config\\Entity\\ConfigEntityInterface');
});
}
function config_get_entity_type_by_name($name) {
$entities = array_filter(entity_get_info(), function ($entity_info) use ($name) {
return isset($entity_info['config_prefix']) && strpos($name, $entity_info['config_prefix'] . '.') === 0;
});
return key($entities);
}
function config_typed() {
return drupal_container()
->get('config.typed');
}
function config_import_create_snapshot(StorageInterface $source_storage, StorageInterface $snapshot_storage) {
$snapshot_storage
->deleteAll();
foreach ($source_storage
->listAll() as $name) {
$snapshot_storage
->write($name, $source_storage
->read($name));
}
}
function config_diff(StorageInterface $source_storage, StorageInterface $target_storage, $name) {
require_once DRUPAL_ROOT . '/core/lib/Drupal/Component/Diff/DiffEngine.php';
$dumper = new Dumper();
$dumper
->setIndentation(2);
$source_data = explode("\n", $dumper
->dump($source_storage
->read($name), PHP_INT_MAX));
$target_data = explode("\n", $dumper
->dump($target_storage
->read($name), PHP_INT_MAX));
if ($source_data === array(
'false',
)) {
$source_data = array(
t('File added'),
);
}
if ($target_data === array(
'false',
)) {
$target_data = array(
t('File removed'),
);
}
return new Diff($source_data, $target_data);
}