Skip to content

Commit

Permalink
Add RouteParameter attribute (#53080)
Browse files Browse the repository at this point in the history
  • Loading branch information
bastien-phi authored Oct 11, 2024
1 parent 3c501ee commit 3818c2a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Illuminate/Container/Attributes/RouteParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Illuminate\Container\Attributes;

use Attribute;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Container\ContextualAttribute;

#[Attribute(Attribute::TARGET_PARAMETER)]
class RouteParameter implements ContextualAttribute
{
/**
* Create a new class instance.
*/
public function __construct(public string $parameter)
{
}

/**
* Resolve the route parameter.
*
* @param self $attribute
* @param \Illuminate\Contracts\Container\Container $container
* @return mixed
*/
public static function resolve(self $attribute, Container $container)
{
return $container->make('request')->route($attribute->parameter);
}
}
24 changes: 24 additions & 0 deletions tests/Container/ContextualAttributeBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Container\Attributes\CurrentUser;
use Illuminate\Container\Attributes\Database;
use Illuminate\Container\Attributes\Log;
use Illuminate\Container\Attributes\RouteParameter;
use Illuminate\Container\Attributes\Storage;
use Illuminate\Container\Attributes\Tag;
use Illuminate\Container\Container;
Expand All @@ -24,7 +25,9 @@
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Database\Connection;
use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Filesystem\FilesystemManager;
use Illuminate\Http\Request;
use Illuminate\Log\LogManager;
use Mockery as m;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -197,6 +200,20 @@ public function testLogAttribute()
$container->make(LogTest::class);
}

public function testRouteParameterAttribute()
{
$container = new Container;
$container->singleton('request', function () {
$request = m::mock(Request::class);
$request->shouldReceive('route')->with('foo')->andReturn(m::mock(Model::class));
$request->shouldReceive('route')->with('bar')->andReturn('bar');

return $request;
});

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

public function testStorageAttribute()
{
$container = new Container;
Expand Down Expand Up @@ -428,6 +445,13 @@ public function __construct(#[Log('foo')] LoggerInterface $foo, #[Log('bar')] Lo
}
}

final class RouteParameterTest
{
public function __construct(#[RouteParameter('foo')] Model $foo, #[RouteParameter('bar')] string $bar)
{
}
}

final class StorageTest
{
public function __construct(#[Storage('foo')] Filesystem $foo, #[Storage('bar')] Filesystem $bar)
Expand Down

0 comments on commit 3818c2a

Please sign in to comment.