Newer
Older
catch
committed
<?php
/**
* @file
* Contains \Drupal\Core\Config\ConfigManager.
*/
namespace Drupal\Core\Config;
use Drupal\Core\Config\Entity\ConfigDependencyManager;
catch
committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\StringTranslation\TranslationManager;
use Symfony\Component\Yaml\Dumper;
/**
* The ConfigManager provides helper functions for the configuration system.
*/
class ConfigManager implements ConfigManagerInterface {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The typed config manager.
*
* @var \Drupal\Core\Config\TypedConfigManager
*/
protected $typedConfigManager;
/**
* The string translation service.
*
* @var \Drupal\Core\StringTranslation\TranslationManager
*/
protected $stringTranslation;
/**
* The active configuration storage.
*
* @var \Drupal\Core\Config\StorageInterface
*/
protected $activeStorage;
catch
committed
/**
* Creates ConfigManager objects.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @param \Drupal\Core\Config\TypedConfigManager $typed_config_manager
* The typed config manager.
* @param \Drupal\Core\StringTranslation\TranslationManager $string_translation
* The string translation service.
catch
committed
*/
public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory, TypedConfigManager $typed_config_manager, TranslationManager $string_translation, StorageInterface $active_storage) {
catch
committed
$this->entityManager = $entity_manager;
$this->configFactory = $config_factory;
$this->typedConfigManager = $typed_config_manager;
$this->stringTranslation = $string_translation;
$this->activeStorage = $active_storage;
catch
committed
}
/**
* {@inheritdoc}
*/
public function getEntityTypeIdByName($name) {
$entities = array_filter($this->entityManager->getDefinitions(), function (EntityTypeInterface $entity_type) use ($name) {
return ($config_prefix = $entity_type->getConfigPrefix()) && strpos($name, $config_prefix . '.') === 0;
});
return key($entities);
}
/**
* {@inheritdoc}
*/
public function getEntityManager() {
return $this->entityManager;
}
/**
* {@inheritdoc}
*/
public function getConfigFactory() {
return $this->configFactory;
}
catch
committed
/**
* {@inheritdoc}
*/
Angie Byron
committed
public function diff(StorageInterface $source_storage, StorageInterface $target_storage, $source_name, $target_name = NULL) {
if (!isset($target_name)) {
$target_name = $source_name;
}
catch
committed
// @todo Replace with code that can be autoloaded.
// https://drupal.org/node/1848266
require_once __DIR__ . '/../../Component/Diff/DiffEngine.php';
catch
committed
// The output should show configuration object differences formatted as YAML.
// But the configuration is not necessarily stored in files. Therefore, they
// need to be read and parsed, and lastly, dumped into YAML strings.
$dumper = new Dumper();
$dumper->setIndentation(2);
Angie Byron
committed
$source_data = explode("\n", $dumper->dump($source_storage->read($source_name), PHP_INT_MAX));
$target_data = explode("\n", $dumper->dump($target_storage->read($target_name), PHP_INT_MAX));
catch
committed
// Check for new or removed files.
if ($source_data === array('false')) {
// Added file.
$source_data = array($this->stringTranslation->translate('File added'));
}
if ($target_data === array('false')) {
// Deleted file.
$target_data = array($this->stringTranslation->translate('File removed'));
}
return new \Diff($source_data, $target_data);
}
/**
* {@inheritdoc}
*/
public function createSnapshot(StorageInterface $source_storage, StorageInterface $snapshot_storage) {
catch
committed
$snapshot_storage->deleteAll();
foreach ($source_storage->listAll() as $name) {
$snapshot_storage->write($name, $source_storage->read($name));
}
}
/**
* {@inheritdoc}
catch
committed
*/
public function uninstall($type, $name) {
// Remove all dependent configuration entities.
$dependent_entities = $this->findConfigEntityDependentsAsEntities($type, array($name));
// Reverse the array to that entities are removed in the correct order of
// dependence. For example, this ensures that field instances are removed
// before fields.
foreach (array_reverse($dependent_entities) as $entity) {
$entity->setUninstalling(TRUE);
$entity->delete();
}
catch
committed
$config_names = $this->configFactory->listAll($name . '.');
foreach ($config_names as $config_name) {
$this->configFactory->get($config_name)->delete();
}
$schema_dir = drupal_get_path($type, $name) . '/config/schema';
if (is_dir($schema_dir)) {
// Refresh the schema cache if uninstalling an extension that provides
// configuration schema.
$this->typedConfigManager->clearCachedDefinitions();
}
}
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
* {@inheritdoc}
*/
public function findConfigEntityDependents($type, array $names) {
$dependency_manager = new ConfigDependencyManager();
// This uses the configuration storage directly to avoid blowing the static
// caches in the configuration factory and the configuration entity system.
// Additionally this ensures that configuration entity dependency discovery
// has no dependencies on the config entity classes. Assume data with UUID
// is a config entity. Only configuration entities can be depended on so we
// can ignore everything else.
$data = array_filter($this->activeStorage->readMultiple($this->activeStorage->listAll()), function($config) {
return isset($config['uuid']);
});
$dependency_manager->setData($data);
$dependencies = array();
foreach ($names as $name) {
$dependencies = array_merge($dependencies, $dependency_manager->getDependentEntities($type, $name));
}
return $dependencies;
}
/**
* {@inheritdoc}
*/
public function findConfigEntityDependentsAsEntities($type, array $names) {
$dependencies = $this->findConfigEntityDependents($type, $names);
$entities = array();
$definitions = $this->entityManager->getDefinitions();
foreach ($dependencies as $config_name => $dependency) {
// Group by entity type to efficient load entities using
catch
committed
// \Drupal\Core\Entity\EntityStorageInterface::loadMultiple().
$entity_type_id = $this->getEntityTypeIdByName($config_name);
// It is possible that a non-configuration entity will be returned if a
// simple configuration object has a UUID key. This would occur if the
// dependents of the system module are calculated since system.site has
// a UUID key.
if ($entity_type_id) {
$id = substr($config_name, strlen($definitions[$entity_type_id]->getConfigPrefix()) + 1);
$entities[$entity_type_id][] = $id;
}
}
$entities_to_return = array();
foreach ($entities as $entity_type_id => $entities_to_load) {
catch
committed
$storage = $this->entityManager->getStorage($entity_type_id);
// Remove the keys since there are potential ID clashes from different
// configuration entity types.
catch
committed
$entities_to_return = array_merge($entities_to_return, array_values($storage->loadMultiple($entities_to_load)));
}
return $entities_to_return;
}