Skip to content

Commit a784aa3

Browse files
minor #45184 [DependencyInjection][EventDispatcher] Avoid instantiating not called listeners when tracing (Jean-Beru)
This PR was squashed before being merged into the 6.1 branch. Discussion ---------- [DependencyInjection][EventDispatcher] Avoid instantiating not called listeners when tracing | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | Fix #43658 | License | MIT | Doc PR | This PR removes useless listeners instantiation when `TraceableEventDispatcher::getNotCalledListeners()` is called. Even if this PR is in WIP, I created it to allow comments from `@nicolas`-grekas (and anyone interested in of course). Commits ------- d4e60e9d96 [DependencyInjection][EventDispatcher] Avoid instantiating not called listeners when tracing
2 parents e495e55 + 285c662 commit a784aa3

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

Dumper/PhpDumper.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,18 @@ private function dumpValue(mixed $value, bool $interpolate = true): string
17391739

17401740
$code = sprintf('return %s;', $code);
17411741

1742-
return sprintf("function ()%s {\n %s\n }", $returnedType, $code);
1742+
$attribute = '';
1743+
if ($value) {
1744+
$attribute = 'name: '.$this->dumpValue((string) $value, $interpolate);
1745+
1746+
if ($this->container->hasDefinition($value) && ($class = $this->container->findDefinition($value)->getClass()) && $class !== (string) $value) {
1747+
$attribute .= ', class: '.$this->dumpValue($class, $interpolate);
1748+
}
1749+
1750+
$attribute = sprintf('#[\Closure(%s)] ', $attribute);
1751+
}
1752+
1753+
return sprintf("%sfunction ()%s {\n %s\n }", $attribute, $returnedType, $code);
17431754
}
17441755

17451756
if ($value instanceof IteratorArgument) {

Tests/Fixtures/php/services10_as_files.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class getClosureService extends ProjectServiceContainer
2929
{
3030
$container->services['closure'] = $instance = new \stdClass();
3131

32-
$instance->closures = [0 => function () use ($container): ?\stdClass {
32+
$instance->closures = [0 => #[\Closure(name: 'foo', class: 'FooClass')] function () use ($container): ?\stdClass {
3333
return ($container->services['foo'] ?? null);
3434
}];
3535

Tests/Fixtures/php/services_closure_argument_compiled.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected function getFooService()
5555
*/
5656
protected function getServiceClosureService()
5757
{
58-
return $this->services['service_closure'] = new \Bar(function () {
58+
return $this->services['service_closure'] = new \Bar(#[\Closure(name: 'foo', class: 'Foo')] function () {
5959
return ($this->services['foo'] ?? ($this->services['foo'] = new \Foo()));
6060
});
6161
}

Tests/Fixtures/php/services_locator.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ protected function getBarServiceService()
7070
*/
7171
protected function getFooServiceService()
7272
{
73-
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(['bar' => function () {
73+
return $this->services['foo_service'] = new \Symfony\Component\DependencyInjection\ServiceLocator(['bar' => #[\Closure(name: 'bar_service', class: 'stdClass')] function () {
7474
return ($this->services['bar_service'] ?? $this->getBarServiceService());
75-
}, 'baz' => function (): \stdClass {
75+
}, 'baz' => #[\Closure(name: 'baz_service', class: 'stdClass')] function (): \stdClass {
7676
return ($this->privates['baz_service'] ?? ($this->privates['baz_service'] = new \stdClass()));
7777
}, 'nil' => function () {
7878
return NULL;
@@ -116,7 +116,7 @@ protected function getTranslator_Loader3Service()
116116
*/
117117
protected function getTranslator1Service()
118118
{
119-
return $this->services['translator_1'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_1' => function () {
119+
return $this->services['translator_1'] = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_1' => #[\Closure(name: 'translator.loader_1', class: 'stdClass')] function () {
120120
return ($this->services['translator.loader_1'] ?? ($this->services['translator.loader_1'] = new \stdClass()));
121121
}]));
122122
}
@@ -128,7 +128,7 @@ protected function getTranslator1Service()
128128
*/
129129
protected function getTranslator2Service()
130130
{
131-
$this->services['translator_2'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_2' => function () {
131+
$this->services['translator_2'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_2' => #[\Closure(name: 'translator.loader_2', class: 'stdClass')] function () {
132132
return ($this->services['translator.loader_2'] ?? ($this->services['translator.loader_2'] = new \stdClass()));
133133
}]));
134134

@@ -144,7 +144,7 @@ protected function getTranslator2Service()
144144
*/
145145
protected function getTranslator3Service()
146146
{
147-
$this->services['translator_3'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_3' => function () {
147+
$this->services['translator_3'] = $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator(new \Symfony\Component\DependencyInjection\ServiceLocator(['translator.loader_3' => #[\Closure(name: 'translator.loader_3', class: 'stdClass')] function () {
148148
return ($this->services['translator.loader_3'] ?? ($this->services['translator.loader_3'] = new \stdClass()));
149149
}]));
150150

Tests/Fixtures/php/services_uninitialized_ref.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ protected function getBarService()
5858
$instance->foo1 = ($this->services['foo1'] ?? null);
5959
$instance->foo2 = null;
6060
$instance->foo3 = ($this->privates['foo3'] ?? null);
61-
$instance->closures = [0 => function () {
61+
$instance->closures = [0 => #[\Closure(name: 'foo1', class: 'stdClass')] function () {
6262
return ($this->services['foo1'] ?? null);
63-
}, 1 => function () {
63+
}, 1 => #[\Closure(name: 'foo2')] function () {
6464
return null;
65-
}, 2 => function () {
65+
}, 2 => #[\Closure(name: 'foo3', class: 'stdClass')] function () {
6666
return ($this->privates['foo3'] ?? null);
6767
}];
6868
$instance->iter = new RewindableGenerator(function () {

0 commit comments

Comments
 (0)