Skip to content

Commit 27db810

Browse files
authored
Merge pull request #32514 from simensen/give-tagged
[7.x] Provide syntactic sugar for contextually binding tagged services
2 parents 4083ae5 + 2ab27cd commit 27db810

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/Illuminate/Container/ContextualBindingBuilder.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,19 @@ public function give($implementation)
6666
$this->container->addContextualBinding($concrete, $this->needs, $implementation);
6767
}
6868
}
69+
70+
/**
71+
* Define tagged services to be used as the implementation for the contextual binding.
72+
*
73+
* @param string $tag
74+
* @return void
75+
*/
76+
public function giveTagged($tag)
77+
{
78+
$this->give(function ($container) use ($tag) {
79+
$taggedServices = $container->tagged($tag);
80+
81+
return is_array($taggedServices) ? $taggedServices : iterator_to_array($taggedServices);
82+
});
83+
}
6984
}

tests/Container/ContextualBindingTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,64 @@ public function testContextualBindingWorksForVariadicDependenciesWithoutFactory(
301301
$this->assertInstanceOf(ContainerContextImplementationStub::class, $resolvedInstance->stubs[0]);
302302
$this->assertInstanceOf(ContainerContextImplementationStubTwo::class, $resolvedInstance->stubs[1]);
303303
}
304+
305+
public function testContextualBindingGivesTagsForArrayWithNoTagsDefined()
306+
{
307+
$container = new Container;
308+
309+
$container->when(ContainerTestContextInjectArray::class)->needs('$stubs')->giveTagged('stub');
310+
311+
$resolvedInstance = $container->make(ContainerTestContextInjectArray::class);
312+
313+
$this->assertCount(0, $resolvedInstance->stubs);
314+
}
315+
316+
public function testContextualBindingGivesTagsForVariadicWithNoTagsDefined()
317+
{
318+
$container = new Container;
319+
320+
$container->when(ContainerTestContextInjectVariadic::class)->needs(IContainerContextContractStub::class)->giveTagged('stub');
321+
322+
$resolvedInstance = $container->make(ContainerTestContextInjectVariadic::class);
323+
324+
$this->assertCount(0, $resolvedInstance->stubs);
325+
}
326+
327+
public function testContextualBindingGivesTagsForArray()
328+
{
329+
$container = new Container;
330+
331+
$container->tag([
332+
ContainerContextImplementationStub::class,
333+
ContainerContextImplementationStubTwo::class,
334+
], ['stub']);
335+
336+
$container->when(ContainerTestContextInjectArray::class)->needs('$stubs')->giveTagged('stub');
337+
338+
$resolvedInstance = $container->make(ContainerTestContextInjectArray::class);
339+
340+
$this->assertCount(2, $resolvedInstance->stubs);
341+
$this->assertInstanceOf(ContainerContextImplementationStub::class, $resolvedInstance->stubs[0]);
342+
$this->assertInstanceOf(ContainerContextImplementationStubTwo::class, $resolvedInstance->stubs[1]);
343+
}
344+
345+
public function testContextualBindingGivesTagsForVariadic()
346+
{
347+
$container = new Container;
348+
349+
$container->tag([
350+
ContainerContextImplementationStub::class,
351+
ContainerContextImplementationStubTwo::class,
352+
], ['stub']);
353+
354+
$container->when(ContainerTestContextInjectVariadic::class)->needs(IContainerContextContractStub::class)->giveTagged('stub');
355+
356+
$resolvedInstance = $container->make(ContainerTestContextInjectVariadic::class);
357+
358+
$this->assertCount(2, $resolvedInstance->stubs);
359+
$this->assertInstanceOf(ContainerContextImplementationStub::class, $resolvedInstance->stubs[0]);
360+
$this->assertInstanceOf(ContainerContextImplementationStubTwo::class, $resolvedInstance->stubs[1]);
361+
}
304362
}
305363

306364
interface IContainerContextContractStub
@@ -385,6 +443,16 @@ public function __construct(ContainerTestContextInjectOne $inner = null)
385443
}
386444
}
387445

446+
class ContainerTestContextInjectArray
447+
{
448+
public $stubs;
449+
450+
public function __construct(array $stubs)
451+
{
452+
$this->stubs = $stubs;
453+
}
454+
}
455+
388456
class ContainerTestContextInjectVariadic
389457
{
390458
public $stubs;

0 commit comments

Comments
 (0)