From 8febec76411872306b58aeb1d9f4224e8c1c2ad1 Mon Sep 17 00:00:00 2001 From: Italo Date: Tue, 2 Nov 2021 12:10:59 -0300 Subject: [PATCH] [8.x] Allows Stringable objects as middleware. (#39439) * Allows Stringable objects as middleware. * Style changes. * Added test for stringable middleware on group. --- .../Routing/PendingResourceRegistration.php | 8 ++ src/Illuminate/Routing/RouteRegistrar.php | 8 ++ tests/Routing/RouteRegistrarTest.php | 78 +++++++++++++++++++ 3 files changed, 94 insertions(+) diff --git a/src/Illuminate/Routing/PendingResourceRegistration.php b/src/Illuminate/Routing/PendingResourceRegistration.php index 59e4b8f0b78f..b2f20a6f8203 100644 --- a/src/Illuminate/Routing/PendingResourceRegistration.php +++ b/src/Illuminate/Routing/PendingResourceRegistration.php @@ -149,6 +149,14 @@ public function parameter($previous, $new) */ public function middleware($middleware) { + if (! is_string($middleware)) { + $middleware = Arr::wrap($middleware); + + foreach ($middleware as $key => $value) { + $middleware[$key] = (string) $value; + } + } + $this->options['middleware'] = $middleware; return $this; diff --git a/src/Illuminate/Routing/RouteRegistrar.php b/src/Illuminate/Routing/RouteRegistrar.php index ad7f6c0ccafa..1b625430de2d 100644 --- a/src/Illuminate/Routing/RouteRegistrar.php +++ b/src/Illuminate/Routing/RouteRegistrar.php @@ -93,6 +93,14 @@ public function attribute($key, $value) throw new InvalidArgumentException("Attribute [{$key}] does not exist."); } + if ($key === 'middleware' && ! is_string($value)) { + $value = Arr::wrap($value); + + foreach ($value as $index => $middleware) { + $value[$index] = (string) $middleware; + } + } + $this->attributes[Arr::get($this->aliases, $key, $key)] = $value; return $this; diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index ce443e040c24..48870bec0e47 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -9,6 +9,7 @@ use Illuminate\Routing\Router; use Mockery as m; use PHPUnit\Framework\TestCase; +use Stringable; class RouteRegistrarTest extends TestCase { @@ -60,6 +61,42 @@ public function testMiddlewareFluentRegistration() $this->assertEquals(['seven'], $this->getRoute()->middleware()); } + public function testMiddlewareAsStringableObject() + { + $one = new class implements Stringable + { + public function __toString() + { + return 'one'; + } + }; + + $this->router->middleware($one)->get('users', function () { + return 'all-users'; + }); + + $this->seeResponse('all-users', Request::create('users', 'GET')); + $this->assertSame(['one'], $this->getRoute()->middleware()); + } + + public function testMiddlewareAsArrayWithStringables() + { + $one = new class implements Stringable + { + public function __toString() + { + return 'one'; + } + }; + + $this->router->middleware([$one, 'two'])->get('users', function () { + return 'all-users'; + }); + + $this->seeResponse('all-users', Request::create('users', 'GET')); + $this->assertSame(['one', 'two'], $this->getRoute()->middleware()); + } + public function testWithoutMiddlewareRegistration() { $this->router->middleware(['one', 'two'])->get('users', function () { @@ -190,6 +227,26 @@ public function testCanRegisterGroupWithMiddleware() $this->seeMiddleware('group-middleware'); } + public function testCanRegisterGroupWithStringableMiddleware() + { + $one = new class implements Stringable + { + public function __toString() + { + return 'one'; + } + }; + + $this->router->middleware($one)->group(function ($router) { + $router->get('users', function () { + return 'all-users'; + }); + }); + + $this->seeResponse('all-users', Request::create('users', 'GET')); + $this->seeMiddleware('one'); + } + public function testCanRegisterGroupWithNamespace() { $this->router->namespace('App\Http\Controllers')->group(function ($router) { @@ -606,6 +663,27 @@ public function testResourceWithoutMiddlewareRegistration() $this->assertEquals(['one'], $this->getRoute()->excludedMiddleware()); } + public function testResourceWithMiddlewareAsStringable() + { + $one = new class implements Stringable + { + public function __toString() + { + return 'one'; + } + }; + + $this->router->resource('users', RouteRegistrarControllerStub::class) + ->only('index') + ->middleware([$one, 'two']) + ->withoutMiddleware('one'); + + $this->seeResponse('controller', Request::create('users', 'GET')); + + $this->assertEquals(['one', 'two'], $this->getRoute()->middleware()); + $this->assertEquals(['one'], $this->getRoute()->excludedMiddleware()); + } + public function testResourceWheres() { $wheres = [