Drupal\Core\Settings is a singleton class.
The self::$instance is initialized from within the constructor.

As part of #2354475: [meta] Refactor the installer, (multi)site management, and pre-container bootstrap, I would like to turn all or most of the singletons into well-behaved objects with injected dependencies.
I could start a rant about the singleton here, but this is sufficiently discussed all over the web already.

As a preparation, we should stop setting Settings::$instance from the constructor.
It is a bad idea anyway, and it makes it unnecessarily harder to refactor anything.

This could all be done at once as part of #2354475: [meta] Refactor the installer, (multi)site management, and pre-container bootstrap, but I think it is better to split as much as possible out into preparation tasks.

A follow-up will be #2363353: Refactor / split up \Drupal\Core\Site\Settings singleton, and introduce other multisite-related classes..

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

donquixote’s picture

Oh-oh.
There is a number of code places where an instance of Settings is being passed around, and then $settings->get(..) or $settings->getHashSalt() is being called.
Unfortunately this is bogus, because Settings::get() is a static method, and it will look into self::$instance->storage, not into $this->storage.

CacheFactoryTest::testCacheFactoryWithDefaultSettings()
CacheFactoryTest::testCacheFactoryWithCustomizedDefaultBackend()
CacheFactoryTest::testCacheFactoryWithSpecifiedPerBinBackend()
SettingsTest::setUp()
SettingsTest::testGetHashSaltEmpty()
ReverseProxyMiddlewareTest::testNoProxy()
ReverseProxyMiddlewareTest::testReverseProxyEnabled()

This is why I think that static and non-static methods should, if possible, not live together in the same class.
Having Settings and SettingsInstance be separate classes would allow Settings::get($key) and $settings->get($key) be two separate methods.

("Separation of concerns" should be reason enough to keep them separate, but this might not convince everyone. The motivation above is more practical and direct.)

donquixote’s picture

Status: Active » Needs review
FileSize
42.79 KB

Re #1 I should clarify: The "new Settings" happens only in the above mentioned test classes.
However, $settings->get() and similar happens in a number of non-test classes. They all show up in the patch.

Patch:

  • Introduce SettingsInterface, implemented by SettingsInstance.
  • Use SettingsInterface / SettingsInstance for Settings::$instance.
    Introduce Settings::setCreateInstance() to replace the set-instance mechanic in Settings::__construct().
    This involves a number of changes in other classes.
  • Introduce SettingsNotInitializedException and PlaceholderSettings.
  • New unit test: SettingsNotInitializedTest.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

marvil07’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll

This patch cleans up and make it harder to make mistakes, it looks good!

Sadly, it has been a while, so it does not apply anymore, so making as NW for now.

rasikap’s picture

Assigned: Unassigned » rasikap
arunkumark’s picture

Hi,

I have re rolled patch for Drupal 8.x- 2.x version.

arunkumark’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 7: settings_not_write_Self_instance_2363881_7.patch, failed testing.

rasikap’s picture

Status: Needs work » Needs review
FileSize
19.8 KB

Was not able to apply patch. Fixed the error found.

Status: Needs review » Needs work

The last submitted patch, 10: 2363881_10.patch, failed testing.

rasikap’s picture

Assigned: rasikap » Unassigned
Manuel Garcia’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
donquixote’s picture

Just saying:
Now that Drupal 8 has been released, we need to be careful if this change causes any BC issues.

This would apply to contrib or custom code that use this low-level settings API.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs issue summary update

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Think this needs to be relooked at to see if still a valid task for D10.
Tagging for IS for any remaining tasks

Please do not just reroll

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.