Closed (fixed)
Project:
Drupal core
Version:
11.x-dev
Component:
base system
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
22 Jan 2020 at 13:25 UTC
Updated:
5 Oct 2023 at 09:29 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
bonrita commentedComment #3
bonrita commentedComment #4
bonrita commentedComment #5
bonrita commentedComment #6
bonrita commentedComment #7
bonrita commentedComment #8
bonrita commentedComment #9
bonrita commentedComment #10
bonrita commentedComment #11
bonrita commentedComment #12
longwaveComment #13
bonrita commentedThis patch is not part of this issue. It is here to combine all the patches that patch the '\Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper' file so as to prevent conflicts.
It combines issues: 3108020, 3108039 and 2961380
Comment #15
bonrita commentedComment #16
bonrita commentedComment #17
bonrita commentedComment #18
bonrita commentedComment #25
cyberwolf commentedThe provided patch does not handle ServiceClosureArgument correctly. The class that will be using this argument, ServiceLocatorTrait, expects it to be a callable and passes itself as an argument when calling it. The return value should be the actual service. See ServiceLocatorTrait::get():
Instead, with the patch the service itself is dumped thus $this->factories[$id] is already the service.
I tried to modify OptimizedPhpArrayDumper a bit and use a closure for the factory instead, however dumping closures is not supported. So eventually I had to use an additional class for the factory. Unfortunately I had to use the static \Drupal::getContainer() in this class. Maybe better solutions for this problem exist, but I'm out of ideas at the moment. Symfony's PhpDumper has a lot more freedom and flexibility in this regards, as it can just just any possible PHP code.
Changes in OptimizedPhpArrayDumper:
The additional class:
Comment #26
longwaveComment #27
cyberwolf commentedI tought another approach, based on how 'private_service' is done. ServiceClosureArgument is dumped now to a stdClass and then in the container itself it's converted to a closure.
Attached a patch based on the 9.5.x branch.
I am not sure when / under which conditions Drupal\Component\DependencyInjection\PhpArrayContainer is used though, so I could not verify my changes in there.
Comment #28
cyberwolf commentedPatch in #27 contained a mistake, attaching a new one.
Comment #29
longwave#2909185: Replace ContainerAwareEventDispatcher with Symfony EventDispatcher also has a patch that handles this.
Comment #30
cyberwolf commentedThanks! I will have a closer look at the other patch, it seems pretty close to my last attempt but might contain some stuff I missed.
I am preparing unit tests as well.
Comment #31
cyberwolf commentedComment #32
cyberwolf commentedComment #33
cyberwolf commentedComment #34
longwaveThanks for adding the tests. This looks OK to me and will simplify swapping out the event dispatcher for Symfony's one.
Comment #35
alexpottI think we need a CR record here to tell people that we now support this. The patch looks good.
Comment #36
borisson_Added a draft change record, we should add an example in the change record of how this looks in a services file. I don't understand this enough to add a good example.
Comment #38
znerol commentedAdapted the example from https://symfony.com/doc/current/service_container/service_closures.html for the CR.
Comment #39
znerol commented#31 fails to apply, needs a reroll.
Comment #40
sagarchauhan commentedRerolled patch on #31
Comment #41
sagarchauhan commentedReuploading the patch after fixing issues.
Comment #42
znerol commentedNo substantial changes since #34, hence RTBC.
I removed the service declaration from the change record, since that isn't working with the custom
YamlFileLoaderDrupal is currently using. But for service definitions built programmatically, this patch still brings some benefits.Comment #43
znerol commented(also added a change record)
Comment #44
longwaveCommitted and pushed 92e703a69a to 11.x. Thanks!
Let's continue in #2909185: Replace ContainerAwareEventDispatcher with Symfony EventDispatcher which should become almost trivial now.
Comment #47
dpiJust created a new issue for following up on ServiceClosureArgument with private services: #3391860: ServiceClosureArgument with private service references original non-existent service