Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 22586ca

Browse files
committed
feature #20648 [DependencyInjection] Added a shortcut method for autowired definitions (wouterj)
This PR was merged into the 3.3-dev branch. Discussion ---------- [DependencyInjection] Added a shortcut method for autowired definitions | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | todo This is a simple proposal to make adding autowired definitions to the `ContainerBuilder` a little easier. Registering autowired services in PHP code was quite verbose at the moment, while the whole point of autowiring is quick service registration. **Before** ```php $container->register('app.twig_extension', AppExtension::class) ->setAutowired(true) ->addTag('twig.extension') ; ``` **After** ```php $container->autowire('app.twig_extension', AppExtension::class) ->addTag('twig.extension') ; ``` With #20264, this will be even nicer: ```php $container->autowire(AppExtension::class) ->addTag('twig.extension') ; ``` Commits ------- 6ef4ce8 Added a shortcut method for autowired definitions
2 parents f437d85 + 6ef4ce8 commit 22586ca

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,22 @@ public function register($id, $class = null)
710710
return $this->setDefinition($id, new Definition($class));
711711
}
712712

713+
/**
714+
* Registers an autowired service definition.
715+
*
716+
* This method implements a shortcut for using setDefinition() with
717+
* an autowired definition.
718+
*
719+
* @param string $id The service identifier
720+
* @param null|string $class The service class
721+
*
722+
* @return Definition The created definition
723+
*/
724+
public function autowire($id, $class = null)
725+
{
726+
return $this->setDefinition($id, (new Definition($class))->setAutowired(true));
727+
}
728+
713729
/**
714730
* Adds the service definitions.
715731
*

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ public function testRegister()
8282
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $builder->getDefinition('foo'), '->register() returns the newly created Definition instance');
8383
}
8484

85+
public function testAutowire()
86+
{
87+
$builder = new ContainerBuilder();
88+
$builder->autowire('foo', 'Bar\FooClass');
89+
90+
$this->assertTrue($builder->hasDefinition('foo'), '->autowire() registers a new service definition');
91+
$this->assertTrue($builder->getDefinition('foo')->isAutowired(), '->autowire() creates autowired definitions');
92+
}
93+
8594
public function testHas()
8695
{
8796
$builder = new ContainerBuilder();

0 commit comments

Comments
 (0)