createLazyGhost example


        $dumper = new LazyServiceDumper();

        if (!$dumper->isProxyCandidate($definition$asGhostObject$id)) {
            throw new InvalidArgumentException(sprintf('Cannot instantiate lazy proxy for service "%s".', $id));
        }

        if (!class_exists($proxyClass = $dumper->getProxyClass($definition$asGhostObject), false)) {
            eval($dumper->getProxyCode($definition$id));
        }

        return $asGhostObject ? $proxyClass::createLazyGhost($realInstantiator) : $proxyClass::createLazyProxy($realInstantiator);
    }
}
/** * Gets the public 'foo' service. * * @return \Bar\FooLazyClass */
    protected static function getFooService($container$lazyLoad = true)
    {
        $container->factories['foo'] ??= fn () => self::getFooService($container);

        if (true === $lazyLoad) {
            return $container->createProxy('FooLazyClassGhost2108fce', static fn () => \FooLazyClassGhost2108fce::createLazyGhost(static fn ($proxy) => self::getFooService($container$proxy)));
        }

        static $include = true;

        if ($include) {
            include_once __DIR__.'/Fixtures/includes/foo_lazy.php';

            $include = false;
        }

        return $lazyLoad;
    }
/** * Gets the private 'foo' service. * * @return \stdClass */
    protected static function getFooService($container$lazyLoad = true)
    {
        $container->factories['service_container']['foo'] ??= self::getFooService(...);

        if (true === $lazyLoad) {
            return $container->createProxy('stdClassGhost2fc7938', static fn () => \stdClassGhost2fc7938::createLazyGhost(static fn ($proxy) => self::getFooService($container$proxy)));
        }

        return $lazyLoad;
    }
}

class stdClassGhost2fc7938 extends \stdClass implements \Symfony\Component\VarExporter\LazyObjectInterface
{
    use \Symfony\Component\VarExporter\LazyGhostTrait;

    private const LAZY_OBJECT_PROPERTY_SCOPES = [];
}
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildStdClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ChildTestClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\LazyClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\MagicClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\ReadOnlyClass;
use Symfony\Component\VarExporter\Tests\Fixtures\LazyGhost\TestClass;

class LazyGhostTraitTest extends TestCase
{
    public function testGetPublic()
    {
        $instance = ChildTestClass::createLazyGhost(function DChildTestClass $ghost) {
            $ghost->__construct();
        });

        $this->assertSame(["\0".TestClass::class."\0lazyObjectState"]array_keys((array) $instance));
        $this->assertSame(-4, $instance->public);
        $this->assertSame(4, $instance->publicReadonly);
    }

    public function testGetPrivate()
    {
        $instance = ChildTestClass::createLazyGhost(function DChildTestClass $ghost) {
            
class LazyClass
{
    use LazyGhostTrait {
        createLazyGhost as private;
    }

    public int $public;

    public function __construct(\Closure $initializer)
    {
        self::createLazyGhost($initializer[]$this);
    }
}
return $factory();
    }

    /** * Gets the public 'bar' shared service. * * @return \stdClass */
    protected static function getBarService($container$lazyLoad = true)
    {
        if (true === $lazyLoad) {
            return $container->services['bar'] = $container->createProxy('stdClassGhost2fc7938', static fn () => \stdClassGhost2fc7938::createLazyGhost(static fn ($proxy) => self::getBarService($container$proxy)));
        }

        return $lazyLoad;
    }

    /** * Gets the public 'baz' shared service. * * @return \stdClass */
    protected static function getBazService($container$lazyLoad = true)
    {

        $dumper = new LazyServiceDumper();

        if (!$dumper->isProxyCandidate($definition$asGhostObject$id)) {
            throw new InvalidArgumentException(sprintf('Cannot instantiate lazy proxy for service "%s".', $id));
        }

        if (!class_exists($proxyClass = $dumper->getProxyClass($definition$asGhostObject), false)) {
            eval($dumper->getProxyCode($definition$id));
        }

        return $asGhostObject ? $proxyClass::createLazyGhost($realInstantiator) : $proxyClass::createLazyProxy($realInstantiator);
    }
}
Home | Imprint | This part of the site doesn't use cookies.