setCallbackWrapper example

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

        $reverseContainer = new ReverseContainer($containernew ServiceLocator([]));

        $bus = $this->createMock(MessageBusInterface::class);

        $dispatcher = new EarlyExpirationDispatcher($bus$reverseContainer);

        $saveResult = null;
        $pool->setCallbackWrapper(function Dcallable $callback, CacheItem $item, bool &$save, AdapterInterface $pool, \Closure $setMetadata, ?LoggerInterface $logger) use ($dispatcher, &$saveResult) {
            try {
                return $dispatcher($callback$item$save$pool$setMetadata$logger);
            } finally {
                $saveResult = $save;
            }
        });

        $this->assertSame(345, $pool->get('foo', fn () => 345));
        $this->assertTrue($saveResult);

        $expected = [
            [
/** * Wraps the callback passed to ->get() in a callable. * * @return callable the previous callback wrapper */
    public function setCallbackWrapper(?callable $callbackWrapper): callable
    {
        if (!isset($this->callbackWrapper)) {
            $this->callbackWrapper = LockRegistry::compute(...);

            if (\in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
                $this->setCallbackWrapper(null);
            }
        }

        if (null !== $callbackWrapper && !$callbackWrapper instanceof \Closure) {
            $callbackWrapper = $callbackWrapper(...);
        }

        $previousWrapper = $this->callbackWrapper;
        $this->callbackWrapper = $callbackWrapper ?? static fn (callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata, ?LoggerInterface $logger) => $callback($item$save);

        return $previousWrapper;
    }
Home | Imprint | This part of the site doesn't use cookies.