setAutowired example

namespace Symfony\Component\DependencyInjection\Loader\Configurator\Traits;

trait AutowireTrait
{
    /** * Enables/disables autowiring. * * @return $this */
    final public function autowire(bool $autowired = true)static
    {
        $this->definition->setAutowired($autowired);

        return $this;
    }
}


            $definition = new ChildDefinition($service['parent']);
        } else {
            $definition = new Definition();
        }

        if (isset($defaults['public'])) {
            $definition->setPublic($defaults['public']);
        }
        if (isset($defaults['autowire'])) {
            $definition->setAutowired($defaults['autowire']);
        }
        if (isset($defaults['autoconfigure'])) {
            $definition->setAutoconfigured($defaults['autoconfigure']);
        }

        $definition->setChanges([]);

        if (isset($service['class'])) {
            $definition->setClass($service['class']);
        }

        
foreach ($this->pluginInstances->getActives() as $plugin) {
            $class = $plugin::class;

            $definition = new Definition();
            if ($container->hasDefinition($class)) {
                $definition = $container->getDefinition($class);
            }

            $definition->setFactory([new Reference(self::class), 'getPluginInstance']);
            $definition->addArgument($class);

            $definition->setAutowired(true);
            $definition->setPublic(true);

            $container->setDefinition($class$definition);
        }
    }

    final public function getPluginInstance(string $class): ?Plugin
    {
        $plugin = $this->pluginInstances->get($class);
        if (!$plugin || !$plugin->isActive()) {
            return null;
        }
public static function setUpBeforeClass(): void
    {
        ClassExistsMock::register(AutowirePass::class);
    }

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

        $container->register(Foo::class);
        $barDefinition = $container->register('bar', Bar::class);
        $barDefinition->setAutowired(true);

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

        $this->assertCount(1, $container->getDefinition('bar')->getArguments());
        $this->assertEquals(Foo::class(string) $container->getDefinition('bar')->getArgument(0));
    }

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

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

        $container->register('service', ServiceLocator::class)
            ->setPublic(true)
            ->setArguments([[]])
        ;
        $container->register('decorator', DecoratedServiceLocator::class)
            ->setDecoratedService('service')
            ->setAutowired(true)
            ->setPublic(true)
        ;
        $container->setAlias(ServiceLocator::class, 'decorator.inner')
            ->setPublic(true)
        ;
        $container->register('user_service', DecoratedServiceLocator::class)
            ->setAutowired(true)
        ;

        $container->compile();

        


    public function testBuildWithExistingDefinition(): void
    {
        $activePluginData = $this->getActivePlugin()->jsonSerialize();
        $loader = new StaticKernelPluginLoader($this->classLoader, null, [$activePluginData]);
        $loader->initializePlugins(TEST_PROJECT_DIR);

        $container = new ContainerBuilder();

        $definition = new Definition();
        $definition->setAutowired(false);
        $definition->setPublic(false);
        $container->setDefinition(SwagTest::class$definition);

        $loader->build($container);

        $actualDefinition = $container->getDefinition(SwagTest::class);
        static::assertSame($definition$actualDefinition);
        static::assertTrue($actualDefinition->isAutowired());
        static::assertTrue($actualDefinition->isPublic());
    }

    
/** * @requires PHP 8.1 * * @dataProvider provideDefaultClasses */
    public function testDumpHandlesDefaultAttribute($class$expectedFile)
    {
        $container = new ContainerBuilder();
        $container
            ->register('foo', $class)
            ->setPublic(true)
            ->setAutowired(true)
            ->setArguments([2 => true]);

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

        $dumper = new YamlDumper($container);

        $this->assertSame(file_get_contents(self::$fixturesPath.'/yaml/'.$expectedFile)$dumper->dump());
    }

    public static function provideDefaultClasses()
    {
        
$this->process($container);

        $this->assertTrue($container->getDefinition('child1')->isLazy());
    }

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

        $container->register('parent', 'stdClass')
            ->setAutowired(true)
        ;

        $container->setDefinition('child1', new ChildDefinition('parent'))
            ->setAutowired(false)
        ;

        $this->process($container);

        $this->assertFalse($container->getDefinition('child1')->isAutowired());
    }

    
