Skip to content

[7.x] Provide syntactic sugar for contextually binding tagged services #32514

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
Apr 23, 2020
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
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)
Copy link
Contributor

@X-Coder264 X-Coder264 Apr 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this also needs to be added to the ContextualBindingBuilder interface (at least on master since we cannot add it on v7)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh, adding to an interface is a major breaking change.

{
$this->give(function ($container) use ($tag) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$container can be typehinted with the container interface typehint for better IDE autocompletion.

$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