Skip to content

Commit d8aaa6f

Browse files
committed
Added a shortcut method for autowired definitions
1 parent 3e61303 commit d8aaa6f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

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
*

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)