FeaturesConfigInstaller.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Drupal\features;
  3. use Drupal\Core\Config\ConfigInstaller;
  4. use Drupal\Core\Config\ConfigInstallerInterface;
  5. use Drupal\Core\Config\ConfigFactoryInterface;
  6. use Drupal\Core\Config\StorageInterface;
  7. use Drupal\Core\Config\TypedConfigManagerInterface;
  8. use Drupal\Core\Config\ConfigManagerInterface;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. /**
  11. * Class for customizing the test for pre existing configuration.
  12. *
  13. * Decorates the ConfigInstaller with findPreExistingConfiguration() modified
  14. * to allow Feature modules to be installed.
  15. */
  16. class FeaturesConfigInstaller extends ConfigInstaller {
  17. /**
  18. * The configuration installer.
  19. *
  20. * @var \Drupal\Core\Config\ConfigInstallerInterface
  21. */
  22. protected $configInstaller;
  23. /**
  24. * The features manager.
  25. *
  26. * @var \Drupal\features\FeaturesManagerInterface
  27. */
  28. protected $featuresManager;
  29. /**
  30. * Constructs the configuration installer.
  31. *
  32. * @param \Drupal\Core\Config\ConfigInstallerInterface $config_installer
  33. * The configuration installer.
  34. * @param \Drupal\features\FeaturesManagerInterface $features_manager
  35. * The features manager.
  36. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  37. * The configuration factory.
  38. * @param \Drupal\Core\Config\StorageInterface $active_storage
  39. * The active configuration storage.
  40. * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
  41. * The typed configuration manager.
  42. * @param \Drupal\Core\Config\ConfigManagerInterface $config_manager
  43. * The configuration manager.
  44. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
  45. * The event dispatcher.
  46. */
  47. public function __construct(ConfigInstallerInterface $config_installer, FeaturesManagerInterface $features_manager, ConfigFactoryInterface $config_factory, StorageInterface $active_storage, TypedConfigManagerInterface $typed_config, ConfigManagerInterface $config_manager, EventDispatcherInterface $event_dispatcher) {
  48. $this->configInstaller = $config_installer;
  49. $this->featuresManager = $features_manager;
  50. list($major, $minor, ) = explode('.', \Drupal::VERSION);
  51. if ($major == 8 && $minor > 2) {
  52. // D8.3 added the %install_profile% argument.
  53. $install_profile = drupal_get_profile();
  54. parent::__construct($config_factory, $active_storage, $typed_config, $config_manager, $event_dispatcher, $install_profile);
  55. }
  56. else {
  57. parent::__construct($config_factory, $active_storage, $typed_config, $config_manager, $event_dispatcher);
  58. }
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function findPreExistingConfiguration(StorageInterface $storage) {
  64. // Override
  65. // Drupal\Core\Config\ConfigInstaller::findPreExistingConfiguration().
  66. // Allow config that already exists coming from Features.
  67. $features_config = array_keys($this->featuresManager->listExistingConfig());
  68. // Map array so we can use isset instead of in_array for faster access.
  69. $features_config = array_combine($features_config, $features_config);
  70. $existing_configuration = array();
  71. // Gather information about all the supported collections.
  72. $collection_info = $this->configManager->getConfigCollectionInfo();
  73. foreach ($collection_info->getCollectionNames() as $collection) {
  74. $config_to_create = array_keys($this->getConfigToCreate($storage, $collection));
  75. $active_storage = $this->getActiveStorages($collection);
  76. foreach ($config_to_create as $config_name) {
  77. if ($active_storage->exists($config_name)) {
  78. // Test if config is part of a Feature package.
  79. if (!isset($features_config[$config_name])) {
  80. $existing_configuration[$collection][] = $config_name;
  81. }
  82. }
  83. }
  84. }
  85. return $existing_configuration;
  86. }
  87. /**
  88. * Creates configuration in a collection based on the provided list.
  89. *
  90. * @param string $collection
  91. * The configuration collection.
  92. * @param array $config_to_create
  93. * An array of configuration data to create, keyed by name.
  94. */
  95. public function createConfiguration($collection, array $config_to_create) {
  96. return parent::createConfiguration($collection, $config_to_create);
  97. }
  98. }