Drupal\Component\Utility\Settings

  1. is a Drupalism and thus has no place in Component.
  2. is not a utility
  3. depends on the site directory from which settings.php is read

Proposed solution

  1. Move the Settings class into Drupal\Core\Site\Settings.
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

sun’s picture

andrei.dincu’s picture

Assigned: Unassigned » andrei.dincu
andrei.dincu’s picture

andrei.dincu’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 3: move-settings-2208475-3.patch, failed testing.

tstoeckler’s picture

Can you explaing in what way Settings is a Drupalism? I am quite astonished by that statement, to be honest.

It is quite a common pattern for PHP projects to include a PHP file that exposes configuration variables in file scope that are then exported to global variables for use in the rest of the project. I.e. $conf in Drupal <=7. Settings is just a wrapper around that which nicely implements the Singleton pattern, because that's exactly what such variables are.

I can totally see other projects using this in exactly the same way we do in drupal_settings_initialize().

I obviously have nothing against *extending* the Component Settings in Core to provide things like Settings::getHashSalt() as discussed in #2207585: Find a new OO home for drupal_get_hash_salt() but I would like some rationale for

Drupal\Component\Utility\Settings is a Drupalism and thus has no place in Component.

before I believe it.

sun’s picture

@tstoeckler:

Yes, the approach of an application-level configuration file is common for PHP projects. But not Drupal's concept of settings.php. And neither Drupal's current usage of Settings within the application layer. I'm sorry for putting it this way, but believing that any other project would find our approach of establishing application-level configuration useful in any way is a bit naive. ;)

There are many different approaches, and even Symfony itself has a concept built-in, but in the end, the approach is always custom to the application. → Settings and settings.php is a 100% Drupalism.

On top, the current Settings "singleton" is ridiculous. It's the anti-definition of a singleton. It's so messed up, we can be lucky that HEAD even works at all. → Even if there was a hypothetical point of sharing our approach with the outside world, this particular piece of nonsense is definitely not.

sun’s picture

Assigned: andrei.dincu » sun
Status: Needs work » Needs review
FileSize
23.54 KB

Trying again (from scratch)

Status: Needs review » Needs work

The last submitted patch, 8: settings.site_.8.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
FileSize
23.64 KB
410 bytes

Oopsie ;)

tstoeckler’s picture

I don't want to hold this up, as I'm not married very much to \Drupal\Component, but FTR I disagree 1000% with #7.

dawehner’s picture

  1. +++ b/core/core.services.yml
    @@ -128,8 +128,8 @@ services:
    +    class: Drupal\Core\Site\Settings
    +    factory_class: Drupal\Core\Site\Settings
    

    Is there any reason why we need to specify both the factory_class and class?

  2. +++ b/core/includes/bootstrap.inc
    @@ -2,7 +2,7 @@
    -use Drupal\Component\Utility\Settings;
    +use Drupal\Core\Site\Settings;
    

    Given the definition of components: "Drupal Components are independent libraries that do not depend on the rest of
    Drupal in order to function." I don't really see a reason why this should not be a component.

  3. +++ b/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageTest.php
    @@ -8,7 +8,7 @@
    diff --git a/core/tests/Drupal/Tests/Component/Utility/SettingsTest.php b/core/tests/Drupal/Tests/Component/Utility/SettingsTest.php
    
    index 7117bcb..ec51380 100644
    --- a/core/tests/Drupal/Tests/Component/Utility/SettingsTest.php
    
    --- a/core/tests/Drupal/Tests/Component/Utility/SettingsTest.php
    +++ b/core/tests/Drupal/Tests/Component/Utility/SettingsTest.php
    
    +++ b/core/tests/Drupal/Tests/Component/Utility/SettingsTest.php
    @@ -2,18 +2,18 @@
    - * Contains \Drupal\Tests\Component\Utility\SettingsTest.php
    + * Contains \Drupal\Tests\Core\Site\SettingsTest.php
      */
    ...
     namespace Drupal\Tests\Component\Utility;
    

    Oh, we don't move the tests here.

