Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Drupal\Component\Utility\Settings
- is a Drupalism and thus has no place in Component.
- is not a utility
- depends on the site directory from which settings.php is read
Proposed solution
- Move the
Settings
class intoDrupal\Core\Site\Settings
.
Comment | File | Size | Author |
---|---|---|---|
#52 | settings.site_.52.patch | 40.15 KB | sun |
#46 | settings.site_.46.patch | 40.15 KB | sun |
#44 | settings.site_.44.patch | 40.16 KB | sun |
#43 | settings.site_.43.patch | 40.13 KB | sun |
#40 | drupal8-settings_site-2208475-40.patch | 40.07 KB | Jalandhar |
Comments
Comment #1
sunComment #2
andrei.dincu CreditAttribution: andrei.dincu commentedComment #3
andrei.dincu CreditAttribution: andrei.dincu commentedComment #4
andrei.dincu CreditAttribution: andrei.dincu commentedComment #6
tstoecklerCan 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
before I believe it.
Comment #7
sun@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.Comment #8
sunTrying again (from scratch)
Comment #10
sunOopsie ;)
Comment #11
tstoecklerI don't want to hold this up, as I'm not married very much to \Drupal\Component, but FTR I disagree 1000% with #7.
Comment #12
dawehnerIs there any reason why we need to specify both the factory_class and class?
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.
Oh, we don't move the tests here.
Comment #13
sunSorry 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 currentSettings
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; theSettings
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.Comment #14
dawehnerIf you are strict you should als move Xss::filter out of components, as external code couples
It would be interesting to know how many places actually require settings() to be a singleton and not being part of the DIC.
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.
Comment #15
ParisLiakos CreditAttribution: ParisLiakos commentedi also completely disagree with this
Well then thats drupal's problem and not the component's?
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
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)
Comment #16
tstoecklerRe ParisLiakos:
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 aCoreSettings extends ComponentSettings
class.Comment #17
ParisLiakos CreditAttribution: ParisLiakos commentedah 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..
Comment #18
ianthomas_ukI'd question why this is RTBC with two people disagreeing with the concept, but that's a bit irrelevant given
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.
Comment #19
alexpottMy 2 pence on this issue:
Comment #20
pwolanin CreditAttribution: pwolanin commentedneeds re-roll for #2219009: Improve DX of Settings class
Comment #21
sunUpdated new imports introduced by #2219009: Improve DX of Settings class
Comment #23
sunUpdated missing instance in twig.engine.
Can't wait for all PHP files to have a .php file extension.
Comment #24
XanoRe-roll because of the following conflict:
Comment #25
XanoFixed one occurrence and test annotations.
Comment #27
ianthomas_uk25: drupal_2208475_25.patch queued for re-testing.
Comment #29
ianthomas_uk25: drupal_2208475_25.patch queued for re-testing.
Comment #30
sunComment #32
sunUpdated SessionHandler.
Comment #33
tstoecklerApparently it does, but how does the "use" statement work before the autoloader has been loaded?
Comment #34
ianthomas_ukThe 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.
Comment #35
tstoecklerRe #34. Ahh thanks a lot for that explanation. Now I can check the "Learn something new everyday" box for today :-)
Comment #36
sunPhpStorage
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.
Comment #37
sunMerged 8.x + Converted new instances.
Comment #38
dawehnerStill fine (under the assumption that we actually want the move).
Fixing the phpunit test is fine.
Comment #40
Jalandhar CreditAttribution: Jalandhar commentedUpdating patch with re-roll. Please review.
Comment #41
sunThanks, @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 :-)
Comment #42
webchickNeeds a small re-roll. Weird that this was ever in Component to begin with.
That's a bit unfortunate. I'd love to keep index.php lean and mean and copy/pastable without use statements.
Comment #43
sunMerged 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.
Comment #44
sunMerged + re-rolled for diff context conflicts (ahead of time)
Comment #46
sunMerged 8.x.
Comment #48
sun46: settings.site_.46.patch queued for re-testing.
Comment #49
damiankloip CreditAttribution: damiankloip commentedMh, looks like this was added without a use for some reason, in https://drupal.org/node/2219009#comment-8605979 - Thanks!
Looks good.
Comment #50
xjmThere 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. :)
Comment #51
alexpottNeeds a reroll.
Comment #52
sunMerged 8.x.
Comment #53
alexpottCommitted 8a56782 and pushed to 8.x. Thanks!
Remove all these on commit because fixing unused use statements is silly.
Comment #55
alexpottFixed change notices
Comment #57
ianthomas_ukFor the record my objections to this are now moot, as #1848266: Convert Diff into a proper, PSR-0-compatible PHP component and #2210631: Move PhpStorageFactory to the Core namespace have been committed.