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

Skip to content

Commit bd244cc

Browse files
committed
feature #58004 [DependencyInjection] Add ContainerBuilder::registerChild() shortcut method (HypeMC)
This PR was merged into the 7.2 branch. Discussion ---------- [DependencyInjection] Add `ContainerBuilder::registerChild()` shortcut method | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | - | License | MIT Extracted from #56823 as suggested in #56823 (comment) Commits ------- 7ba430c [DependencyInjection] Add `ContainerBuilder::registerChild()` shortcut method
2 parents 092cf5f + 7ba430c commit bd244cc

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,15 @@ public function register(string $id, ?string $class = null): Definition
937937
return $this->setDefinition($id, new Definition($class));
938938
}
939939

940+
/**
941+
* This method provides a fluid interface for easily registering a child
942+
* service definition of the given parent service.
943+
*/
944+
public function registerChild(string $id, string $parent): ChildDefinition
945+
{
946+
return $this->setDefinition($id, new ChildDefinition($parent));
947+
}
948+
940949
/**
941950
* Registers an autowired service definition.
942951
*

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,18 @@ public function testRegister()
212212
{
213213
$builder = new ContainerBuilder();
214214
$builder->register('foo', 'Bar\FooClass');
215-
$this->assertTrue($builder->hasDefinition('foo'), '->register() registers a new service definition');
216-
$this->assertInstanceOf(Definition::class, $builder->getDefinition('foo'), '->register() returns the newly created Definition instance');
215+
$this->assertTrue($builder->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists');
216+
$this->assertInstanceOf(Definition::class, $builder->getDefinition('foo'), '->getDefinition() returns an instance of Definition');
217+
}
218+
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'), '->hasDefinition() returns true if a service definition exists');
225+
$this->assertInstanceOf(ChildDefinition::class, $definition = $builder->getDefinition('bar'), '->getDefinition() returns an instance of Definition');
226+
$this->assertSame('foo', $definition->getParent(), '->getParent() returns the id of the parent service');
217227
}
218228

219229
public function testAutowire()

0 commit comments

Comments
 (0)