diff --git a/src/Illuminate/Container/Attributes/Context.php b/src/Illuminate/Container/Attributes/Context.php index 34516ea3afc5..1c858074646d 100644 --- a/src/Illuminate/Container/Attributes/Context.php +++ b/src/Illuminate/Container/Attributes/Context.php @@ -13,7 +13,7 @@ class Context implements ContextualAttribute /** * Create a new attribute instance. */ - public function __construct(public string $key, public mixed $default = null) + public function __construct(public string $key, public mixed $default = null, public bool $hidden = false) { } @@ -26,6 +26,11 @@ public function __construct(public string $key, public mixed $default = null) */ public static function resolve(self $attribute, Container $container): mixed { - return $container->make(Repository::class)->get($attribute->key, $attribute->default); + $repository = $container->make(Repository::class); + + return match ($attribute->hidden) { + true => $repository->getHidden($attribute->key, $attribute->default), + false => $repository->get($attribute->key, $attribute->default), + }; } } diff --git a/tests/Container/ContextualAttributeBindingTest.php b/tests/Container/ContextualAttributeBindingTest.php index 610e4a1d1cd8..4eb73ad12d7d 100644 --- a/tests/Container/ContextualAttributeBindingTest.php +++ b/tests/Container/ContextualAttributeBindingTest.php @@ -231,6 +231,21 @@ public function testContextAttribute(): void $container->make(ContextTest::class); } + public function testContextAttributeInteractingWithHidden(): void + { + $container = new Container; + + $container->singleton(ContextRepository::class, function () { + $context = m::mock(ContextRepository::class); + $context->shouldReceive('getHidden')->once()->with('bar', null)->andReturn('bar'); + $context->shouldNotReceive('get'); + + return $context; + }); + + $container->make(ContextHiddenTest::class); + } + public function testStorageAttribute() { $container = new Container; @@ -448,6 +463,13 @@ public function __construct(#[Context('foo')] string $foo) } } +final class ContextHiddenTest +{ + public function __construct(#[Context('bar', hidden: true)] string $foo) + { + } +} + final class DatabaseTest { public function __construct(#[Database('foo')] Connection $foo, #[Database('bar')] Connection $bar)