setProxyInstantiator example


class ContainerBuilderTest extends TestCase
{
    public function testCreateProxyServiceWithRuntimeInstantiator()
    {
        $builder = new ContainerBuilder();
        $builder->setProxyInstantiator(new RuntimeInstantiator());

        $builder->register('foo1', \ProxyManagerBridgeFooClass::class)->setFile(__DIR__.'/Fixtures/includes/foo.php')->setPublic(true);
        $builder->getDefinition('foo1')->setLazy(true)->addTag('proxy', ['interface' => \ProxyManagerBridgeFooClass::class]);

        $builder->compile();

        /* @var $foo1 \ProxyManager\Proxy\LazyLoadingInterface|\ProxyManager\Proxy\ValueHolderInterface */
        $foo1 = $builder->get('foo1');

        $foo1->__destruct();
        $this->assertSame(0, $foo1::$destructorCount);

        
$builder = new ContainerBuilder();
        $builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
        $builder->register('foo2', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/%file%.php');
        $builder->setParameter('file', 'foo');
        $this->assertInstanceOf(\Bar\FooClass::class$builder->get('foo1'), '->createService() requires the file defined by the service definition');
        $this->assertInstanceOf(\Bar\FooClass::class$builder->get('foo2'), '->createService() replaces parameters in the file provided by the service definition');
    }

    public function testCreateProxyWithRealServiceInstantiator()
    {
        $builder = new ContainerBuilder();
        $builder->setProxyInstantiator(new RealServiceInstantiator());

        $builder->register('foo1', 'Bar\FooClass')->setFile(__DIR__.'/Fixtures/includes/foo.php');
        $builder->getDefinition('foo1')->setLazy(true);

        $foo1 = $builder->get('foo1');

        $this->assertSame($foo1$builder->get('foo1'), 'The same proxy is retrieved on multiple subsequent calls');
        $this->assertSame('Bar\FooClass', $foo1::class);
    }

    public function testCreateLazyProxy()
    {
Home | Imprint | This part of the site doesn't use cookies.