From c0f761a29ba71cef7fff746834149945bb0425c2 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Mon, 1 Nov 2021 16:05:06 -0300 Subject: [PATCH 1/3] Allows Stringable objects as middleware. --- .../Routing/PendingResourceRegistration.php | 8 +++ src/Illuminate/Routing/RouteRegistrar.php | 8 +++ tests/Routing/RouteRegistrarTest.php | 55 +++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/src/Illuminate/Routing/PendingResourceRegistration.php b/src/Illuminate/Routing/PendingResourceRegistration.php index 59e4b8f0b78f..086df064546a 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..0877f4d0d3c0 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,40 @@ 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 () { @@ -606,6 +641,26 @@ 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 = [ From 5662fa1640773aaf4ed6ec863ad97688633edfd9 Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Mon, 1 Nov 2021 16:34:36 -0300 Subject: [PATCH 2/3] Style changes. --- src/Illuminate/Routing/PendingResourceRegistration.php | 2 +- tests/Routing/RouteRegistrarTest.php | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Routing/PendingResourceRegistration.php b/src/Illuminate/Routing/PendingResourceRegistration.php index 086df064546a..b2f20a6f8203 100644 --- a/src/Illuminate/Routing/PendingResourceRegistration.php +++ b/src/Illuminate/Routing/PendingResourceRegistration.php @@ -149,7 +149,7 @@ public function parameter($previous, $new) */ public function middleware($middleware) { - if (!is_string($middleware)) { + if (! is_string($middleware)) { $middleware = Arr::wrap($middleware); foreach ($middleware as $key => $value) { diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 0877f4d0d3c0..44c374e79ce9 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -63,7 +63,8 @@ public function testMiddlewareFluentRegistration() public function testMiddlewareAsStringableObject() { - $one = new class implements Stringable { + $one = new class implements Stringable + { public function __toString() { return 'one'; @@ -80,7 +81,8 @@ public function __toString() public function testMiddlewareAsArrayWithStringables() { - $one = new class implements Stringable { + $one = new class implements Stringable + { public function __toString() { return 'one'; @@ -643,7 +645,8 @@ public function testResourceWithoutMiddlewareRegistration() public function testResourceWithMiddlewareAsStringable() { - $one = new class implements Stringable { + $one = new class implements Stringable + { public function __toString() { return 'one'; From ba79d4ba61c39f5dbc3f31169dd79ba2e53767ab Mon Sep 17 00:00:00 2001 From: DarkGhosthunter Date: Tue, 2 Nov 2021 11:46:51 -0300 Subject: [PATCH 3/3] Added test for stringable middleware on group. --- tests/Routing/RouteRegistrarTest.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/Routing/RouteRegistrarTest.php b/tests/Routing/RouteRegistrarTest.php index 44c374e79ce9..48870bec0e47 100644 --- a/tests/Routing/RouteRegistrarTest.php +++ b/tests/Routing/RouteRegistrarTest.php @@ -227,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) {