initializeLazyObject example

parent::__clone();
        }
    }

    public function __serialize(): array
    {
        $class = self::class;

        if ((Registry::$parentMethods[$class] ??= Registry::getParentMethods($class))['serialize']) {
            $properties = parent::__serialize();
        } else {
            $this->initializeLazyObject();
            $properties = (array) $this;
        }
        unset($properties["\0$class\0lazyObjectState"]);

        if (Registry::$parentMethods[$class]['serialize'] || !Registry::$parentMethods[$class]['sleep']) {
            return $properties;
        }

        $scope = get_parent_class($class);
        $data = [];

        
$container->compile();
        $dumper = new PhpDumper($container);

        $this->assertStringEqualsFile(self::$fixturesPath.'/php/lazy_autowire_attribute.php', $dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_Lazy_Autowire_Attribute']));

        require self::$fixturesPath.'/php/lazy_autowire_attribute.php';

        $container = new \Symfony_DI_PhpDumper_Test_Lazy_Autowire_Attribute();

        $this->assertInstanceOf(Foo::class$container->get('bar')->foo);
        $this->assertInstanceOf(LazyObjectInterface::class$container->get('bar')->foo);
        $this->assertSame($container->get('foo')$container->get('bar')->foo->initializeLazyObject());
    }

    public function testLazyAutowireAttributeWithIntersection()
    {
        $container = new ContainerBuilder();
        $container->register('foo', AAndIInterfaceConsumer::class)
            ->setPublic('true')
            ->setAutowired(true);

        $container->compile();

        

        $initCounter = 0;
        $proxy = $this->createLazyProxy(TestClass::classfunction D) use (&$initCounter) {
            ++$initCounter;

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

        $this->assertSame(0, $initCounter);
        $this->assertFalse($proxy->isLazyObjectInitialized());

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

        $proxy->initializeLazyObject();
        $this->assertSame(1, $initCounter);
    }

    public function testClone()
    {
        $initCounter = 0;
        $proxy = $this->createLazyProxy(TestClass::classfunction D) use (&$initCounter) {
            
'defaultManager',
            'irrelevant'
        );
        $registry->setTestContainer($container);

        $service = $container->get('foo');

        self::assertInstanceOf(\stdClass::class$service);
        self::assertInstanceOf(LazyObjectInterface::class$service);
        self::assertFalse($service->isLazyObjectInitialized());

        $service->initializeLazyObject();

        self::assertTrue($container->initialized('foo'));
        self::assertTrue($service->isLazyObjectInitialized());

        $registry->resetManager();
        $service->initializeLazyObject();

        $wrappedValue = $service->initializeLazyObject();
        self::assertInstanceOf(\stdClass::class$wrappedValue);
        self::assertNotInstanceOf(LazyObjectInterface::class$wrappedValue);
    }

    
parent::__clone();
        }
    }

    public function __serialize(): array
    {
        $class = self::class;

        if ((Registry::$parentMethods[$class] ??= Registry::getParentMethods($class))['serialize']) {
            $properties = parent::__serialize();
        } else {
            $this->initializeLazyObject();
            $properties = (array) $this;
        }
        unset($properties["\0$class\0lazyObjectState"]);

        if (Registry::$parentMethods[$class]['serialize'] || !Registry::$parentMethods[$class]['sleep']) {
            return $properties;
        }

        $scope = get_parent_class($class);
        $data = [];

        
'dummyProperty' => fn () => 123,
        ]);

        $this->assertSame(["\0".TestClass::class."\0lazyObjectState"]array_keys((array) $instance));
        $this->assertFalse($instance->isLazyObjectInitialized());
        $this->assertSame(123, $instance->public);
        $this->assertFalse($instance->isLazyObjectInitialized());
        $this->assertTrue($instance->isLazyObjectInitialized(true));
        $this->assertSame(['public', "\0".TestClass::class."\0lazyObjectState"]array_keys((array) $instance));
        $this->assertSame(1, $counter);

        $instance->initializeLazyObject();
        $this->assertTrue($instance->isLazyObjectInitialized());
        $this->assertSame(123, $instance->public);
        $this->assertSame(6, $counter);

        $properties = (array) $instance;
        $this->assertInstanceOf(LazyObjectState::class$properties["\0".TestClass::class."\0lazyObjectState"]);
        unset($properties["\0".TestClass::class."\0lazyObjectState"]);
        $this->assertSame(array_keys((array) new ChildTestClass())array_keys($properties));
        $this->assertSame([123, 345, 456, 567, 234, 678]array_values($properties));
    }

    
Home | Imprint | This part of the site doesn't use cookies.