diff -u b/core/includes/install.inc b/core/includes/install.inc --- b/core/includes/install.inc +++ b/core/includes/install.inc @@ -412,7 +412,10 @@ if ($system_versions = drupal_get_schema_versions('system')) { KeyValueFactory::get('system.module.schema')->set('system', max($system_versions)); } - module_config()->set('system', 0)->save(); + config('system.module') + ->set('system', 0) + ->setSorter('module_config_sort') + ->save(); // Clear out module list and hook implementation statics before calling // system_rebuild_theme_data(). diff -u b/core/includes/module.inc b/core/includes/module.inc --- b/core/includes/module.inc +++ b/core/includes/module.inc @@ -135,7 +135,7 @@ } } else { - $enabled_modules = module_config()->get(); + $enabled_modules = config('system.module')->get(); _system_list_warm('module', 'system', 'core/modules/system.module', TRUE); $module_data = system_rebuild_module_data(); $bootstrap_list = array(); @@ -167,7 +167,7 @@ // locations in the file system. The ordering here must also be // consistent with the one used in module_implements(). $enabled_themes = config('system.theme')->get(); - $enabled_modules = module_config()->get(); + $enabled_modules = config('system.module')->get(); // It's important to retrieve these first because the rebuild calls // reset the system_list static. _system_list_warm('module', 'system', 'core/modules/system.module', TRUE); @@ -453,7 +453,7 @@ $schema_store = KeyValueFactory::get('system.module.schema'); $weight_store = KeyValueFactory::get('system.module.weight'); foreach ($module_list as $module) { - $config = module_config(); + $config = config('system.module'); // Only process modules that are not already enabled. $enabled = TRUE; if (!$schema_store->get($module)) { @@ -467,6 +467,7 @@ if (!$enabled) { $config ->set($module, $weight) + ->setSorter('module_config_sort') ->save(); if ($weight) { $weight_store->delete($module_list); @@ -583,7 +584,7 @@ $store = KeyValueFactory::get('system.module.weight'); foreach ($module_list as $module) { if (module_exists($module)) { - $config = module_config(); + $config = config('system.module'); module_load_install($module); module_invoke($module, 'disable'); if ($weight = $config->get($module)) { @@ -591,6 +592,7 @@ } $config ->clear($module) + ->setSorter('module_config_sort') ->save(); $invoke_modules[] = $module; watchdog('system', '%module module disabled.', array('%module' => $module), WATCHDOG_INFO); @@ -1149,21 +1151,27 @@ -/** - * Retrieves the module list configuration object. - * - * @return Drupal\Core\Config\ModuleConfig - * A configuration object. - */ -function module_config() { - return drupal_container()->get('config.factory')->get('system.module', 'Drupal\Core\Config\ModuleConfig')->load(); -} - function module_set_weight($module, $weight) { - $config = module_config(); + $config = config('system.module'); if ($config->get($module) !== NULL) { $config ->set($module, $weight) + ->setSorter('module_config_sort') ->save(); } else { KeyValueFactory::get('system.module.weight')->set($module, $weight); } } + +function module_config_sort($data) { + $sort = array(); + foreach ($data as $name => $weight) { + // We can't use the sign directly because + (ASCII 43) is before + // - (ASCII 45). So negative nubmers get a 0, non-negative numbers + // a 1 prefix. + $prefix = (int) ($weight >= 0); + // PHP_INT_MAX is at most 19 characters so make every number equally + // 19 digits long. + $sort[] = $prefix . sprintf('%019d', abs($weight)) . $name; + } + array_multisort($sort, SORT_STRING, $data); + return $data; +} \ No newline at end of file diff -u b/core/includes/theme.inc b/core/includes/theme.inc --- b/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1394,7 +1394,7 @@ drupal_clear_css_cache(); $config = config('system.theme'); foreach ($theme_list as $key) { - // The value is not used, it needs to be consisent with + // The value is not used, it needs to be consistent with // config('system.module') which uses the value for weight so set all of // them to the same value. 1 looks like 'enabled' so let's use that. $config->set($key, 1); diff -u b/core/includes/update.inc b/core/includes/update.inc --- b/core/includes/update.inc +++ b/core/includes/update.inc @@ -28,7 +28,7 @@ */ function update_fix_compatibility() { foreach (array('module', 'theme') as $type) { - $config = $type == 'module' ? module_config() : config('system.theme'); + $config = config("system.$type"); $save = FALSE; foreach ($config->get() as $name => $weight) { if (update_check_incompatibility($name, $type)) { @@ -37,6 +37,9 @@ } } if ($save) { + if ($type == 'module') { + $config->setSorter('module_config_sort'); + } $config->save(); } } @@ -176,7 +179,7 @@ // web server, an exception will be thrown, halting the update. drupal_install_config_directories(); $configs = array( - 'module' => module_config(), + 'module' => config('system.module'), 'theme' => config('system.theme'), ); $schema_store = KeyValueFactory::get('system.module.schema'); @@ -198,6 +201,7 @@ } } } + $configs['module']->setSorter('module_config_sort'); foreach ($configs as $config) { $config->save(); } @@ -400,7 +404,10 @@ } } // Enable the module with a weight of 0. - module_config()->set($module, 0)->save(); + config('system.module') + ->set($module, 0) + ->setSorter('module_config_sort') + ->save(); // Change the schema version from SCHEMA_UNINSTALLED to 0, so any module // updates since the module's inception are executed in a core upgrade. $store->set($module, 0); diff -u b/core/lib/Drupal/Core/Config/Config.php b/core/lib/Drupal/Core/Config/Config.php --- b/core/lib/Drupal/Core/Config/Config.php +++ b/core/lib/Drupal/Core/Config/Config.php @@ -64,10 +64,21 @@ */ protected $eventDispatcher; - + /** + * TRUE if the configuration object does not need sorting on save. + * + * @var bool + */ protected $sorted = FALSE; /** + * The callable that sorts the data before save. + * + * @var callable + */ + protected $sorter; + + /** * Constructs a configuration object. * * @param string $name @@ -314,7 +325,7 @@ */ public function save() { if (!$this->sorted) { - $this->sortByKey($this->data); + call_user_func($this->sorter ? $this->sorter : array($this, 'sortByKey'), $this->data); } $this->storage->write($this->name, $this->data); $this->isNew = FALSE; @@ -340,6 +351,14 @@ } /** + * @param callable + */ + function setSorter($sorter) { + $this->sorter = $sorter; + return $this; + } + + /** * Sorts all keys in configuration data. * * Ensures that re-inserted keys appear in the same location as before, in @@ -350,13 +369,14 @@ * @param array $data * An associative array to sort recursively by key name. */ - public function sortByKey(array &$data) { + public function sortByKey(array $data) { ksort($data); foreach ($data as &$value) { if (is_array($value)) { - $this->sortByKey($value); + $value = $this->sortByKey($value); } } + return $data; } /** reverted: --- b/core/lib/Drupal/Core/Config/ConfigFactory.php +++ a/core/lib/Drupal/Core/Config/ConfigFactory.php @@ -57,13 +57,11 @@ * * @param string $name * The name of the configuration object to construct. - * @param $class_name - * Optional. The name of the class handling this configuration object. * * @return Drupal\Core\Config\Config * A configuration object with the given $name. */ + public function get($name) { - public function get($name, $class_name = 'Drupal\Core\Config\Config') { global $conf; // @todo Caching the instantiated objects per name might cut off a fair @@ -84,7 +82,7 @@ // @todo The decrease of CPU time is interesting, since that means that // ContainerBuilder involves plenty of function calls (which are known to // be slow in PHP). + $config = new Config($name, $this->storage, $this->eventDispatcher); - $config = new $class_name($name, $this->storage, $this->eventDispatcher); return $config->init(); } reverted: --- b/core/lib/Drupal/Core/Config/ModuleConfig.php +++ /dev/null @@ -1,34 +0,0 @@ -data) { - $sort = array(); - foreach ($this->data as $name => $weight) { - // We can't use the sign directly because + (ASCII 43) is before - // - (ASCII 45). So negative nubmers get a 0, non-negative numbers - // a 1 prefix. - $prefix = (int) ($weight >= 0); - // PHP_INT_MAX is at most 19 characters so make every number equally - // 19 digits long. - $sort[] = $prefix . sprintf('%019d', abs($weight)) . $name; - } - array_multisort($sort, SORT_STRING, $this->data); - } - $this->storage->write($this->name, $this->data); - $this->isNew = FALSE; - $this->notify('save'); - return $this; - } - -} diff -u b/core/modules/field/field.module b/core/modules/field/field.module --- b/core/modules/field/field.module +++ b/core/modules/field/field.module @@ -364,7 +364,7 @@ } if ($non_deleted) { // Calling module_exists() here leads to infinite recursion. - if (module_config()->get('field_ui') !== NULL) { + if (config('system.module')->get('field_ui') !== NULL) { $explanation = t('Field type(s) in use - see Field list', array('@fields-page' => url('admin/reports/fields'))); } else { diff -u b/core/modules/help/lib/Drupal/help/Tests/HelpTest.php b/core/modules/help/lib/Drupal/help/Tests/HelpTest.php --- b/core/modules/help/lib/Drupal/help/Tests/HelpTest.php +++ b/core/modules/help/lib/Drupal/help/Tests/HelpTest.php @@ -109,7 +109,7 @@ protected function getModuleList() { $modules = array(); $module_data = system_rebuild_module_data(); - foreach (module_config()->get() as $module => $data) { + foreach (config('system.module')->get() as $module => $data) { if (file_exists($module_data[$module]->filename) && function_exists($module . '_help')) { $modules[$module] = $module_data[$module]->info['name']; } diff -u b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php --- b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php @@ -275,11 +275,13 @@ $config = config('system.module'); foreach ($config->get() as $module => $data) { - if (!in_array($module, $required_modules)) { + if (!in_array($module, $modules)) { $config->clear($module); } } - $config->save(); + $config + ->setSorter('module_config_sort') + ->save(); } } diff -u b/core/modules/system/system.module b/core/modules/system/system.module --- b/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -2810,7 +2810,7 @@ $modules[$module]->weight = $weight; } // Enabled status and weights. - foreach (module_config()->get() as $module => $weight) { + foreach (config('system.module')->get() as $module => $weight) { $modules[$module]->status = 1; $modules[$module]->weight = $weight; } @@ -2828,7 +2828,7 @@ $type = 'module'; // It is not impossible a function resets the modules cache. Keep it. $modules = $modules_cache['data']; - foreach (module_config()->get() as $module => $weight) { + foreach (config('system.module')->get() as $module => $weight) { $function = $module .'_system_info_alter'; if (function_exists($function)) { foreach ($modules as $record) { diff -u b/core/modules/update/lib/Drupal/update/Tests/UpdateContribTest.php b/core/modules/update/lib/Drupal/update/Tests/UpdateContribTest.php --- b/core/modules/update/lib/Drupal/update/Tests/UpdateContribTest.php +++ b/core/modules/update/lib/Drupal/update/Tests/UpdateContribTest.php @@ -210,7 +210,9 @@ $config->clear($theme); } } - $config->save(); + $config + ->setSorter('module_config_sort') + ->save(); // Define the initial state for core and the test contrib themes. $system_info = array(