sun’s picture

FileSize
23.81 KB
651 bytes

Sorry for missing that. Moved PHPUnit test accordingly.

re #12.1: The registration of Settings as a service is not touched here. I agree it looks odd. To be investigated in #2199795: Make the Settings class prevent serialization of actual settings, in case that will revamp/fix the full Settings singleton implementation.

re #12.2: As mentioned in the OP and in #7, yes, we should place classes and components into Drupal\Component, IF they are fully decoupled, sensible, and useful for other PHP projects. However, the current Settings class fails on all points: (1) It claims to implement the singleton pattern, but isn't, because a true singleton is not possible in Drupal due to simpletests; the Settings class is re-instantiated all over place throughout core. (2) Every PHP project has its own concept for application level settings already, so there is no point to expose this utility class in Component. (3) We identified that it would make sense to enhance the class with specific methods in #2207585: Find a new OO home for drupal_get_hash_salt(), which are custom to Drupal.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

If you are strict you should als move Xss::filter out of components, as external code couples

s. However, the current Settings class fails on all points: (1) It claims to implement the singleton pattern, but isn't, because a true singleton is not possible in Drupal due to simpletests; the Settings class is re-instantiated all over place throughout core.

It would be interesting to know how many places actually require settings() to be a singleton and not being part of the DIC.

Every PHP project has its own concept for application level settings already, so there is no point to expose this utility class in Component.

Do you have examples how other projects solve things like this?, just asking curiosly.

I kinda agree that such a piece of code should be clearly marked as being part of core and not something really generic people should try to reuse.

ParisLiakos’s picture

i also completely disagree with this

It claims to implement the singleton pattern, but isn't, because a true singleton is not possible in Drupal due to simpletests; the Settings class is re-instantiated all over place throughout core.

Well then thats drupal's problem and not the component's?

Every PHP project has its own concept for application level settings already, so there is no point to expose this utility class in Component.

I want to use this component in my custom 100 lines of code website and Settings component sounds like a great candidate for that because it is lightweight. Why only other php projects can only re-use our components? any php codebase should be able to pull this from composer, not just other frameworks

We identified that it would make sense to enhance the class with specific methods in #2207585: Find a new OO home for drupal_get_hash_salt(), which are custom to Drupal.

I dont see whats wrong with settings()->get('hash_shalt')

Also if we are still doing this: moving this to Core namespace would require to move all components that require it to moved too. (DiffEngine and PhpStorage)

tstoeckler’s picture

Re ParisLiakos:

We identified that it would make sense to enhance the class with specific methods in #2207585: Find a new OO home for drupal_get_hash_salt(), which are custom to Drupal.

I dont see whats wrong with settings()->get('hash_shalt')

It was identified in the quoted issue that we want to add additional validation code to that getter, to throw an exception in case 'hash_salt' is empty.

As I referenced above, though:
A: I completely agree with you (you == ParisLiakos)
B: I don't see why we couldn't add that Settings::getHashSalt() in a CoreSettings extends ComponentSettings class.

ParisLiakos’s picture

ah i see..CoreSettings would work ..
Or check before even creating the settings object against all the required keys in drupal_settings_initialize and throw the exception there..

ianthomas_uk’s picture

Status: Reviewed & tested by the community » Needs work

I'd question why this is RTBC with two people disagreeing with the concept, but that's a bit irrelevant given

moving this to Core namespace would require to move all components that require it to moved too. (DiffEngine and PhpStorage)

would need to be resolved before this is committed.

I don't understand how this class "depends on the site directory from which settings.php is read". drupal_settings_initialize() depends on that, but you could create a new Settings instance with an array from wherever you wanted to (some tests create a Settings instance with their own custom arrays). It's a totally self contained class, so Component seems appropriate.

I'll put my thoughts about hash_salt on that issue.

alexpott’s picture

My 2 pence on this issue:

  • The use of Settings by Diff is a best spurious - solved by #1848266: Convert Diff into a proper, PSR-0-compatible PHP component
  • I think we should move of PhpStorageFactory to \Drupal\Core too
  • whilst there is nothing Drupal specific about the Settings implementation it is our Application's settings singleton so having this in Component is odd too - who is honestly going to want to reuse this.
