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

Skip to content

Commit bb00b98

Browse files
committed
[DependencyInjection] Add ContainerBuilder::registerChild() shortcut method
1 parent a8098b2 commit bb00b98

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

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

910
7.1
1011
---

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,17 @@ public function register(string $id, ?string $class = null): Definition
937937
return $this->setDefinition($id, new Definition($class));
938938
}
939939

940+
/**
941+
* Registers a service definition.
942+
*
943+
* This method allows for simple registration of service definition
944+
* with a fluid interface.
945+
*/
946+
public function registerChild(string $id, string $parent): Definition
947+
{
948+
return $this->setDefinition($id, new ChildDefinition($parent));
949+
}
950+
940951
/**
941952
* Registers an autowired service definition.
942953
*

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,16 @@ public function testRegister()
216216
$this->assertInstanceOf(Definition::class, $builder->getDefinition('foo'), '->register() returns the newly created Definition instance');
217217
}
218218

219+
public function testRegisterChild()
220+
{
221+
$builder = new ContainerBuilder();
222+
$builder->register('foo', 'Bar\FooClass');
223+
$builder->registerChild('bar', 'foo');
224+
$this->assertTrue($builder->hasDefinition('bar'), '->registerChild() registers a new service definition');
225+
$this->assertInstanceOf(ChildDefinition::class, $definition = $builder->getDefinition('bar'), '->registerChild() returns the newly created ChildDefinition instance');
226+
$this->assertSame('foo', $definition->getParent(), '->registerChild() registers a new child service definition with the parent set');
227+
}
228+
219229
public function testAutowire()
220230
{
221231
$builder = new ContainerBuilder();

0 commit comments

Comments
 (0)