annotate core/lib/Drupal/Core/Config/ConfigInstaller.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Config;
Chris@0 4
Chris@0 5 use Drupal\Component\Utility\Crypt;
Chris@0 6 use Drupal\Component\Utility\Unicode;
Chris@0 7 use Drupal\Core\Config\Entity\ConfigDependencyManager;
Chris@0 8 use Drupal\Core\Config\Entity\ConfigEntityDependency;
Chris@0 9 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Chris@0 10
Chris@0 11 class ConfigInstaller implements ConfigInstallerInterface {
Chris@0 12
Chris@0 13 /**
Chris@0 14 * The configuration factory.
Chris@0 15 *
Chris@0 16 * @var \Drupal\Core\Config\ConfigFactoryInterface
Chris@0 17 */
Chris@0 18 protected $configFactory;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * The active configuration storages, keyed by collection.
Chris@0 22 *
Chris@0 23 * @var \Drupal\Core\Config\StorageInterface[]
Chris@0 24 */
Chris@0 25 protected $activeStorages;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * The typed configuration manager.
Chris@0 29 *
Chris@0 30 * @var \Drupal\Core\Config\TypedConfigManagerInterface
Chris@0 31 */
Chris@0 32 protected $typedConfig;
Chris@0 33
Chris@0 34 /**
Chris@0 35 * The configuration manager.
Chris@0 36 *
Chris@0 37 * @var \Drupal\Core\Config\ConfigManagerInterface
Chris@0 38 */
Chris@0 39 protected $configManager;
Chris@0 40
Chris@0 41 /**
Chris@0 42 * The event dispatcher.
Chris@0 43 *
Chris@0 44 * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
Chris@0 45 */
Chris@0 46 protected $eventDispatcher;
Chris@0 47
Chris@0 48 /**
Chris@0 49 * The configuration storage that provides the default configuration.
Chris@0 50 *
Chris@0 51 * @var \Drupal\Core\Config\StorageInterface
Chris@0 52 */
Chris@0 53 protected $sourceStorage;
Chris@0 54
Chris@0 55 /**
Chris@0 56 * Is configuration being created as part of a configuration sync.
Chris@0 57 *
Chris@0 58 * @var bool
Chris@0 59 */
Chris@0 60 protected $isSyncing = FALSE;
Chris@0 61
Chris@0 62 /**
Chris@0 63 * The name of the currently active installation profile.
Chris@0 64 *
Chris@0 65 * @var string
Chris@0 66 */
Chris@0 67 protected $installProfile;
Chris@0 68
Chris@0 69 /**
Chris@0 70 * Constructs the configuration installer.
Chris@0 71 *
Chris@0 72 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 73 * The configuration factory.
Chris@0 74 * @param \Drupal\Core\Config\StorageInterface $active_storage
Chris@0 75 * The active configuration storage.
Chris@0 76 * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
Chris@0 77 * The typed configuration manager.
Chris@0 78 * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
Chris@0 79 * The configuration manager.
Chris@0 80 * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
Chris@0 81 * The event dispatcher.
Chris@0 82 * @param string $install_profile
Chris@0 83 * The name of the currently active installation profile.
Chris@0 84 */
Chris@0 85 public function __construct(ConfigFactoryInterface $config_factory, StorageInterface $active_storage, TypedConfigManagerInterface $typed_config, ConfigManagerInterface $config_manager, EventDispatcherInterface $event_dispatcher, $install_profile) {
Chris@0 86 $this->configFactory = $config_factory;
Chris@0 87 $this->activeStorages[$active_storage->getCollectionName()] = $active_storage;
Chris@0 88 $this->typedConfig = $typed_config;
Chris@0 89 $this->configManager = $config_manager;
Chris@0 90 $this->eventDispatcher = $event_dispatcher;
Chris@0 91 $this->installProfile = $install_profile;
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * {@inheritdoc}
Chris@0 96 */
Chris@0 97 public function installDefaultConfig($type, $name) {
Chris@0 98 $extension_path = $this->drupalGetPath($type, $name);
Chris@0 99 // Refresh the schema cache if the extension provides configuration schema
Chris@0 100 // or is a theme.
Chris@0 101 if (is_dir($extension_path . '/' . InstallStorage::CONFIG_SCHEMA_DIRECTORY) || $type == 'theme') {
Chris@0 102 $this->typedConfig->clearCachedDefinitions();
Chris@0 103 }
Chris@0 104
Chris@0 105 $default_install_path = $this->getDefaultConfigDirectory($type, $name);
Chris@0 106 if (is_dir($default_install_path)) {
Chris@0 107 if (!$this->isSyncing()) {
Chris@0 108 $storage = new FileStorage($default_install_path, StorageInterface::DEFAULT_COLLECTION);
Chris@0 109 $prefix = '';
Chris@0 110 }
Chris@0 111 else {
Chris@0 112 // The configuration importer sets the source storage on the config
Chris@0 113 // installer. The configuration importer handles all of the
Chris@0 114 // configuration entity imports. We only need to ensure that simple
Chris@0 115 // configuration is created when the extension is installed.
Chris@0 116 $storage = $this->getSourceStorage();
Chris@0 117 $prefix = $name . '.';
Chris@0 118 }
Chris@0 119
Chris@0 120 // Gets profile storages to search for overrides if necessary.
Chris@0 121 $profile_storages = $this->getProfileStorages($name);
Chris@0 122
Chris@0 123 // Gather information about all the supported collections.
Chris@0 124 $collection_info = $this->configManager->getConfigCollectionInfo();
Chris@0 125 foreach ($collection_info->getCollectionNames() as $collection) {
Chris@0 126 $config_to_create = $this->getConfigToCreate($storage, $collection, $prefix, $profile_storages);
Chris@0 127 // If we're installing a profile ensure configuration that is overriding
Chris@0 128 // is excluded.
Chris@0 129 if ($name == $this->drupalGetProfile()) {
Chris@0 130 $existing_configuration = $this->getActiveStorages($collection)->listAll();
Chris@0 131 $config_to_create = array_diff_key($config_to_create, array_flip($existing_configuration));
Chris@0 132 }
Chris@0 133 if (!empty($config_to_create)) {
Chris@0 134 $this->createConfiguration($collection, $config_to_create);
Chris@0 135 }
Chris@0 136 }
Chris@0 137 }
Chris@0 138
Chris@0 139 // During a drupal installation optional configuration is installed at the
Chris@0 140 // end of the installation process.
Chris@0 141 // @see install_install_profile()
Chris@0 142 if (!$this->isSyncing() && !$this->drupalInstallationAttempted()) {
Chris@0 143 $optional_install_path = $extension_path . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
Chris@0 144 if (is_dir($optional_install_path)) {
Chris@0 145 // Install any optional config the module provides.
Chris@0 146 $storage = new FileStorage($optional_install_path, StorageInterface::DEFAULT_COLLECTION);
Chris@0 147 $this->installOptionalConfig($storage, '');
Chris@0 148 }
Chris@0 149 // Install any optional configuration entities whose dependencies can now
Chris@0 150 // be met. This searches all the installed modules config/optional
Chris@0 151 // directories.
Chris@0 152 $storage = new ExtensionInstallStorage($this->getActiveStorages(StorageInterface::DEFAULT_COLLECTION), InstallStorage::CONFIG_OPTIONAL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION, FALSE, $this->installProfile);
Chris@0 153 $this->installOptionalConfig($storage, [$type => $name]);
Chris@0 154 }
Chris@0 155
Chris@0 156 // Reset all the static caches and list caches.
Chris@0 157 $this->configFactory->reset();
Chris@0 158 }
Chris@0 159
Chris@0 160 /**
Chris@0 161 * {@inheritdoc}
Chris@0 162 */
Chris@0 163 public function installOptionalConfig(StorageInterface $storage = NULL, $dependency = []) {
Chris@0 164 $profile = $this->drupalGetProfile();
Chris@0 165 $optional_profile_config = [];
Chris@0 166 if (!$storage) {
Chris@0 167 // Search the install profile's optional configuration too.
Chris@0 168 $storage = new ExtensionInstallStorage($this->getActiveStorages(StorageInterface::DEFAULT_COLLECTION), InstallStorage::CONFIG_OPTIONAL_DIRECTORY, StorageInterface::DEFAULT_COLLECTION, TRUE, $this->installProfile);
Chris@0 169 // The extension install storage ensures that overrides are used.
Chris@0 170 $profile_storage = NULL;
Chris@0 171 }
Chris@0 172 elseif (!empty($profile)) {
Chris@0 173 // Creates a profile storage to search for overrides.
Chris@0 174 $profile_install_path = $this->drupalGetPath('module', $profile) . '/' . InstallStorage::CONFIG_OPTIONAL_DIRECTORY;
Chris@0 175 $profile_storage = new FileStorage($profile_install_path, StorageInterface::DEFAULT_COLLECTION);
Chris@0 176 $optional_profile_config = $profile_storage->listAll();
Chris@0 177 }
Chris@0 178 else {
Chris@0 179 // Profile has not been set yet. For example during the first steps of the
Chris@0 180 // installer or during unit tests.
Chris@0 181 $profile_storage = NULL;
Chris@0 182 }
Chris@0 183
Chris@0 184 $enabled_extensions = $this->getEnabledExtensions();
Chris@0 185 $existing_config = $this->getActiveStorages()->listAll();
Chris@0 186
Chris@0 187 $list = array_unique(array_merge($storage->listAll(), $optional_profile_config));
Chris@0 188 $list = array_filter($list, function ($config_name) use ($existing_config) {
Chris@0 189 // Only list configuration that:
Chris@0 190 // - does not already exist
Chris@0 191 // - is a configuration entity (this also excludes config that has an
Chris@0 192 // implicit dependency on modules that are not yet installed)
Chris@0 193 return !in_array($config_name, $existing_config) && $this->configManager->getEntityTypeIdByName($config_name);
Chris@0 194 });
Chris@0 195
Chris@0 196 $all_config = array_merge($existing_config, $list);
Chris@0 197 $all_config = array_combine($all_config, $all_config);
Chris@0 198 $config_to_create = $storage->readMultiple($list);
Chris@0 199 // Check to see if the corresponding override storage has any overrides or
Chris@0 200 // new configuration that can be installed.
Chris@0 201 if ($profile_storage) {
Chris@0 202 $config_to_create = $profile_storage->readMultiple($list) + $config_to_create;
Chris@0 203 }
Chris@0 204 // Sort $config_to_create in the order of the least dependent first.
Chris@0 205 $dependency_manager = new ConfigDependencyManager();
Chris@0 206 $dependency_manager->setData($config_to_create);
Chris@0 207 $config_to_create = array_merge(array_flip($dependency_manager->sortAll()), $config_to_create);
Chris@0 208
Chris@0 209 foreach ($config_to_create as $config_name => $data) {
Chris@0 210 // Remove configuration where its dependencies cannot be met.
Chris@0 211 $remove = !$this->validateDependencies($config_name, $data, $enabled_extensions, $all_config);
Chris@0 212 // If $dependency is defined, remove configuration that does not have a
Chris@0 213 // matching dependency.
Chris@0 214 if (!$remove && !empty($dependency)) {
Chris@0 215 // Create a light weight dependency object to check dependencies.
Chris@0 216 $config_entity = new ConfigEntityDependency($config_name, $data);
Chris@0 217 $remove = !$config_entity->hasDependency(key($dependency), reset($dependency));
Chris@0 218 }
Chris@0 219
Chris@0 220 if ($remove) {
Chris@0 221 // Remove from the list of configuration to create.
Chris@0 222 unset($config_to_create[$config_name]);
Chris@0 223 // Remove from the list of all configuration. This ensures that any
Chris@0 224 // configuration that depends on this configuration is also removed.
Chris@0 225 unset($all_config[$config_name]);
Chris@0 226 }
Chris@0 227 }
Chris@0 228 if (!empty($config_to_create)) {
Chris@0 229 $this->createConfiguration(StorageInterface::DEFAULT_COLLECTION, $config_to_create, TRUE);
Chris@0 230 }
Chris@0 231 }
Chris@0 232
Chris@0 233 /**
Chris@0 234 * Gets configuration data from the provided storage to create.
Chris@0 235 *
Chris@0 236 * @param StorageInterface $storage
Chris@0 237 * The configuration storage to read configuration from.
Chris@0 238 * @param string $collection
Chris@0 239 * The configuration collection to use.
Chris@0 240 * @param string $prefix
Chris@0 241 * (optional) Limit to configuration starting with the provided string.
Chris@0 242 * @param \Drupal\Core\Config\StorageInterface[] $profile_storages
Chris@0 243 * An array of storage interfaces containing profile configuration to check
Chris@0 244 * for overrides.
Chris@0 245 *
Chris@0 246 * @return array
Chris@0 247 * An array of configuration data read from the source storage keyed by the
Chris@0 248 * configuration object name.
Chris@0 249 */
Chris@0 250 protected function getConfigToCreate(StorageInterface $storage, $collection, $prefix = '', array $profile_storages = []) {
Chris@0 251 if ($storage->getCollectionName() != $collection) {
Chris@0 252 $storage = $storage->createCollection($collection);
Chris@0 253 }
Chris@0 254 $data = $storage->readMultiple($storage->listAll($prefix));
Chris@0 255
Chris@0 256 // Check to see if the corresponding override storage has any overrides.
Chris@0 257 foreach ($profile_storages as $profile_storage) {
Chris@0 258 if ($profile_storage->getCollectionName() != $collection) {
Chris@0 259 $profile_storage = $profile_storage->createCollection($collection);
Chris@0 260 }
Chris@0 261 $data = $profile_storage->readMultiple(array_keys($data)) + $data;
Chris@0 262 }
Chris@0 263 return $data;
Chris@0 264 }
Chris@0 265
Chris@0 266 /**
Chris@0 267 * Creates configuration in a collection based on the provided list.
Chris@0 268 *
Chris@0 269 * @param string $collection
Chris@0 270 * The configuration collection.
Chris@0 271 * @param array $config_to_create
Chris@0 272 * An array of configuration data to create, keyed by name.
Chris@0 273 */
Chris@0 274 protected function createConfiguration($collection, array $config_to_create) {
Chris@0 275 // Order the configuration to install in the order of dependencies.
Chris@0 276 if ($collection == StorageInterface::DEFAULT_COLLECTION) {
Chris@0 277 $dependency_manager = new ConfigDependencyManager();
Chris@0 278 $config_names = $dependency_manager
Chris@0 279 ->setData($config_to_create)
Chris@0 280 ->sortAll();
Chris@0 281 }
Chris@0 282 else {
Chris@0 283 $config_names = array_keys($config_to_create);
Chris@0 284 }
Chris@0 285
Chris@0 286 foreach ($config_names as $name) {
Chris@0 287 // Allow config factory overriders to use a custom configuration object if
Chris@0 288 // they are responsible for the collection.
Chris@0 289 $overrider = $this->configManager->getConfigCollectionInfo()->getOverrideService($collection);
Chris@0 290 if ($overrider) {
Chris@0 291 $new_config = $overrider->createConfigObject($name, $collection);
Chris@0 292 }
Chris@0 293 else {
Chris@0 294 $new_config = new Config($name, $this->getActiveStorages($collection), $this->eventDispatcher, $this->typedConfig);
Chris@0 295 }
Chris@0 296 if ($config_to_create[$name] !== FALSE) {
Chris@0 297 $new_config->setData($config_to_create[$name]);
Chris@0 298 // Add a hash to configuration created through the installer so it is
Chris@0 299 // possible to know if the configuration was created by installing an
Chris@0 300 // extension and to track which version of the default config was used.
Chris@0 301 if (!$this->isSyncing() && $collection == StorageInterface::DEFAULT_COLLECTION) {
Chris@0 302 $new_config->set('_core.default_config_hash', Crypt::hashBase64(serialize($config_to_create[$name])));
Chris@0 303 }
Chris@0 304 }
Chris@0 305 if ($collection == StorageInterface::DEFAULT_COLLECTION && $entity_type = $this->configManager->getEntityTypeIdByName($name)) {
Chris@0 306 // If we are syncing do not create configuration entities. Pluggable
Chris@0 307 // configuration entities can have dependencies on modules that are
Chris@0 308 // not yet enabled. This approach means that any code that expects
Chris@0 309 // default configuration entities to exist will be unstable after the
Chris@0 310 // module has been enabled and before the config entity has been
Chris@0 311 // imported.
Chris@0 312 if ($this->isSyncing()) {
Chris@0 313 continue;
Chris@0 314 }
Chris@0 315 /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $entity_storage */
Chris@0 316 $entity_storage = $this->configManager
Chris@0 317 ->getEntityManager()
Chris@0 318 ->getStorage($entity_type);
Chris@0 319 // It is possible that secondary writes can occur during configuration
Chris@0 320 // creation. Updates of such configuration are allowed.
Chris@0 321 if ($this->getActiveStorages($collection)->exists($name)) {
Chris@0 322 $id = $entity_storage->getIDFromConfigName($name, $entity_storage->getEntityType()->getConfigPrefix());
Chris@0 323 $entity = $entity_storage->load($id);
Chris@0 324 $entity = $entity_storage->updateFromStorageRecord($entity, $new_config->get());
Chris@0 325 }
Chris@0 326 else {
Chris@0 327 $entity = $entity_storage->createFromStorageRecord($new_config->get());
Chris@0 328 }
Chris@0 329 if ($entity->isInstallable()) {
Chris@0 330 $entity->trustData()->save();
Chris@0 331 }
Chris@0 332 }
Chris@0 333 else {
Chris@0 334 $new_config->save(TRUE);
Chris@0 335 }
Chris@0 336 }
Chris@0 337 }
Chris@0 338
Chris@0 339 /**
Chris@0 340 * {@inheritdoc}
Chris@0 341 */
Chris@0 342 public function installCollectionDefaultConfig($collection) {
Chris@0 343 $storage = new ExtensionInstallStorage($this->getActiveStorages(StorageInterface::DEFAULT_COLLECTION), InstallStorage::CONFIG_INSTALL_DIRECTORY, $collection, $this->drupalInstallationAttempted(), $this->installProfile);
Chris@0 344 // Only install configuration for enabled extensions.
Chris@0 345 $enabled_extensions = $this->getEnabledExtensions();
Chris@0 346 $config_to_install = array_filter($storage->listAll(), function ($config_name) use ($enabled_extensions) {
Chris@0 347 $provider = Unicode::substr($config_name, 0, strpos($config_name, '.'));
Chris@0 348 return in_array($provider, $enabled_extensions);
Chris@0 349 });
Chris@0 350 if (!empty($config_to_install)) {
Chris@0 351 $this->createConfiguration($collection, $storage->readMultiple($config_to_install));
Chris@0 352 // Reset all the static caches and list caches.
Chris@0 353 $this->configFactory->reset();
Chris@0 354 }
Chris@0 355 }
Chris@0 356
Chris@0 357 /**
Chris@0 358 * {@inheritdoc}
Chris@0 359 */
Chris@0 360 public function setSourceStorage(StorageInterface $storage) {
Chris@0 361 $this->sourceStorage = $storage;
Chris@0 362 return $this;
Chris@0 363 }
Chris@0 364
Chris@0 365 /**
Chris@0 366 * Gets the configuration storage that provides the default configuration.
Chris@0 367 *
Chris@0 368 * @return \Drupal\Core\Config\StorageInterface|null
Chris@0 369 * The configuration storage that provides the default configuration.
Chris@0 370 * Returns null if the source storage has not been set.
Chris@0 371 */
Chris@0 372 public function getSourceStorage() {
Chris@0 373 return $this->sourceStorage;
Chris@0 374 }
Chris@0 375
Chris@0 376 /**
Chris@0 377 * Gets the configuration storage that provides the active configuration.
Chris@0 378 *
Chris@0 379 * @param string $collection
Chris@0 380 * (optional) The configuration collection. Defaults to the default
Chris@0 381 * collection.
Chris@0 382 *
Chris@0 383 * @return \Drupal\Core\Config\StorageInterface
Chris@0 384 * The configuration storage that provides the default configuration.
Chris@0 385 */
Chris@0 386 protected function getActiveStorages($collection = StorageInterface::DEFAULT_COLLECTION) {
Chris@0 387 if (!isset($this->activeStorages[$collection])) {
Chris@0 388 $this->activeStorages[$collection] = reset($this->activeStorages)->createCollection($collection);
Chris@0 389 }
Chris@0 390 return $this->activeStorages[$collection];
Chris@0 391 }
Chris@0 392
Chris@0 393 /**
Chris@0 394 * {@inheritdoc}
Chris@0 395 */
Chris@0 396 public function setSyncing($status) {
Chris@0 397 if (!$status) {
Chris@0 398 $this->sourceStorage = NULL;
Chris@0 399 }
Chris@0 400 $this->isSyncing = $status;
Chris@0 401 return $this;
Chris@0 402 }
Chris@0 403
Chris@0 404 /**
Chris@0 405 * {@inheritdoc}
Chris@0 406 */
Chris@0 407 public function isSyncing() {
Chris@0 408 return $this->isSyncing;
Chris@0 409 }
Chris@0 410
Chris@0 411 /**
Chris@0 412 * Finds pre-existing configuration objects for the provided extension.
Chris@0 413 *
Chris@0 414 * Extensions can not be installed if configuration objects exist in the
Chris@0 415 * active storage with the same names. This can happen in a number of ways,
Chris@0 416 * commonly:
Chris@0 417 * - if a user has created configuration with the same name as that provided
Chris@0 418 * by the extension.
Chris@0 419 * - if the extension provides default configuration that does not depend on
Chris@0 420 * it and the extension has been uninstalled and is about to the
Chris@0 421 * reinstalled.
Chris@0 422 *
Chris@0 423 * @return array
Chris@0 424 * Array of configuration object names that already exist keyed by
Chris@0 425 * collection.
Chris@0 426 */
Chris@0 427 protected function findPreExistingConfiguration(StorageInterface $storage) {
Chris@0 428 $existing_configuration = [];
Chris@0 429 // Gather information about all the supported collections.
Chris@0 430 $collection_info = $this->configManager->getConfigCollectionInfo();
Chris@0 431
Chris@0 432 foreach ($collection_info->getCollectionNames() as $collection) {
Chris@0 433 $config_to_create = array_keys($this->getConfigToCreate($storage, $collection));
Chris@0 434 $active_storage = $this->getActiveStorages($collection);
Chris@0 435 foreach ($config_to_create as $config_name) {
Chris@0 436 if ($active_storage->exists($config_name)) {
Chris@0 437 $existing_configuration[$collection][] = $config_name;
Chris@0 438 }
Chris@0 439 }
Chris@0 440 }
Chris@0 441 return $existing_configuration;
Chris@0 442 }
Chris@0 443
Chris@0 444 /**
Chris@0 445 * {@inheritdoc}
Chris@0 446 */
Chris@0 447 public function checkConfigurationToInstall($type, $name) {
Chris@0 448 if ($this->isSyncing()) {
Chris@0 449 // Configuration is assumed to already be checked by the config importer
Chris@0 450 // validation events.
Chris@0 451 return;
Chris@0 452 }
Chris@0 453 $config_install_path = $this->getDefaultConfigDirectory($type, $name);
Chris@0 454 if (!is_dir($config_install_path)) {
Chris@0 455 return;
Chris@0 456 }
Chris@0 457
Chris@0 458 $storage = new FileStorage($config_install_path, StorageInterface::DEFAULT_COLLECTION);
Chris@0 459
Chris@0 460 $enabled_extensions = $this->getEnabledExtensions();
Chris@0 461 // Add the extension that will be enabled to the list of enabled extensions.
Chris@0 462 $enabled_extensions[] = $name;
Chris@0 463 // Gets profile storages to search for overrides if necessary.
Chris@0 464 $profile_storages = $this->getProfileStorages($name);
Chris@0 465
Chris@0 466 // Check the dependencies of configuration provided by the module.
Chris@0 467 list($invalid_default_config, $missing_dependencies) = $this->findDefaultConfigWithUnmetDependencies($storage, $enabled_extensions, $profile_storages);
Chris@0 468 if (!empty($invalid_default_config)) {
Chris@0 469 throw UnmetDependenciesException::create($name, array_unique($missing_dependencies, SORT_REGULAR));
Chris@0 470 }
Chris@0 471
Chris@0 472 // Install profiles can not have config clashes. Configuration that
Chris@0 473 // has the same name as a module's configuration will be used instead.
Chris@0 474 if ($name != $this->drupalGetProfile()) {
Chris@0 475 // Throw an exception if the module being installed contains configuration
Chris@0 476 // that already exists. Additionally, can not continue installing more
Chris@0 477 // modules because those may depend on the current module being installed.
Chris@0 478 $existing_configuration = $this->findPreExistingConfiguration($storage);
Chris@0 479 if (!empty($existing_configuration)) {
Chris@0 480 throw PreExistingConfigException::create($name, $existing_configuration);
Chris@0 481 }
Chris@0 482 }
Chris@0 483 }
Chris@0 484
Chris@0 485 /**
Chris@0 486 * Finds default configuration with unmet dependencies.
Chris@0 487 *
Chris@0 488 * @param \Drupal\Core\Config\StorageInterface $storage
Chris@0 489 * The storage containing the default configuration.
Chris@0 490 * @param array $enabled_extensions
Chris@0 491 * A list of all the currently enabled modules and themes.
Chris@0 492 * @param \Drupal\Core\Config\StorageInterface[] $profile_storages
Chris@0 493 * An array of storage interfaces containing profile configuration to check
Chris@0 494 * for overrides.
Chris@0 495 *
Chris@0 496 * @return array
Chris@0 497 * An array containing:
Chris@0 498 * - A list of configuration that has unmet dependencies.
Chris@0 499 * - An array that will be filled with the missing dependency names, keyed
Chris@0 500 * by the dependents' names.
Chris@0 501 */
Chris@0 502 protected function findDefaultConfigWithUnmetDependencies(StorageInterface $storage, array $enabled_extensions, array $profile_storages = []) {
Chris@0 503 $missing_dependencies = [];
Chris@0 504 $config_to_create = $this->getConfigToCreate($storage, StorageInterface::DEFAULT_COLLECTION, '', $profile_storages);
Chris@0 505 $all_config = array_merge($this->configFactory->listAll(), array_keys($config_to_create));
Chris@0 506 foreach ($config_to_create as $config_name => $config) {
Chris@0 507 if ($missing = $this->getMissingDependencies($config_name, $config, $enabled_extensions, $all_config)) {
Chris@0 508 $missing_dependencies[$config_name] = $missing;
Chris@0 509 }
Chris@0 510 }
Chris@0 511 return [
Chris@0 512 array_intersect_key($config_to_create, $missing_dependencies),
Chris@0 513 $missing_dependencies,
Chris@0 514 ];
Chris@0 515 }
Chris@0 516
Chris@0 517 /**
Chris@0 518 * Validates an array of config data that contains dependency information.
Chris@0 519 *
Chris@0 520 * @param string $config_name
Chris@0 521 * The name of the configuration object that is being validated.
Chris@0 522 * @param array $data
Chris@0 523 * Configuration data.
Chris@0 524 * @param array $enabled_extensions
Chris@0 525 * A list of all the currently enabled modules and themes.
Chris@0 526 * @param array $all_config
Chris@0 527 * A list of all the active configuration names.
Chris@0 528 *
Chris@0 529 * @return bool
Chris@0 530 * TRUE if all dependencies are present, FALSE otherwise.
Chris@0 531 */
Chris@0 532 protected function validateDependencies($config_name, array $data, array $enabled_extensions, array $all_config) {
Chris@0 533 if (!isset($data['dependencies'])) {
Chris@0 534 // Simple config or a config entity without dependencies.
Chris@0 535 list($provider) = explode('.', $config_name, 2);
Chris@0 536 return in_array($provider, $enabled_extensions, TRUE);
Chris@0 537 }
Chris@0 538
Chris@0 539 $missing = $this->getMissingDependencies($config_name, $data, $enabled_extensions, $all_config);
Chris@0 540 return empty($missing);
Chris@0 541 }
Chris@0 542
Chris@0 543 /**
Chris@0 544 * Returns an array of missing dependencies for a config object.
Chris@0 545 *
Chris@0 546 * @param string $config_name
Chris@0 547 * The name of the configuration object that is being validated.
Chris@0 548 * @param array $data
Chris@0 549 * Configuration data.
Chris@0 550 * @param array $enabled_extensions
Chris@0 551 * A list of all the currently enabled modules and themes.
Chris@0 552 * @param array $all_config
Chris@0 553 * A list of all the active configuration names.
Chris@0 554 *
Chris@0 555 * @return array
Chris@0 556 * A list of missing config dependencies.
Chris@0 557 */
Chris@0 558 protected function getMissingDependencies($config_name, array $data, array $enabled_extensions, array $all_config) {
Chris@0 559 $missing = [];
Chris@0 560 if (isset($data['dependencies'])) {
Chris@0 561 list($provider) = explode('.', $config_name, 2);
Chris@0 562 $all_dependencies = $data['dependencies'];
Chris@0 563
Chris@0 564 // Ensure enforced dependencies are included.
Chris@0 565 if (isset($all_dependencies['enforced'])) {
Chris@0 566 $all_dependencies = array_merge($all_dependencies, $data['dependencies']['enforced']);
Chris@0 567 unset($all_dependencies['enforced']);
Chris@0 568 }
Chris@0 569 // Ensure the configuration entity type provider is in the list of
Chris@0 570 // dependencies.
Chris@0 571 if (!isset($all_dependencies['module']) || !in_array($provider, $all_dependencies['module'])) {
Chris@0 572 $all_dependencies['module'][] = $provider;
Chris@0 573 }
Chris@0 574
Chris@0 575 foreach ($all_dependencies as $type => $dependencies) {
Chris@0 576 $list_to_check = [];
Chris@0 577 switch ($type) {
Chris@0 578 case 'module':
Chris@0 579 case 'theme':
Chris@0 580 $list_to_check = $enabled_extensions;
Chris@0 581 break;
Chris@0 582 case 'config':
Chris@0 583 $list_to_check = $all_config;
Chris@0 584 break;
Chris@0 585 }
Chris@0 586 if (!empty($list_to_check)) {
Chris@0 587 $missing = array_merge($missing, array_diff($dependencies, $list_to_check));
Chris@0 588 }
Chris@0 589 }
Chris@0 590 }
Chris@0 591
Chris@0 592 return $missing;
Chris@0 593 }
Chris@0 594
Chris@0 595 /**
Chris@0 596 * Gets the list of enabled extensions including both modules and themes.
Chris@0 597 *
Chris@0 598 * @return array
Chris@0 599 * A list of enabled extensions which includes both modules and themes.
Chris@0 600 */
Chris@0 601 protected function getEnabledExtensions() {
Chris@0 602 // Read enabled extensions directly from configuration to avoid circular
Chris@0 603 // dependencies on ModuleHandler and ThemeHandler.
Chris@0 604 $extension_config = $this->configFactory->get('core.extension');
Chris@0 605 $enabled_extensions = (array) $extension_config->get('module');
Chris@0 606 $enabled_extensions += (array) $extension_config->get('theme');
Chris@0 607 // Core can provide configuration.
Chris@0 608 $enabled_extensions['core'] = 'core';
Chris@0 609 return array_keys($enabled_extensions);
Chris@0 610 }
Chris@0 611
Chris@0 612 /**
Chris@0 613 * Gets the profile storage to use to check for profile overrides.
Chris@0 614 *
Chris@0 615 * The install profile can override module configuration during a module
Chris@0 616 * install. Both the install and optional directories are checked for matching
Chris@0 617 * configuration. This allows profiles to override default configuration for
Chris@0 618 * modules they do not depend on.
Chris@0 619 *
Chris@0 620 * @param string $installing_name
Chris@0 621 * (optional) The name of the extension currently being installed.
Chris@0 622 *
Chris@0 623 * @return \Drupal\Core\Config\StorageInterface[]|null
Chris@0 624 * Storages to access configuration from the installation profile. If we're
Chris@0 625 * installing the profile itself, then it will return an empty array as the
Chris@0 626 * profile storage should not be used.
Chris@0 627 */
Chris@0 628 protected function getProfileStorages($installing_name = '') {
Chris@0 629 $profile = $this->drupalGetProfile();
Chris@0 630 $profile_storages = [];
Chris@0 631 if ($profile && $profile != $installing_name) {
Chris@0 632 $profile_path = $this->drupalGetPath('module', $profile);
Chris@0 633 foreach ([InstallStorage::CONFIG_INSTALL_DIRECTORY, InstallStorage::CONFIG_OPTIONAL_DIRECTORY] as $directory) {
Chris@0 634 if (is_dir($profile_path . '/' . $directory)) {
Chris@0 635 $profile_storages[] = new FileStorage($profile_path . '/' . $directory, StorageInterface::DEFAULT_COLLECTION);
Chris@0 636 }
Chris@0 637 }
Chris@0 638 }
Chris@0 639 return $profile_storages;
Chris@0 640 }
Chris@0 641
Chris@0 642 /**
Chris@0 643 * Gets an extension's default configuration directory.
Chris@0 644 *
Chris@0 645 * @param string $type
Chris@0 646 * Type of extension to install.
Chris@0 647 * @param string $name
Chris@0 648 * Name of extension to install.
Chris@0 649 *
Chris@0 650 * @return string
Chris@0 651 * The extension's default configuration directory.
Chris@0 652 */
Chris@0 653 protected function getDefaultConfigDirectory($type, $name) {
Chris@0 654 return $this->drupalGetPath($type, $name) . '/' . InstallStorage::CONFIG_INSTALL_DIRECTORY;
Chris@0 655 }
Chris@0 656
Chris@0 657 /**
Chris@0 658 * Wrapper for drupal_get_path().
Chris@0 659 *
Chris@0 660 * @param $type
Chris@0 661 * The type of the item; one of 'core', 'profile', 'module', 'theme', or
Chris@0 662 * 'theme_engine'.
Chris@0 663 * @param $name
Chris@0 664 * The name of the item for which the path is requested. Ignored for
Chris@0 665 * $type 'core'.
Chris@0 666 *
Chris@0 667 * @return string
Chris@0 668 * The path to the requested item or an empty string if the item is not
Chris@0 669 * found.
Chris@0 670 */
Chris@0 671 protected function drupalGetPath($type, $name) {
Chris@0 672 return drupal_get_path($type, $name);
Chris@0 673 }
Chris@0 674
Chris@0 675 /**
Chris@0 676 * Gets the install profile from settings.
Chris@0 677 *
Chris@0 678 * @return string|null
Chris@0 679 * The name of the installation profile or NULL if no installation profile
Chris@0 680 * is currently active. This is the case for example during the first steps
Chris@0 681 * of the installer or during unit tests.
Chris@0 682 */
Chris@0 683 protected function drupalGetProfile() {
Chris@0 684 return $this->installProfile;
Chris@0 685 }
Chris@0 686
Chris@0 687 /**
Chris@0 688 * Wrapper for drupal_installation_attempted().
Chris@0 689 *
Chris@0 690 * @return bool
Chris@0 691 * TRUE if a Drupal installation is currently being attempted.
Chris@0 692 */
Chris@0 693 protected function drupalInstallationAttempted() {
Chris@0 694 return drupal_installation_attempted();
Chris@0 695 }
Chris@0 696
Chris@0 697 }