Skip to content

[12.x] feat: Add Contextual Implementation/Interface Binding via PHP8 Attribute #55904

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
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
35 changes: 35 additions & 0 deletions src/Illuminate/Container/Attributes/Give.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Illuminate\Container\Attributes;

use Attribute;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Container\ContextualAttribute;

#[Attribute(Attribute::TARGET_PARAMETER)]
class Give implements ContextualAttribute
{
/**
* Provide a concrete class implementation for dependency injection.
*
* @template T
* @param class-string<T> $class
* @param array|null $params
*/
public function __construct(
public string $class,
public array $params = []
) {}

/**
* Resolve the dependency.
*
* @param self $attribute
* @param \Illuminate\Contracts\Container\Container $container
* @return mixed
*/
public static function resolve(self $attribute, Container $container): mixed
{
return $container->make($attribute->class, $attribute->params);
}
}
53 changes: 52 additions & 1 deletion tests/Container/ContextualAttributeBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Container\Attributes\Database;
use Illuminate\Container\Attributes\Log;
use Illuminate\Container\Attributes\Give;
use Illuminate\Container\Attributes\RouteParameter;
use Illuminate\Container\Attributes\Storage;
use Illuminate\Container\Attributes\Tag;
Expand Down Expand Up @@ -46,7 +47,7 @@ public function testDependencyCanBeResolvedFromAttributeBinding()
{
$container = new Container;

$container->bind(ContainerTestContract::class, fn () => new ContainerTestImplB);
$container->bind(ContainerTestContract::class, fn (): ContainerTestImplB => new ContainerTestImplB);
$container->whenHasAttribute(ContainerTestAttributeThatResolvesContractImpl::class, function (ContainerTestAttributeThatResolvesContractImpl $attribute) {
return match ($attribute->name) {
'A' => new ContainerTestImplA,
Expand All @@ -65,6 +66,30 @@ public function testDependencyCanBeResolvedFromAttributeBinding()
$this->assertInstanceOf(ContainerTestImplB::class, $classB->property);
}

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

$container->bind(ContainerTestContract::class, concrete: ContainerTestImplA::class);

$resolution = $container->make(GiveTestSimple::class);

$this->assertInstanceOf(SimpleDependency::class, $resolution->dependency);
}


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

$container->bind(ContainerTestContract::class, concrete: ContainerTestImplA::class);

$resolution = $container->make(GiveTestComplex::class);

$this->assertInstanceOf(ComplexDependency::class, $resolution->dependency);
$this->assertTrue($resolution->dependency->param);
}

public function testScalarDependencyCanBeResolvedFromAttributeBinding()
{
$container = new Container;
Expand Down Expand Up @@ -161,6 +186,7 @@ public function testConfigAttribute()
$container->make(ConfigTest::class);
}


public function testDatabaseAttribute()
{
$container = new Container;
Expand Down Expand Up @@ -435,6 +461,13 @@ public function __construct(
}
}

final class SimpleDependency implements ContainerTestContract {}

final class ComplexDependency implements ContainerTestContract
{
public function __construct(public bool $param) {}
}

final class AuthedTest
{
public function __construct(#[Authenticated('foo')] AuthenticatableContract $foo, #[CurrentUser('bar')] AuthenticatableContract $bar)
Expand Down Expand Up @@ -505,6 +538,24 @@ public function __construct(#[Storage('foo')] Filesystem $foo, #[Storage('bar')]
}
}

final class GiveTestSimple
{
public function __construct(
#[Give(SimpleDependency::class)]
public readonly ContainerTestContract $dependency
) {
}
}

final class GiveTestComplex
{
public function __construct(
#[Give(ComplexDependency::class, ['param' => true])]
public readonly ContainerTestContract $dependency
) {
}
}

final class TimezoneObject
{
public function __construct(
Expand Down