pwolanin’s picture

Issue tags: +Needs reroll
sun’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
33.19 KB
10.31 KB

Updated new imports introduced by #2219009: Improve DX of Settings class

Status: Needs review » Needs work

The last submitted patch, 21: settings.site_.21.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
FileSize
33.56 KB
374 bytes

Updated missing instance in twig.engine.

Can't wait for all PHP files to have a .php file extension.

Xano’s picture

FileSize
33.75 KB

Re-roll because of the following conflict:

diff a/core/includes/session.inc b/core/includes/session.inc	(rejected hunks)
@@ -17,7 +17,7 @@
  */
 
 use Drupal\Component\Utility\Crypt;
-use Drupal\Component\Utility\Settings;
+use Drupal\Core\Site\Settings;
 use Drupal\Core\Session\UserSession;
 use Drupal\Core\Utility\Error;
 
Xano’s picture

FileSize
35.03 KB
2.53 KB

Fixed one occurrence and test annotations.

Status: Needs review » Needs work

The last submitted patch, 25: drupal_2208475_25.patch, failed testing.

ianthomas_uk’s picture

25: drupal_2208475_25.patch queued for re-testing.

The last submitted patch, 25: drupal_2208475_25.patch, failed testing.

ianthomas_uk’s picture

25: drupal_2208475_25.patch queued for re-testing.

sun’s picture

Status: Needs work » Needs review
FileSize
35.07 KB
2.93 KB
  1. Merged 8.x.
  2. Re-applied and polished #25.

Status: Needs review » Needs work

The last submitted patch, 30: settings.site_.30.patch, failed testing.

sun’s picture

Status: Needs work » Needs review
FileSize
35.59 KB
527 bytes

Updated SessionHandler.

tstoeckler’s picture

+++ b/index.php
@@ -8,6 +8,8 @@
+use Drupal\Core\Site\Settings;
+
 require_once __DIR__ . '/core/vendor/autoload.php';
 require_once __DIR__ . '/core/includes/bootstrap.inc';

Apparently it does, but how does the "use" statement work before the autoloader has been loaded?

ianthomas_uk’s picture

The use statement doesn't do anything immediately, it just says "Should you, at some point in the future, need a class called Settings, this is the one I mean." As long as you don't actually attempt to use the Settings class before the autoloader has been defined it should be fine.

BTW my concerns about this change have been addressed, but I'd still like to see the first two bullets of #19 done before this gets in, if only to avoid other Components from copying those two and depending on Core code.

tstoeckler’s picture

Re #34. Ahh thanks a lot for that explanation. Now I can check the "Learn something new everyday" box for today :-)

sun’s picture

FileSize
38.64 KB

PhpStorage was converted already, the only remaining use is in #1848266: Convert Diff into a proper, PSR-0-compatible PHP component — which is a complete rewrite anyway... so I wonder whether we really need to wait for that?

Updated 1 new instance + order the changed use statements.

sun’s picture

FileSize
40.03 KB

Merged 8.x + Converted new instances.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Still fine (under the assumption that we actually want the move).

Fixing the phpunit test is fine.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 37: settings.site_.37.patch, failed testing.

Jalandhar’s picture

Assigned: sun » Unassigned
Status: Needs work » Needs review
FileSize
40.07 KB

Updating patch with re-roll. Please review.

sun’s picture

Assigned: Unassigned » sun
Status: Needs review » Reviewed & tested by the community

Thanks, @Jalandhar. — Please don't unassign me though, as I'm actively maintaining the changes of my issues in sandbox branches. I have to merge HEAD anyway, in order to manually verify and confirm that a new patch of someone else is the same as my previous patch (which means extra work, as I would have re-rolled anyway). Issues that are "needs work" and which didn't see progress for at least two weeks would most likely appreciate re-rolls a lot more :-)

webchick’s picture

Status: Reviewed & tested by the community » Needs work

