Skip to content

Commit

Permalink
Merge pull request #32514 from simensen/give-tagged
Browse files Browse the repository at this point in the history
[7.x] Provide syntactic sugar for contextually binding tagged services
  • Loading branch information
taylorotwell authored Apr 23, 2020
2 parents 4083ae5 + 2ab27cd commit 27db810
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Illuminate/Container/ContextualBindingBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,19 @@ public function give($implementation)
$this->container->addContextualBinding($concrete, $this->needs, $implementation);
}
}

/**
* Define tagged services to be used as the implementation for the contextual binding.
*
* @param string $tag
* @return void
*/
public function giveTagged($tag)
{
$this->give(function ($container) use ($tag) {
$taggedServices = $container->tagged($tag);

return is_array($taggedServices) ? $taggedServices : iterator_to_array($taggedServices);
});
}
}
68 changes: 68 additions & 0 deletions tests/Container/ContextualBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,64 @@ public function testContextualBindingWorksForVariadicDependenciesWithoutFactory(
$this->assertInstanceOf(ContainerContextImplementationStub::class, $resolvedInstance->stubs[0]);
$this->assertInstanceOf(ContainerContextImplementationStubTwo::class, $resolvedInstance->stubs[1]);
}

public function testContextualBindingGivesTagsForArrayWithNoTagsDefined()
{
$container = new Container;

$container->when(ContainerTestContextInjectArray::class)->needs('$stubs')->giveTagged('stub');

$resolvedInstance = $container->make(ContainerTestContextInjectArray::class);

$this->assertCount(0, $resolvedInstance->stubs);
}

public function testContextualBindingGivesTagsForVariadicWithNoTagsDefined()
{
$container = new Container;

$container->when(ContainerTestContextInjectVariadic::class)->needs(IContainerContextContractStub::class)->giveTagged('stub');

$resolvedInstance = $container->make(ContainerTestContextInjectVariadic::class);

$this->assertCount(0, $resolvedInstance->stubs);
}

public function testContextualBindingGivesTagsForArray()
{
$container = new Container;

$container->tag([
ContainerContextImplementationStub::class,
ContainerContextImplementationStubTwo::class,
], ['stub']);

$container->when(ContainerTestContextInjectArray::class)->needs('$stubs')->giveTagged('stub');

$resolvedInstance = $container->make(ContainerTestContextInjectArray::class);

$this->assertCount(2, $resolvedInstance->stubs);
$this->assertInstanceOf(ContainerContextImplementationStub::class, $resolvedInstance->stubs[0]);
$this->assertInstanceOf(ContainerContextImplementationStubTwo::class, $resolvedInstance->stubs[1]);
}

public function testContextualBindingGivesTagsForVariadic()
{
$container = new Container;

$container->tag([
ContainerContextImplementationStub::class,
ContainerContextImplementationStubTwo::class,
], ['stub']);

$container->when(ContainerTestContextInjectVariadic::class)->needs(IContainerContextContractStub::class)->giveTagged('stub');

$resolvedInstance = $container->make(ContainerTestContextInjectVariadic::class);

$this->assertCount(2, $resolvedInstance->stubs);
$this->assertInstanceOf(ContainerContextImplementationStub::class, $resolvedInstance->stubs[0]);
$this->assertInstanceOf(ContainerContextImplementationStubTwo::class, $resolvedInstance->stubs[1]);
}
}

interface IContainerContextContractStub
Expand Down Expand Up @@ -385,6 +443,16 @@ public function __construct(ContainerTestContextInjectOne $inner = null)
}
}

class ContainerTestContextInjectArray
{
public $stubs;

public function __construct(array $stubs)
{
$this->stubs = $stubs;
}
}

class ContainerTestContextInjectVariadic
{
public $stubs;
Expand Down

0 comments on commit 27db810

Please sign in to comment.