Skip to content

Commit b47abb5

Browse files
[12.x] Allowing Context Attribute to Interact with Hidden (#55799)
* Added hidden attribute to Context class, updated resolve method * Added test case for context attribute interacting with hidden attributes
1 parent 7de96ad commit b47abb5

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/Illuminate/Container/Attributes/Context.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Context implements ContextualAttribute
1313
/**
1414
* Create a new attribute instance.
1515
*/
16-
public function __construct(public string $key, public mixed $default = null)
16+
public function __construct(public string $key, public mixed $default = null, public bool $hidden = false)
1717
{
1818
}
1919

@@ -26,6 +26,11 @@ public function __construct(public string $key, public mixed $default = null)
2626
*/
2727
public static function resolve(self $attribute, Container $container): mixed
2828
{
29-
return $container->make(Repository::class)->get($attribute->key, $attribute->default);
29+
$repository = $container->make(Repository::class);
30+
31+
return match ($attribute->hidden) {
32+
true => $repository->getHidden($attribute->key, $attribute->default),
33+
false => $repository->get($attribute->key, $attribute->default),
34+
};
3035
}
3136
}

tests/Container/ContextualAttributeBindingTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,21 @@ public function testContextAttribute(): void
231231
$container->make(ContextTest::class);
232232
}
233233

234+
public function testContextAttributeInteractingWithHidden(): void
235+
{
236+
$container = new Container;
237+
238+
$container->singleton(ContextRepository::class, function () {
239+
$context = m::mock(ContextRepository::class);
240+
$context->shouldReceive('getHidden')->once()->with('bar', null)->andReturn('bar');
241+
$context->shouldNotReceive('get');
242+
243+
return $context;
244+
});
245+
246+
$container->make(ContextHiddenTest::class);
247+
}
248+
234249
public function testStorageAttribute()
235250
{
236251
$container = new Container;
@@ -448,6 +463,13 @@ public function __construct(#[Context('foo')] string $foo)
448463
}
449464
}
450465

466+
final class ContextHiddenTest
467+
{
468+
public function __construct(#[Context('bar', hidden: true)] string $foo)
469+
{
470+
}
471+
}
472+
451473
final class DatabaseTest
452474
{
453475
public function __construct(#[Database('foo')] Connection $foo, #[Database('bar')] Connection $bar)

0 commit comments

Comments
 (0)