Skip to content

[12.x] Add Context contextual attribute #55760

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 12.x
Choose a base branch
from
Open
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
31 changes: 31 additions & 0 deletions src/Illuminate/Container/Attributes/Context.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Illuminate\Container\Attributes;

use Attribute;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Container\ContextualAttribute;
use Illuminate\Log\Context\Repository;

#[Attribute(Attribute::TARGET_PARAMETER)]
class Context implements ContextualAttribute
{
/**
* Create a new attribute instance.
*/
public function __construct(public string $key, public mixed $default = null)
{
}

/**
* Resolve the configuration value.
*
* @param self $attribute
* @param \Illuminate\Contracts\Container\Container $container
* @return mixed
*/
public static function resolve(self $attribute, Container $container): mixed
{
return $container->make(Repository::class)->get($attribute->key, $attribute->default);
}
}
23 changes: 23 additions & 0 deletions tests/Container/ContextualAttributeBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Container\Attributes\Authenticated;
use Illuminate\Container\Attributes\Cache;
use Illuminate\Container\Attributes\Config;
use Illuminate\Container\Attributes\Context;
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Container\Attributes\Database;
use Illuminate\Container\Attributes\Log;
Expand All @@ -28,6 +29,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Http\Request;
use Illuminate\Log\Context\Repository as ContextRepository;
use Illuminate\Log\LogManager;
use Mockery as m;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -215,6 +217,20 @@ public function testRouteParameterAttribute()
$container->make(RouteParameterTest::class);
}

public function testContextAttribute(): void
{
$container = new Container;

$container->singleton(ContextRepository::class, function () {
$context = m::mock(ContextRepository::class);
$context->shouldReceive('get')->once()->with('foo', null)->andReturn('foo');

return $context;
});

$container->make(ContextTest::class);
}

public function testStorageAttribute()
{
$container = new Container;
Expand Down Expand Up @@ -425,6 +441,13 @@ public function __construct(#[Config('foo')] string $foo, #[Config('bar')] strin
}
}

final class ContextTest
{
public function __construct(#[Context('foo')] string $foo)
{
}
}

final class DatabaseTest
{
public function __construct(#[Database('foo')] Connection $foo, #[Database('bar')] Connection $bar)
Expand Down