Needs a small re-roll. Weird that this was ever in Component to begin with.

+++ b/index.php
@@ -8,6 +8,8 @@
+use Drupal\Core\Site\Settings;
+

@@ -16,7 +18,7 @@
-  if (\Drupal\Component\Utility\Settings::get('rebuild_access', FALSE)) {
+  if (Settings::get('rebuild_access', FALSE)) {

That's a bit unfortunate. I'd love to keep index.php lean and mean and copy/pastable without use statements.

sun’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
40.13 KB

Merged 8.x.

@webchick: Yeah, that's pretty much a pipe-dream. #2016629: Refactor bootstrap to better utilize the kernel will add some more use statements, in order to make our front controllers properly use the kernel architecture.

sun’s picture

FileSize
40.16 KB

Merged + re-rolled for diff context conflicts (ahead of time)

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 44: settings.site_.44.patch, failed testing.

sun’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
40.15 KB

Merged 8.x.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 46: settings.site_.46.patch, failed testing.

sun’s picture

Status: Needs work » Needs review

46: settings.site_.46.patch queued for re-testing.

damiankloip’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/index.php
@@ -16,7 +18,7 @@
+  if (Settings::get('rebuild_access', FALSE)) {

Mh, looks like this was added without a use for some reason, in https://drupal.org/node/2219009#comment-8605979 - Thanks!

Looks good.

xjm’s picture

There are change records that will need to be corrected for this:
https://drupal.org/list-changes/published/drupal?keywords_description=Dr...

Please make sure they get updated when this goes in. :)

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs a reroll.

git ac https://drupal.org/files/issues/settings.site_.46.patch
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 41110  100 41110    0     0  23837      0  0:00:01  0:00:01 --:--:-- 34721
error: patch failed: core/includes/install.core.inc:1
error: core/includes/install.core.inc: patch does not apply
sun’s picture

Status: Needs work » Reviewed & tested by the community
FileSize
40.15 KB

Merged 8.x.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 8a56782 and pushed to 8.x. Thanks!

diff --git a/core/includes/update.inc b/core/includes/update.inc
index a7dabc5..694a8a9 100644
--- a/core/includes/update.inc
+++ b/core/includes/update.inc
@@ -14,7 +14,6 @@
 use Drupal\Core\Config\ConfigException;
 use Drupal\Core\DrupalKernel;
 use Drupal\Core\Page\DefaultHtmlPageRenderer;
-use Drupal\Core\Site\Settings;
 use Drupal\Core\Utility\Error;
 use Drupal\Component\Uuid\Uuid;
 use Drupal\Component\Utility\NestedArray;
diff --git a/core/lib/Drupal/Core/Authentication/Provider/Cookie.php b/core/lib/Drupal/Core/Authentication/Provider/Cookie.php
index 9b1f99d..167a0b0 100644
--- a/core/lib/Drupal/Core/Authentication/Provider/Cookie.php
+++ b/core/lib/Drupal/Core/Authentication/Provider/Cookie.php
@@ -9,7 +9,6 @@
 
 use Drupal\Core\Authentication\AuthenticationProviderInterface;
 use Drupal\Core\Session\SessionManagerInterface;
-use Drupal\Core\Site\Settings;
 use Symfony\Component\HttpFoundation\Request;
 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
 
diff --git a/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageTest.php b/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageTest.php
index 8e7d7c2..58cd7ca 100644
--- a/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageTest.php
+++ b/core/tests/Drupal/Tests/Component/PhpStorage/MTimeProtectedFileStorageTest.php
@@ -7,8 +7,6 @@
 
 namespace Drupal\Tests\Component\PhpStorage;
 
-use Drupal\Core\Site\Settings;
-
 /**
  * Tests the directory mtime based PHP loader implementation.
  */

Remove all these on commit because fixing unused use statements is silly.

  • Commit 8a56782 on 8.x by alexpott:
    Issue #2208475 by sun, Xano, Jalandhar, andrei.dincu: Move Settings into...
alexpott’s picture

Fixed change notices

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

ianthomas_uk’s picture