diff --git a/core/lib/Drupal/Core/Config/ConfigFactory.php b/core/lib/Drupal/Core/Config/ConfigFactory.php index 289141d..0869bbe 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactory.php +++ b/core/lib/Drupal/Core/Config/ConfigFactory.php @@ -72,20 +72,13 @@ class ConfigFactory implements ConfigFactoryInterface, EventSubscriberInterface protected $typedConfigManager; /** - * An array of config factory override objects grouped by priority. + * An array of config factory override objects ordered by priority. * * @var array */ protected $configFactoryOverrides = array(); /** - * An array of sorted config factory override objects. - * - * @var array|\Drupal\Core\Config\ConfigFactoryOverrideInterface[] - */ - protected $sortedConfigFactoryOverrides; - - /** * Constructs the Config factory. * * @param \Drupal\Core\Config\StorageInterface $storage @@ -238,7 +231,7 @@ public function loadMultiple(array $names) { */ protected function loadModuleOverrides(array $names) { $overrides = array(); - foreach ($this->getSortedConfigFactoryOverrides() as $override) { + foreach ($this->configFactoryOverrides as $override) { // Existing overrides take precedence since these will have been added // by events with a higher priority. $overrides = NestedArray::mergeDeepArray(array($override->loadOverrides($names), $overrides), TRUE); @@ -414,27 +407,8 @@ static function getSubscribedEvents() { /** * {@inheritdoc} */ - public function addOverride(ConfigFactoryOverrideInterface $config_factory_override, $priority) { - $this->configFactoryOverrides[$priority][] = $config_factory_override; - // Force the overrides to be re-sorted. - $this->sortedConfigFactoryOverrides = NULL; - } - - /** - * {@inheritdoc} - */ - public function getSortedConfigFactoryOverrides() { - if (!isset($this->sortedConfigFactoryOverrides)) { - // Sort the override objects according to priority. - krsort($this->configFactoryOverrides); - // Merge nested override objects from $this->configFactoryOverrides into - // $this->sortedConfigFactoryOverrides. - $this->sortedConfigFactoryOverrides = array(); - foreach ($this->configFactoryOverrides as $overrides) { - $this->sortedConfigFactoryOverrides = array_merge($this->sortedConfigFactoryOverrides, $overrides); - } - } - return $this->sortedConfigFactoryOverrides; + public function addOverride(ConfigFactoryOverrideInterface $config_factory_override) { + $this->configFactoryOverrides[] = $config_factory_override; } } diff --git a/core/lib/Drupal/Core/Config/ConfigFactoryInterface.php b/core/lib/Drupal/Core/Config/ConfigFactoryInterface.php index 7380bb7..be656ea 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactoryInterface.php +++ b/core/lib/Drupal/Core/Config/ConfigFactoryInterface.php @@ -192,21 +192,12 @@ public function getLanguageConfigName($langcode, $name); public function listAll($prefix = ''); /** - * Adds config factory overrides. + * Adds config factory override services. * * @param \Drupal\Core\Config\ConfigFactoryOverrideInterface $config_factory_override - * The config factory override to add. - * @param int $priority - * Priority of the config factory override. + * The config factory override service to add. It is added at the end of the + * priority list (lower priority relative to existing ones). */ - public function addOverride(ConfigFactoryOverrideInterface $config_factory_override, $priority); - - /** - * Returns the sorted array of config factory override objects. - * - * @return array|\Drupal\Core\Config\ConfigFactoryOverrideInterface[] - * An array of config factory override objects. - */ - public function getSortedConfigFactoryOverrides(); + public function addOverride(ConfigFactoryOverrideInterface $config_factory_override); } diff --git a/core/lib/Drupal/Core/Config/ConfigFactoryOverridePass.php b/core/lib/Drupal/Core/Config/ConfigFactoryOverridePass.php index beae669..261edbe 100644 --- a/core/lib/Drupal/Core/Config/ConfigFactoryOverridePass.php +++ b/core/lib/Drupal/Core/Config/ConfigFactoryOverridePass.php @@ -24,10 +24,33 @@ class ConfigFactoryOverridePass implements CompilerPassInterface { */ public function process(ContainerBuilder $container) { $manager = $container->getDefinition('config.factory'); + $services = array(); foreach ($container->findTaggedServiceIds('config.factory.override') as $id => $attributes) { $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; - $manager->addMethodCall('addOverride', array(new Reference($id), $priority)); + $services[] = array('id' => $id, 'priority' => $priority); } + usort($services, array($this, 'compareServicePriorities')); + foreach ($services as $service) { + $manager->addMethodCall('addOverride', array(new Reference($service['id']))); + } + } + + /** + * Compares services by priority for ordering. + * + * @param array $a + * Service to compare. + * @param array $b + * Service to compare. + * @return int + * Relative order of services to be used with usort. Higher priorities come + * first. + */ + private function compareServicePriorities($a, $b) { + if ($a['priority'] == $b['priority']) { + return 0; + } + return ($a['priority'] > $b['priority']) ? -1 : 1; } } diff --git a/core/modules/config/tests/config_override/lib/Drupal/config_override/ConfigOverriderLowPriority.php b/core/modules/config/tests/config_override/lib/Drupal/config_override/ConfigOverriderLowPriority.php index 6bf8ee1..2b563e9 100644 --- a/core/modules/config/tests/config_override/lib/Drupal/config_override/ConfigOverriderLowPriority.php +++ b/core/modules/config/tests/config_override/lib/Drupal/config_override/ConfigOverriderLowPriority.php @@ -23,9 +23,9 @@ public function loadOverrides($names) { if (in_array('system.site', $names)) { $overrides = array('system.site' => array( + 'name' => 'Should not apply because of higher priority listener', // This override should apply because it is not overridden by the // higher priority listener. - 'name' => 'Should not apply because of higher priority listener', 'slogan' => 'Yay for overrides!', ) );