Skip to content

[DependencyInjection] Added a shortcut method for autowired definitions #20648

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Symfony/Component/DependencyInjection/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,22 @@ public function register($id, $class = null)
return $this->setDefinition($id, new Definition($class));
}

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

/**
* Adds the service definitions.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ public function testRegister()
$this->assertInstanceOf('Symfony\Component\DependencyInjection\Definition', $builder->getDefinition('foo'), '->register() returns the newly created Definition instance');
}

public function testAutowire()
{
$builder = new ContainerBuilder();
$builder->autowire('foo', 'Bar\FooClass');

$this->assertTrue($builder->hasDefinition('foo'), '->autowire() registers a new service definition');
$this->assertTrue($builder->getDefinition('foo')->isAutowired(), '->autowire() creates autowired definitions');
}

public function testHas()
{
$builder = new ContainerBuilder();
Expand Down