From 556ff26c716c5f5d0c6f85ff77eb66824f3aa8c5 Mon Sep 17 00:00:00 2001 From: AJ Meireles Date: Tue, 20 May 2025 23:34:03 -0300 Subject: [PATCH 1/2] Added hidden attribute to Context class, updated resolve method --- src/Illuminate/Container/Attributes/Context.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) 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), + }; } } From df99ac64c306c41021b333339ee288369a158dd4 Mon Sep 17 00:00:00 2001 From: AJ Meireles Date: Tue, 20 May 2025 23:37:47 -0300 Subject: [PATCH 2/2] Added test case for context attribute interacting with hidden attributes --- .../ContextualAttributeBindingTest.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) 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)