|
2 | 2 |
|
3 | 3 | namespace Illuminate\Tests\Container;
|
4 | 4 |
|
| 5 | +use Attribute; |
5 | 6 | use Illuminate\Container\Container;
|
6 | 7 | use Illuminate\Container\EntryNotFoundException;
|
7 | 8 | use Illuminate\Contracts\Container\BindingResolutionException;
|
| 9 | +use Illuminate\Contracts\Container\ContextualAttribute; |
8 | 10 | use PHPUnit\Framework\TestCase;
|
9 | 11 | use Psr\Container\ContainerExceptionInterface;
|
10 | 12 | use stdClass;
|
@@ -522,6 +524,23 @@ public function testGetAlias()
|
522 | 524 | $this->assertSame('ConcreteStub', $container->getAlias('foo'));
|
523 | 525 | }
|
524 | 526 |
|
| 527 | + public function testCurrentlyResolving() |
| 528 | + { |
| 529 | + $container = new Container; |
| 530 | + |
| 531 | + $container->afterResolvingAttribute(ContainerCurrentResolvingAttribute::class, function ($attr, $instance, $container) { |
| 532 | + $this->assertEquals(ContainerCurrentResolvingConcrete::class, $container->currentlyResolving()); |
| 533 | + }); |
| 534 | + |
| 535 | + $container->when(ContainerCurrentResolvingConcrete::class) |
| 536 | + ->needs('$currentlyResolving') |
| 537 | + ->give(fn ($container) => $container->currentlyResolving()); |
| 538 | + |
| 539 | + $resolved = $container->make(ContainerCurrentResolvingConcrete::class); |
| 540 | + |
| 541 | + $this->assertEquals(ContainerCurrentResolvingConcrete::class, $resolved->currentlyResolving); |
| 542 | +} |
| 543 | + |
525 | 544 | public function testGetAliasRecursive()
|
526 | 545 | {
|
527 | 546 | $container = new Container;
|
@@ -860,3 +879,23 @@ public function work(IContainerContractStub $stub)
|
860 | 879 | return $stub;
|
861 | 880 | }
|
862 | 881 | }
|
| 882 | + |
| 883 | +#[Attribute(Attribute::TARGET_PARAMETER)] |
| 884 | +class ContainerCurrentResolvingAttribute implements ContextualAttribute |
| 885 | +{ |
| 886 | + public function resolve() |
| 887 | + { |
| 888 | + } |
| 889 | +} |
| 890 | + |
| 891 | +class ContainerCurrentResolvingConcrete |
| 892 | +{ |
| 893 | + public $currentlyResolving; |
| 894 | + |
| 895 | + public function __construct( |
| 896 | + #[ContainerCurrentResolvingAttribute] |
| 897 | + string $currentlyResolving |
| 898 | + ) { |
| 899 | + $this->currentlyResolving = $currentlyResolving; |
| 900 | + } |
| 901 | +} |
0 commit comments