Skip to content

[DependencyInjection] Add ContainerBuilder::registerChild() shortcut method #58004

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
Aug 14, 2024
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
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate `!tagged` tag, use `!tagged_iterator` instead
* Add a `ContainerBuilder::registerChild()` shortcut method for registering child definitions

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,15 @@ public function register(string $id, ?string $class = null): Definition
return $this->setDefinition($id, new Definition($class));
}

/**
* This method provides a fluid interface for easily registering a child
* service definition of the given parent service.
*/
public function registerChild(string $id, string $parent): ChildDefinition
{
return $this->setDefinition($id, new ChildDefinition($parent));
}

/**
* Registers an autowired service definition.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,18 @@ public function testRegister()
{
$builder = new ContainerBuilder();
$builder->register('foo', 'Bar\FooClass');
$this->assertTrue($builder->hasDefinition('foo'), '->register() registers a new service definition');
$this->assertInstanceOf(Definition::class, $builder->getDefinition('foo'), '->register() returns the newly created Definition instance');
$this->assertTrue($builder->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists');
$this->assertInstanceOf(Definition::class, $builder->getDefinition('foo'), '->getDefinition() returns an instance of Definition');
}

public function testRegisterChild()
{
$builder = new ContainerBuilder();
$builder->register('foo', 'Bar\FooClass');
$builder->registerChild('bar', 'foo');
$this->assertTrue($builder->hasDefinition('bar'), '->hasDefinition() returns true if a service definition exists');
$this->assertInstanceOf(ChildDefinition::class, $definition = $builder->getDefinition('bar'), '->getDefinition() returns an instance of Definition');
$this->assertSame('foo', $definition->getParent(), '->getParent() returns the id of the parent service');
}

public function testAutowire()
Expand Down
Loading