10000 - · symfony/symfony@aca93ff · GitHub
[go: up one dir, main page]

Skip to content

Commit aca93ff

Browse files
Jean-Berunicolas-grekas
authored andcommitted
-
1 parent bdb8d6a commit aca93ff

File tree

8 files changed

+49
-46
lines changed

8 files changed

+49
-46
lines changed

src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php

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

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

1742-
$attributes = '';
1743-
if ($value && $this->container->hasDefinition($value) && $class = $this->container->findDefinition($value)->getClass()) {
1744-
$attributes = sprintf('#[\%s(class: %s)] ', \Closure::class, $this->dumpValue($class, $interpolate));
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);
17451751
}
17461752

1747-
return sprintf("%sfunction ()%s {\n %s\n }", $attributes, $returnedType, $code);
1753+
return sprintf("%sfunction ()%s {\n %s\n }", $attribute, $returnedType, $code);
17481754
}
17491755

17501756
if ($value instanceof IteratorArgument) {

src/Symfony/Component/DependencyInjection/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 => #[\Closure(class: 'FooClass')] 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

src/Symfony/Component/DependencyInjection/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(#[\Closure(class: 'Foo')] 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
}

src/Symfony/Component/DependencyInjection/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' => #[\Closure(class: 'stdClass')] 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' => #[\Closure(class: 'stdClass')] 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' => #[\Closure(class: 'stdClass')] 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' => #[\Closure(class: 'stdClass')] 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' => #[\Closure(class: 'stdClass')] 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

src/Symfony/Component/DependencyInjection/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 => #[\Closure(class: 'stdClass')] 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 => #[\Closure(class: 'stdClass')] function () {
65+
}, 2 => #[\Closure(name: 'foo3', class: 'stdClass')] function () {
6666
return ($this->privates['foo3'] ?? null);
6767
}];
6868
$instance->iter = new RewindableGenerator(function () {

src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function getCalledListeners(Request $request = null): array
188188
public function getNotCalledListeners(Request $request = null): array
189189
{
190190
try {
191-
$allListeners = $this->dispatcher instanceof EventDispatcher ? $this->getListenersWithPriorityByReflection() : $this->getListenersWithPriority();
191+
$allListeners = $this->dispatcher instanceof EventDispatcher ? $this->getListenersWithPriority() : $this->getListenersWithoutPriority();
192192
} catch (\Exception $e) {
193193
$this->logger?->info('An exception was thrown while getting the uncalled listeners.', ['exception' => $e]);
194194

@@ -212,15 +212,12 @@ public function getNotCalledListeners(Request $request = null): array
212212
$notCalled = [];
213213

214214
foreach ($allListeners as $eventName => $listeners) {
215-
foreach ($listeners as $listener) {
216-
$priority = $listener['priority'] ?? null;
217-
$listener = $listener['listener'];
218-
215+
foreach ($listeners as [$listener, $priority]) {
219216
if (!\in_array($listener, $calledListeners, true)) {
220217
if (!$listener instanceof WrappedListener) {
221-
$listener = new WrappedListener($listener, null, $this->stopwatch, $this);
218+
$listener = new WrappedListener($listener, null, $this->stopwatch, $this, $priority);
222219
}
223-
$notCalled[] = $listener->getInfo($eventName, $priority);
220+
$notCalled[] = $listener->getInfo($eventName);
224221
}
225222
}
226223
}
@@ -353,30 +350,31 @@ private function sortNotCalledListeners(array $a, array $b): int
353350
return 1;
354351
}
355352

356-
private function getListenersWithPriorityByReflection(): array
353+
private function getListenersWithPriority(): array
357354
{
358355
$result = [];
359356

360-
$allListeners = (new \ReflectionProperty(EventDispatcher::class, 'listeners'))->getValue($this->dispatcher);
357+
$allListeners = new \ReflectionProperty(EventDispatcher::class, 'listeners');
358+
$allListeners->setAccessible(true);
361359

362-
foreach ($allListeners as $eventName => $eventListeners) {
363-
foreach ($eventListeners as $priority => $priorityListeners) {
364-
foreach ($priorityListeners as $listener) {
365-
$result[$eventName][] = ['listener' => $listener, 'priority' => $priority];
360+
foreach ($allListeners->getValue($this->dispatcher) as $eventName => $listenersByPriority) {
361+
foreach ($listenersByPriority as $priority => $listeners) {
362+
foreach ($listeners as $listener) {
363+
$result[$eventName][] = [$listener, $priority];
366364
}
367365
}
368366
}
369367

370368
return $result;
371369
}
372370

373-
private function getListenersWithPriority(): array
371+
private function getListenersWithoutPriority(): array
374372
{
375373
$result = [];
376374

377375
foreach ($this->getListeners() as $eventName => $listeners) {
378376
foreach ($listeners as $listener) {
379-
$result[$eventName][] = ['listener' => $listener];
377+
$result[$eventName][] = [$listener, null];
380378
}
381379
}
382380

src/Symfony/Component/EventDispatcher/Debug/WrappedListener.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@ final class WrappedListener
2929
private Stopwatch $stopwatch;
3030
private ?EventDispatcherInterface $dispatcher;
3131
private string $pretty;
32+
private string $callableRef;
3233
private ClassStub|string $stub;
3334
private ?int $priority = null;
3435
private static bool $hasClassStub;
3536

36-
public function __construct(callable|array $listener, ?string $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null)
37+
public function __construct(callable|array $listener, ?string $name, Stopwatch $stopwatch, EventDispatcherInterface $dispatcher = null, int $priority = null)
3738
{
3839
$this->listener = $listener;
3940
$this->optimizedListener = $listener instanceof \Closure ? $listener : (\is_callable($listener) ? \Closure::fromCallable($listener) : null);
4041
$this->stopwatch = $stopwatch;
4142
$this->dispatcher = $dispatcher;
43+
$this->priority = $priority;
4244

4345
if (\is_array($listener)) {
44-
$this->name = $this->getNameFromArray($listener);
46+
[$this->name, $this->callableRef] = $this->parseListener($listener);
4547
$this->pretty = $this->name.'::'.$listener[1];
48+
$this->callableRef .= '::'.$listener[1];
4649
} elseif ($listener instanceof \Closure) {
4750
$r = new \ReflectionFunction($listener);
4851
if (str_contains($r->name, '{closure}')) {
@@ -58,6 +61,7 @@ public function __construct(callable|array $listener, ?string $name, Stopwatch $
5861
} else {
5962
$this->name = get_debug_type($listener);
6063
$this->pretty = $this->name.'::__invoke';
64+
$this->callableRef = \get_class($listener).'::__invoke';
6165
}
6266

6367
if (null !== $name) {
@@ -87,18 +91,13 @@ public function getPretty(): string
8791
return $this->pretty;
8892
}
8993

90-
/**
91-
* @param int|null $priority
92-
*/
93-
public function getInfo(string $eventName /*, int $priority = null */): array
94+
public function getInfo(string $eventName): array
9495
{
95-
$this->stub ??= self::$hasClassStub ? new ClassStub($this->pretty.'()', $this->pretty) : $this->pretty.'()';
96-
97-
$priority = \func_num_args() > 1 && null !== func_get_arg(1) ? (int) func_get_arg(1) : null;
96+
$this->stub ??= self::$hasClassStub ? new ClassStub($this->pretty.'()', $this->callableRef ?? $this->listener) : $this->pretty.'()';
9897

9998
return [
10099
'event' => $eventName,
101-
'priority' => $priority ?? $this->priority ?? $this->dispatcher?->getListenerPriority($eventName, $this->listener),
100+
'priority' => $this->priority ??= $this->dispatcher?->getListenerPriority($eventName, $this->listener),
102101
'pretty' => $this->pretty,
103102
'stub' => $this->stub,
104103
];
@@ -109,7 +108,7 @@ public function __invoke(object $event, string $eventName, EventDispatcherInterf
109108
$dispatcher = $this->dispatcher ?: $dispatcher;
110109

111110
$this->called = true;
112-
$this->priority = $dispatcher->getListenerPriority($eventName, $this->listener);
111+
$this->priority ??= $dispatcher->getListenerPriority($eventName, $this->listener);
113112

114113
$e = $this->stopwatch->start($this->name, 'event_listener');
115114

@@ -124,20 +123,20 @@ public function __invoke(object $event, string $eventName, EventDispatcherInterf
124123
}
125124
}
126125

127-
private function getNameFromArray(array $listener): string
126+
private function parseListener(array $listener): array
128127
{
129128
if ($listener[0] instanceof \Closure) {
130129
foreach ((new \ReflectionFunction($listener[0]))->getAttributes(\Closure::class) as $attribute) {
131-
if ($name = $attribute->getArguments()['class'] ?? false) {
132-
return $name;
130+
if ($name = $attribute->getArguments()['name'] ?? false) {
131+
return [$name, $attribute->getArguments()['class'] ?? $name];
133132
}
134133
}
135134
}
136135

137136
if (\is_object($listener[0])) {
138-
return get_debug_type($listener[0]);
137+
return [get_debug_type($listener[0]), \get_class($listener[0])];
139138
}
140139

141-
return $listener[0];
140+
return [$listener[0], $listener[0]];
142141
}
143142
}

src/Symfony/Component/EventDispatcher/Tests/Debug/WrappedListenerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function provideListenersToDescribe()
4040
[\Closure::fromCallable([new FooListener(), 'listen']), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen'],
4141
[\Closure::fromCallable(['Symfony\Component\EventDispatcher\Tests\Debug\FooListener', 'listenStatic']), 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listenStatic'],
4242
[\Closure::fromCallable(function () {}), 'closure'],
43-
[[#[\Closure(class: FooListener::class)] static fn () => new FooListener(), 'listen'], 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen'],
43+
[[#[\Closure(name: FooListener::class)] static fn () => new FooListener(), 'listen'], 'Symfony\Component\EventDispatcher\Tests\Debug\FooListener::listen'],
4444
];
4545
}
4646
}

0 commit comments

Comments
 (0)
0