Skip to content

Commit a7a1325

Browse files
Merge branch '7.3' into 7.4
* 7.3: [Cache] Fix accepting named closures as early-expiration callbacks [Mime] Update mime types [Yaml] Align unquoted multiline scalar parsing with spec for comments work around limitation in JsonResponse when the data is null do not use recipient phone numbers as sender e-mail addresses [Dotenv] DotenvDumpCommand cannot be internal
2 parents 222aabc + 8095968 commit a7a1325

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

Messenger/EarlyExpirationMessage.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public static function create(ReverseContainer $reverseContainer, callable $call
3131

3232
$pool = $reverseContainer->getId($pool);
3333

34+
if ($callback instanceof \Closure && !($r = new \ReflectionFunction($callback))->isAnonymous()) {
35+
$callback = [$r->getClosureThis() ?? $r->getClosureCalledClass()?->name, $r->name];
36+
$callback[0] ?: $callback = $r->name;
37+
}
38+
3439
if (\is_object($callback)) {
3540
if (null === $id = $reverseContainer->getId($callback)) {
3641
return null;

Tests/Messenger/EarlyExpirationMessageTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,58 @@ public function __invoke(CacheItem $item)
5757
$this->assertSame('@computation_service', $msg->getCallback());
5858
$this->assertSame($computationService, $msg->findCallback($reverseContainer));
5959
}
60+
61+
public function testCreateWithNonAnonymousClosureBoundToInstance()
62+
{
63+
$pool = new ArrayAdapter();
64+
$item = $pool->getItem('foo');
65+
$item->set(234);
66+
67+
$computationService = new class {
68+
public function compute(CacheItem $item)
69+
{
70+
return 123;
71+
}
72+
73+
public static function staticCompute(CacheItem $item)
74+
{
75+
return 123;
76+
}
77+
};
78+
79+
$container = new Container();
80+
$container->set('computation_service', $computationService);
81+
$container->set('cache_pool', $pool);
82+
83+
$reverseContainer = new ReverseContainer($container, new ServiceLocator([]));
84+
85+
$closure = $computationService->compute(...);
86+
$msg = EarlyExpirationMessage::create($reverseContainer, $closure, $item, $pool);
87+
$this->assertSame(['@computation_service', 'compute'], $msg->getCallback());
88+
89+
$closure = $computationService::staticCompute(...);
90+
$msg = EarlyExpirationMessage::create($reverseContainer, $closure, $item, $pool);
91+
$this->assertSame([$computationService::class, 'staticCompute'], $msg->getCallback());
92+
93+
$msg = EarlyExpirationMessage::create($reverseContainer, var_dump(...), $item, $pool);
94+
$this->assertSame('var_dump', $msg->getCallback());
95+
96+
$this->assertSame('cache_pool', $msg->getPool());
97+
}
98+
99+
public function testCreateWithAnonymousClosure()
100+
{
101+
$pool = new ArrayAdapter();
102+
$item = $pool->getItem('foo');
103+
$item->set(234);
104+
105+
$container = new Container();
106+
$container->set('cache_pool', $pool);
107+
108+
$reverseContainer = new ReverseContainer($container, new ServiceLocator([]));
109+
110+
$msg = EarlyExpirationMessage::create($reverseContainer, static fn () => 123, $item, $pool);
111+
112+
$this->assertNull($msg);
113+
}
60114
}

0 commit comments

Comments
 (0)