Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public static function create(ReverseContainer $reverseContainer, callable $call

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

if ($callback instanceof \Closure && !str_contains(($r = new \ReflectionFunction($callback))->name, '{closure')) {
$callback = [$r->getClosureThis() ?? (\PHP_VERSION_ID >= 80111 ? $r->getClosureCalledClass() : $r->getClosureScopeClass())?->name, $r->name];
$callback[0] ?: $callback = $r->name;
}

if (\is_object($callback)) {
if (null === $id = $reverseContainer->getId($callback)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,58 @@ public function __invoke(CacheItem $item)
$this->assertSame('@computation_service', $msg->getCallback());
$this->assertSame($computationService, $msg->findCallback($reverseContainer));
}

public function testCreateWithNonAnonymousClosureBoundToInstance()
{
$pool = new ArrayAdapter();
$item = $pool->getItem('foo');
$item->set(234);

$computationService = new class {
public function compute(CacheItem $item)
{
return 123;
}

public static function staticCompute(CacheItem $item)
{
return 123;
}
};

$container = new Container();
$container->set('computation_service', $computationService);
$container->set('cache_pool', $pool);

$reverseContainer = new ReverseContainer($container, new ServiceLocator([]));

$closure = $computationService->compute(...);
$msg = EarlyExpirationMessage::create($reverseContainer, $closure, $item, $pool);
$this->assertSame(['@computation_service', 'compute'], $msg->getCallback());

$closure = $computationService::staticCompute(...);
$msg = EarlyExpirationMessage::create($reverseContainer, $closure, $item, $pool);
$this->assertSame([$computationService::class, 'staticCompute'], $msg->getCallback());

$msg = EarlyExpirationMessage::create($reverseContainer, var_dump(...), $item, $pool);
$this->assertSame('var_dump', $msg->getCallback());

$this->assertSame('cache_pool', $msg->getPool());
}

public function testCreateWithAnonymousClosure()
{
$pool = new ArrayAdapter();
$item = $pool->getItem('foo');
$item->set(234);

$container = new Container();
$container->set('cache_pool', $pool);

$reverseContainer = new ReverseContainer($container, new ServiceLocator([]));

$msg = EarlyExpirationMessage::create($reverseContainer, static fn () => 123, $item, $pool);

$this->assertNull($msg);
}
}