Autowire example


}

#[AsDecorator(decorates: AsDecoratorFoo::class)] class AutowireNestedAttributes implements AsDecoratorInterface
{
    public function __construct(
        #[Autowire([             'decorated' => new AutowireDecorated(),
            'iterator' => new TaggedIterator('foo'),
            'locator' => new TaggedLocator('foo'),
            'service' => new Autowire(service: 'bar')
        ])] array $options)
    {
    }
}
use Symfony\Component\ExpressionLanguage\Expression;

class AutowireTest extends TestCase
{
    /** * @dataProvider provideMultipleParameters */
    public function testCanOnlySetOneParameter(array $parameters)
    {
        $this->expectException(LogicException::class);

        new Autowire(...$parameters);
    }

    public function testMustSetOneParameter()
    {
        $this->expectException(LogicException::class);

        new Autowire();
    }

    public function testCanUseZeroForValue()
    {
        
$container->register('some.id', \stdClass::class);
        $container->setParameter('some.parameter', 'foo');
        $container->setParameter('null.parameter', null);

        (new ResolveClassPass())->process($container);
        (new AutowirePass())->process($container);

        $definition = $container->getDefinition(AutowireAttribute::class);

        $this->assertCount(10, $definition->getArguments());
        $this->assertEquals(new TypedReference('some.id', 'stdClass', attributes: [new Autowire(service: 'some.id')])$definition->getArgument(0));
        $this->assertEquals(new Expression("parameter('some.parameter')")$definition->getArgument(1));
        $this->assertSame('foo/bar', $definition->getArgument(2));
        $this->assertNull($definition->getArgument(3));
        $this->assertEquals(new TypedReference('some.id', 'stdClass', attributes: [new Autowire(service: 'some.id')])$definition->getArgument(4));
        $this->assertEquals(new Expression("parameter('some.parameter')")$definition->getArgument(5));
        $this->assertSame('bar', $definition->getArgument(6));
        $this->assertSame('@bar', $definition->getArgument(7));
        $this->assertSame('foo', $definition->getArgument(8));
        $this->assertEquals(new TypedReference('invalid.id', 'stdClass', ContainerInterface::NULL_ON_INVALID_REFERENCE, attributes: [new Autowire(service: 'invalid.id')])$definition->getArgument(9));

        $container->compile();

        
$this->markTestSkipped('Requires symfony/service-contracts 3.2+');
        }

        $container = new ContainerBuilder();

        $subscriber = new class() implements ServiceSubscriberInterface {
            public static function getSubscribedServices(): array
            {
                return [
                    new SubscribedService('tagged.iterator', 'iterable', attributes: new TaggedIterator('tag')),
                    new SubscribedService('tagged.locator', PsrContainerInterface::class, attributes: new TaggedLocator('tag')),
                    new SubscribedService('autowired', 'stdClass', attributes: new Autowire(service: 'service.id')),
                    new SubscribedService('autowired.nullable', 'stdClass', nullable: true, attributes: new Autowire(service: 'service.id')),
                    new SubscribedService('autowired.parameter', 'string', attributes: new Autowire('%parameter.1%')),
                    new SubscribedService('autowire.decorated', \stdClass::class, attributes: new AutowireDecorated()),
                    new SubscribedService('target', \stdClass::class, attributes: new Target('someTarget')),
                ];
            }
        };

        $container->setParameter('parameter.1', 'foobar');
        $container->register('foo', $subscriber::class)
            ->addMethodCall('setContainer', [new Reference(PsrContainerInterface::class)])
            
Home | Imprint | This part of the site doesn't use cookies.