Skip to content
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
9 changes: 7 additions & 2 deletions src/Illuminate/Container/Attributes/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand All @@ -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),
};
}
}
22 changes: 22 additions & 0 deletions tests/Container/ContextualAttributeBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down