return $this->setDefinition($idnew Definition($class));
    }

    /** * Registers an autowired service definition. * * This method implements a shortcut for using setDefinition() with * an autowired definition. */
    public function autowire(string $id, string $class = null): Definition
    {
        return $this->setDefinition($id(new Definition($class))->setAutowired(true));
    }

    /** * Adds the service definitions. * * @param array<string, Definition> $definitions * * @return void */
    public function addDefinitions(array $definitions)
    {
        


    /** * @dataProvider provideDefaultClasses */
    public function testDumpHandlesDefaultAttribute($class$expectedFile)
    {
        $container = new ContainerBuilder();
        $container
            ->register('foo', $class)
            ->setPublic(true)
            ->setAutowired(true)
            ->setArguments([2 => true]);

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

        $dumper = new XmlDumper($container);

        $this->assertSame(file_get_contents(self::$fixturesPath.'/xml/'.$expectedFile)$dumper->dump());
    }

    public static function provideDefaultClasses()
    {
        
$container->compile();

        $this->assertTrue($container->get('bar')->callPassed(), '->compile() initializes properties before method calls');
    }

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

        $container->register(A::class)->setPublic(true);
        $bDefinition = $container->register('b', B::class);
        $bDefinition->setAutowired(true);
        $bDefinition->setPublic(true);

        $container->compile();

        $this->assertEquals(A::class(string) $container->getDefinition('b')->getArgument(0));
    }

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

        
if ($this->isLoadingInstanceof) {
            $definition = new ChildDefinition('');
        } elseif ($parent = $service->getAttribute('parent')) {
            $definition = new ChildDefinition($parent);
        } else {
            $definition = new Definition();
        }

        if ($defaults->getChanges()['public'] ?? false) {
            $definition->setPublic($defaults->isPublic());
        }
        $definition->setAutowired($defaults->isAutowired());
        $definition->setAutoconfigured($defaults->isAutoconfigured());
        $definition->setChanges([]);

        foreach (['class', 'public', 'shared', 'synthetic', 'abstract'] as $key) {
            if ($value = $service->getAttribute($key)) {
                $method = 'set'.$key;
                $definition->$method(XmlUtils::phpize($value));
            }
        }

        if ($value = $service->getAttribute('lazy')) {
            
$this->expectDeprecation('Since symfony/dependency-injection 6.3: Relying on the "@required" annotation on method "Symfony\Component\DependencyInjection\Tests\Compiler\SetterInjectionParentAnnotation::setDependencies()" is deprecated, use the "Symfony\Contracts\Service\Attribute\Required" attribute instead.');

        $container = new ContainerBuilder();
        $container->register(FooAnnotation::class);
        $container->register(A::class);
        $container->register(CollisionA::class);
        $container->register(CollisionB::class);

        // manually configure *one* call, to override autowiring         $container
            ->register('setter_injection', SetterInjectionAnnotation::class)
            ->setAutowired(true)
            ->addMethodCall('setWithCallsConfigured', ['manual_arg1', 'manual_arg2']);

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

        $methodCalls = $container->getDefinition('setter_injection')->getMethodCalls();

        $this->assertEquals(
            ['setWithCallsConfigured', 'setFoo', 'setChildMethodWithoutDocBlock', 'setDependencies'],
            array_column($methodCalls, 0)
        );

        
private string $resource;
    private ?array $excludes = null;
    private bool $allowParent;
    private ?string $path;

    public function __construct(ServicesConfigurator $parent, PhpFileLoader $loader, Definition $defaults, string $namespace, string $resource, bool $allowParent, string $path = null)
    {
        $definition = new Definition();
        if (!$defaults->isPublic() || !$defaults->isPrivate()) {
            $definition->setPublic($defaults->isPublic());
        }
        $definition->setAutowired($defaults->isAutowired());
        $definition->setAutoconfigured($defaults->isAutoconfigured());
        // deep clone, to avoid multiple process of the same instance in the passes         $definition->setBindings(unserialize(serialize($defaults->getBindings())));
        $definition->setChanges([]);

        $this->loader = $loader;
        $this->resource = $resource;
        $this->allowParent = $allowParent;
        $this->path = $path;

        parent::__construct($parent$definition$namespace$defaults->getTags());
    }
<?php
use Symfony\Component\DependencyInjection\ContainerBuilder;

$container = new ContainerBuilder();

$container
    ->register('foo', 'Foo')
    ->setAutowired(true)
    ->setPublic(true)
;

return $container;
Home | Imprint | This part of the site doesn't use cookies.