Skip to content

Commit 3818c2a

Browse files
authored
Add RouteParameter attribute (#53080)
1 parent 3c501ee commit 3818c2a

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Illuminate\Container\Attributes;
4+
5+
use Attribute;
6+
use Illuminate\Contracts\Container\Container;
7+
use Illuminate\Contracts\Container\ContextualAttribute;
8+
9+
#[Attribute(Attribute::TARGET_PARAMETER)]
10+
class RouteParameter implements ContextualAttribute
11+
{
12+
/**
13+
* Create a new class instance.
14+
*/
15+
public function __construct(public string $parameter)
16+
{
17+
}
18+
19+
/**
20+
* Resolve the route parameter.
21+
*
22+
* @param self $attribute
23+
* @param \Illuminate\Contracts\Container\Container $container
24+
* @return mixed
25+
*/
26+
public static function resolve(self $attribute, Container $container)
27+
{
28+
return $container->make('request')->route($attribute->parameter);
29+
}
30+
}

tests/Container/ContextualAttributeBindingTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Container\Attributes\CurrentUser;
1515
use Illuminate\Container\Attributes\Database;
1616
use Illuminate\Container\Attributes\Log;
17+
use Illuminate\Container\Attributes\RouteParameter;
1718
use Illuminate\Container\Attributes\Storage;
1819
use Illuminate\Container\Attributes\Tag;
1920
use Illuminate\Container\Container;
@@ -24,7 +25,9 @@
2425
use Illuminate\Contracts\Filesystem\Filesystem;
2526
use Illuminate\Database\Connection;
2627
use Illuminate\Database\DatabaseManager;
28+
use Illuminate\Database\Eloquent\Model;
2729
use Illuminate\Filesystem\FilesystemManager;
30+
use Illuminate\Http\Request;
2831
use Illuminate\Log\LogManager;
2932
use Mockery as m;
3033
use PHPUnit\Framework\TestCase;
@@ -197,6 +200,20 @@ public function testLogAttribute()
197200
$container->make(LogTest::class);
198201
}
199202

203+
public function testRouteParameterAttribute()
204+
{
205+
$container = new Container;
206+
$container->singleton('request', function () {
207+
$request = m::mock(Request::class);
208+
$request->shouldReceive('route')->with('foo')->andReturn(m::mock(Model::class));
209+
$request->shouldReceive('route')->with('bar')->andReturn('bar');
210+
211+
return $request;
212+
});
213+
214+
$container->make(RouteParameterTest::class);
215+
}
216+
200217
public function testStorageAttribute()
201218
{
202219
$container = new Container;
@@ -428,6 +445,13 @@ public function __construct(#[Log('foo')] LoggerInterface $foo, #[Log('bar')] Lo
428445
}
429446
}
430447

448+
final class RouteParameterTest
449+
{
450+
public function __construct(#[RouteParameter('foo')] Model $foo, #[RouteParameter('bar')] string $bar)
451+
{
452+
}
453+
}
454+
431455
final class StorageTest
432456
{
433457
public function __construct(#[Storage('foo')] Filesystem $foo, #[Storage('bar')] Filesystem $bar)

0 commit comments

Comments
 (0)