Problem/Motivation
For #2991683: Move configuration transformation API in \Drupal\Core\Config namespace we need a MemoryStorage to work with configuration without persisting it.
But it is also useful in a more general circumstance. A couple of contrib modules implemented their own already.
For example: config_provider (InMemoryStorage) and config_owner(MemoryConfigStorage)
In addition it is useful for testing code dealing with configuration storages for example in #3016429: Add a config storage copy utility trait and fix ConfigManager::createSnapshot.
Proposed resolution
Add \Drupal\Core\Config\MemoryStorage
.
Remaining tasks
write patch
review
commit
User interface changes
none
API changes
New class.
Data model changes
none
Comment | File | Size | Author |
---|---|---|---|
#32 | 3015886-32.patch | 5.85 KB | alexpott |
#32 | 28-32-interdiff.txt | 624 bytes | alexpott |
#28 | interdiff-3015886-27-28.txt | 607 bytes | bircher |
#28 | 3015886-28.patch | 5.85 KB | bircher |
#27 | 3015886-27.patch | 5.89 KB | alexpott |
Comments
Comment #2
bircherComment #3
Upchuk CreditAttribution: Upchuk as a volunteer commentedLooks good. Just some nit picks :)
Nitpick but why are you setting the config first and then checking if that is NULL? You could do in one op, like:
No need for an else here. You can just return the array_keys if the prefix is empty. And then just start looping if you do have a prefix.
Same here. You can just return, no need for an else.
Missing period.
!==
Left over comment?
Comment #4
bircherAddressed feeback
Comment #5
Upchuk CreditAttribution: Upchuk as a volunteer commentedLooks good, thanks for the fixes.
Comment #6
catchApologies for the pedantry. Still need to get my head around the issue this blocks before being able to do a proper review here.
Comment #7
bircher:D
Not necessarily having to understand the whole issue this blocks is one of the reasons I decided to split this off in its own issue.
I updated the issue summary to better point out to contrib modules that implement their own in-memory storage.
Comment #8
Upchuk CreditAttribution: Upchuk as a volunteer commentedFixing reference to use case memory storage.
Comment #9
alexpottThe only reason this is not marked as a risky test is because \Drupal\KernelTests\KernelTestBase::assertPostConditions makes an assertion. Should do
$this->markTestSkipped('MemoryStorage can not be invalid');
Comment #10
Upchuk CreditAttribution: Upchuk as a volunteer commentedHere you go.
Comment #11
borisson_Back to rtbc, @alexpott's remark was fixed in #10.
Comment #12
alexpottI'm not sure about injecting $config into constructor. If this is only for colections then we could do
instead since you can access protected properties in that case.
If it is not then we should add additional test coverage in MemoryStorageTest of pre-populating MemoryStorage from something that implements ArrayAccess.
Comment #13
bircherMakes sense. I like it to be an internal implementation detail.
Comment #14
bircherthen again, for testing purposes it may be interesting to create the memory storage with an ArrayAccess object to spy on the content to make assertions, and in that case we should also make sure that empty collections are unset.
Comment #15
bircherlunchbreak coding, attached the patch.
Comment #16
alexpott@bircher for testing purposes you don't need to have something unnecessary on the constructor. You can use reflection is you want. Personally I prefer #13
Comment #17
bircherOk, I can agree with that.
All one has to do in a test is:
Attached is the version from #13 but with unsetting unused collections to make the comparison easier.
Comment #18
Upchuk CreditAttribution: Upchuk as a volunteer commented@bircher ok but then if we instantiate like this, it means that
$this->config
will no longer keep track of multiple collections but we will have a new instance for another collection (which duplicates whatever already exists inside). This also means that things like$this->config[$this->collection][$name]
don't make sense anymore across the methods.This is a big change from the initial implementation in which the storage kept track internally of its collections.
So this wont allow pre-populating the memory storage with multiple collections that are different (for example mimic'ing translations).
Comment #19
Upchuk CreditAttribution: Upchuk as a volunteer commentedAah, nevermind. Long day :)
Comment #20
Upchuk CreditAttribution: Upchuk as a volunteer commentedYupp, looks good to me!
Comment #22
Upchuk CreditAttribution: Upchuk as a volunteer commentedHad failed for some reason. Back to RTBC
Comment #23
catch..
So I think we should replace the second comment with exactly what's in the first. Leaving RTBC because that could be done on commit.
Comment #24
alexpottSome improvements though using array functions over foreach and addressed #23.
Also I've discovered an inconsistency in how CRUD operations occur on empty collections. This inconsistency extends to \Drupal\Core\Config\FileStorage and \Drupal\Core\Config\DatabaseStorage so it does not block this issue. Opened #3020964: Empty collection config CRUD operations are inconsistent across the backend.
Comment #25
alexpottHmmm... as a result of working on #3020964: Empty collection config CRUD operations are inconsistent across the backend. I've got a few more fixes. We might want to land that first.
Comment #26
alexpottAh patch attached now passes the latest test on #3020964: Empty collection config CRUD operations are inconsistent across the backend.
Comment #27
alexpottRerolled now that #3020964: Empty collection config CRUD operations are inconsistent across the backend. has landed.
Comment #28
bircherI wanted to put it back to RTBC as I thing the changes by Alex are great. But then I noticed when reviewing that since we now delete the collection key from the array object we don't have to check it for being empty. And if it so happens to not be unset but empty it will be unset later and still false is returned. So attached one more simplification.
Comment #29
mpotter CreditAttribution: mpotter at Phase2 commentedReviewed #28 and looks good here.
Comment #30
alexpottComment #31
catchDoesn't this result in $data being an unused variable? I personally don't mind this vs. an array_keys() call but I think we had a load of patches cleaning this up at one point.
Comment #32
alexpott@catch not according to PHPStorm - it's when you don't use $name that you'd get an unused var.
At some point I guess this can turn into an array_filter() but not until we require at least PHP 5.6.
Let's make the change - these variables to think about is always a good thing.
Comment #33
catchCommitted 243e979 and pushed to 8.7.x. Thanks!