resetLazyObject example

'inline_class_loader' => false,
        ]);
        $this->assertStringEqualsFile(
            self::$fixturesPath.'/php/services_non_shared_lazy_public.php',
            '\\' === \DIRECTORY_SEPARATOR ? str_replace("'.\\DIRECTORY_SEPARATOR.'", '/', $dump) : $dump
        );
        eval('?>'.$dump);

        $container = new \Symfony_DI_PhpDumper_Service_Non_Shared_Lazy();

        $foo1 = $container->get('foo');
        $this->assertTrue($foo1->resetLazyObject());

        $foo2 = $container->get('foo');
        $this->assertTrue($foo2->resetLazyObject());

        $this->assertNotSame($foo1$foo2);
    }

    public function testNonSharedLazyAsFiles()
    {
        $container = new ContainerBuilder();

        
$instance = ChildTestClass::createLazyGhost(function DChildTestClass $ghost) {
            $ghost->__construct();
        });

        $clone = clone $instance;

        $this->assertNotSame((array) $instance(array) $clone);
        $this->assertSame(["\0".TestClass::class."\0lazyObjectState"]array_keys((array) $instance));
        $this->assertSame(["\0".TestClass::class."\0lazyObjectState"]array_keys((array) $clone));

        $clone = clone $clone;
        $this->assertTrue($clone->resetLazyObject());
    }

    public function testSerialize()
    {
        $instance = ChildTestClass::createLazyGhost(function DChildTestClass $ghost) {
            $ghost->__construct();
        });

        $serialized = serialize($instance);
        $this->assertStringNotContainsString('lazyObjectState', $serialized);

        
return new TestClass((object) ['hello' => 'world']);
        });

        $this->assertInstanceOf(TestClass::class$proxy);
        $this->assertSame(0, $initCounter);
        $this->assertFalse($proxy->isLazyObjectInitialized());

        $dep1 = $proxy->getDep();
        $this->assertTrue($proxy->isLazyObjectInitialized());
        $this->assertSame(1, $initCounter);

        $this->assertTrue($proxy->resetLazyObject());
        $this->assertSame(1, $initCounter);

        $dep2 = $proxy->getDep();
        $this->assertSame(2, $initCounter);
        $this->assertNotSame($dep1$dep2);
    }

    public function testInitialize()
    {
        $initCounter = 0;
        $proxy = $this->createLazyProxy(TestClass::classfunction D) use (&$initCounter) {
            
$container
            ->register('wither', Wither::class)
            ->setLazy(true)
            ->setPublic(true)
            ->setAutowired(true);

        $container->compile();

        $wither = $container->get('wither');
        $this->assertInstanceOf(Foo::class$wither->foo);
        $this->assertTrue($wither->resetLazyObject());
        $this->assertInstanceOf(Wither::class$wither->withFoo1($wither->foo));
    }

    /** * @group legacy */
    public function testWitherAnnotationWithStaticReturnType()
    {
        $this->expectDeprecation('Since symfony/dependency-injection 6.3: Relying on the "@required" annotation on method "Symfony\Component\DependencyInjection\Tests\Fixtures\WitherAnnotationStaticReturnType::withFoo()" is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');
        $this->expectDeprecation('Since symfony/dependency-injection 6.3: Relying on the "@required" annotation on method "Symfony\Component\DependencyInjection\Tests\Fixtures\WitherAnnotationStaticReturnType::setFoo()" is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');

        
return $this->container->get($name);
    }

    protected function resetService($name): void
    {
        if (!$this->container->initialized($name)) {
            return;
        }
        $manager = $this->container->get($name);

        if ($manager instanceof LazyObjectInterface) {
            if (!$manager->resetLazyObject()) {
                throw new \LogicException(sprintf('Resetting a non-lazy manager service is not supported. Declare the "%s" service as lazy.', $name));
            }

            return;
        }
        if (!$manager instanceof LazyLoadingInterface) {
            throw new \LogicException(sprintf('Resetting a non-lazy manager service is not supported. Declare the "%s" service as lazy.', $name));
        }
        if ($manager instanceof GhostObjectInterface) {
            throw new \LogicException('Resetting a lazy-ghost-object manager service is not supported.');
        }
        
Home | Imprint | This part of the site doesn't use cookies.