public function testNoConfigEntities() { $dep_manger = new ConfigDependencyManager(); $dep_manger->setData(array('simple.config' => array('key' => 'value'))); $this->assertEmpty($dep_manger->getDependentEntities('config', 'config_test.dynamic.entity_id:745b0ce0-aece-42dd-a800-ade5b8455e84')); // Configuration is always dependent on its provider. $dependencies = $dep_manger->getDependentEntities('module', 'simple'); $this->assertArrayHasKey('simple.config', $dependencies); $this->assertCount(1, $dependencies); }
/** * Creates configuration in a collection based on the provided list. * * @param string $collection * The configuration collection. * @param array $config_to_create * An array of configuration data to create, keyed by name. */ protected function createConfiguration($collection, array $config_to_create) { // Order the configuration to install in the order of dependencies. if ($collection == StorageInterface::DEFAULT_COLLECTION) { $dependency_manager = new ConfigDependencyManager(); $config_names = $dependency_manager->setData($config_to_create)->sortAll(); } else { $config_names = array_keys($config_to_create); } foreach ($config_names as $name) { // Allow config factory overriders to use a custom configuration object if // they are responsible for the collection. $overrider = $this->configManager->getConfigCollectionInfo()->getOverrideService($collection); if ($overrider) { $new_config = $overrider->createConfigObject($name, $collection); } else { $new_config = new Config($name, $this->getActiveStorages($collection), $this->eventDispatcher, $this->typedConfig); } if ($config_to_create[$name] !== FALSE) { $new_config->setData($config_to_create[$name]); } if ($collection == StorageInterface::DEFAULT_COLLECTION && ($entity_type = $this->configManager->getEntityTypeIdByName($name))) { // If we are syncing do not create configuration entities. Pluggable // configuration entities can have dependencies on modules that are // not yet enabled. This approach means that any code that expects // default configuration entities to exist will be unstable after the // module has been enabled and before the config entity has been // imported. if ($this->isSyncing()) { continue; } /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage */ $entity_storage = $this->configManager->getEntityManager()->getStorage($entity_type); // It is possible that secondary writes can occur during configuration // creation. Updates of such configuration are allowed. if ($this->getActiveStorages($collection)->exists($name)) { $id = $entity_storage->getIDFromConfigName($name, $entity_storage->getEntityType()->getConfigPrefix()); $entity = $entity_storage->load($id); $entity = $entity_storage->updateFromStorageRecord($entity, $new_config->get()); } else { $entity = $entity_storage->createFromStorageRecord($new_config->get()); } if ($entity->isInstallable()) { $entity->trustData()->save(); } } else { $new_config->save(TRUE); } } }
/** * Gets and sorts configuration data from the source and target storages. */ protected function getAndSortConfigData($collection) { $source_storage = $this->getSourceStorage($collection); $target_storage = $this->getTargetStorage($collection); $target_names = $target_storage->listAll(); $source_names = $source_storage->listAll(); // Prime the static caches by reading all the configuration in the source // and target storages. $target_data = $target_storage->readMultiple($target_names); $source_data = $source_storage->readMultiple($source_names); // If the collection only supports simple configuration do not use // configuration dependencies. if ($this->configManager->supportsConfigurationEntities($collection)) { $dependency_manager = new ConfigDependencyManager(); $this->targetNames[$collection] = $dependency_manager->setData($target_data)->sortAll(); $this->sourceNames[$collection] = $dependency_manager->setData($source_data)->sortAll(); } else { $this->targetNames[$collection] = $target_names; $this->sourceNames[$collection] = $source_names; } }
/** * {@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 getConfigDependencyManager() { $dependency_manager = new ConfigDependencyManager(); // Read all configuration using the factory. This ensures that multiple // deletes during the same request benefit from the static cache. Using the // factory also ensures 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_map(function ($config) { $data = $config->get(); if (isset($data['uuid'])) { return $data; } return FALSE; }, $this->configFactory->loadMultiple($this->activeStorage->listAll())); $dependency_manager->setData(array_filter($data)); return $dependency_manager; }
/** * Creates configuration in a collection based on the provided list. * * @param string $collection * The configuration collection. * @param array $config_to_install * A list of configuration object names to create. */ protected function createConfiguration($collection, array $config_to_install) { // Order the configuration to install in the order of dependencies. $data = $this->getSourceStorage($collection)->readMultiple($config_to_install); $config_entity_support = $this->configManager->supportsConfigurationEntities($collection); if ($config_entity_support) { $dependency_manager = new ConfigDependencyManager(); $config_to_install = $dependency_manager->setData($data)->sortAll(); } // Remove configuration that already exists in the active storage. $config_to_install = array_diff($config_to_install, $this->getActiveStorage($collection)->listAll()); foreach ($config_to_install as $name) { // Allow config factory overriders to use a custom configuration object if // they are responsible for the collection. $overrider = $this->configManager->getConfigCollectionInfo()->getOverrideService($collection); if ($overrider) { $new_config = $overrider->createConfigObject($name, $collection); } else { $new_config = new Config($name, $this->getActiveStorage($collection), $this->eventDispatcher, $this->typedConfig); } if ($data[$name] !== FALSE) { $new_config->setData($data[$name]); } if ($config_entity_support && ($entity_type = $this->configManager->getEntityTypeIdByName($name))) { // If we are syncing do not create configuration entities. Pluggable // configuration entities can have dependencies on modules that are // not yet enabled. This approach means that any code that expects // default configuration entities to exist will be unstable after the // module has been enabled and before the config entity has been // imported. if ($this->isSyncing) { continue; } $entity_storage = $this->configManager->getEntityManager()->getStorage($entity_type); // It is possible that secondary writes can occur during configuration // creation. Updates of such configuration are allowed. if ($this->getActiveStorage($collection)->exists($name)) { $id = $entity_storage->getIDFromConfigName($name, $entity_storage->getEntityType()->getConfigPrefix()); $entity = $entity_storage->load($id); foreach ($new_config->get() as $property => $value) { $entity->set($property, $value); } $entity->save(); } else { $entity_storage->create($new_config->get())->save(); } } else { $new_config->save(); } } }
/** * @dataProvider providerTestSortAll */ public function testSortAll(array $data, array $expected_order) { $dependency_manager = new ConfigDependencyManager(); $dependency_manager->setData($data); $this->assertEquals($expected_order, $dependency_manager->sortAll()); }
/** * {@inheritdoc} */ public function setData(array $data) { parent::setData($data); $this->sorted_graph = NULL; return $this; }