Skip to content

Commit 8febec7

Browse files
[8.x] Allows Stringable objects as middleware. (#39439)
* Allows Stringable objects as middleware. * Style changes. * Added test for stringable middleware on group.
1 parent 545181d commit 8febec7

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

src/Illuminate/Routing/PendingResourceRegistration.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ public function parameter($previous, $new)
149149
*/
150150
public function middleware($middleware)
151151
{
152+
if (! is_string($middleware)) {
153+
$middleware = Arr::wrap($middleware);
154+
155+
foreach ($middleware as $key => $value) {
156+
$middleware[$key] = (string) $value;
157+
}
158+
}
159+
152160
$this->options['middleware'] = $middleware;
153161

154162
return $this;

src/Illuminate/Routing/RouteRegistrar.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ public function attribute($key, $value)
9393
throw new InvalidArgumentException("Attribute [{$key}] does not exist.");
9494
}
9595

96+
if ($key === 'middleware' && ! is_string($value)) {
97+
$value = Arr::wrap($value);
98+
99+
foreach ($value as $index => $middleware) {
100+
$value[$index] = (string) $middleware;
101+
}
102+
}
103+
96104
$this->attributes[Arr::get($this->aliases, $key, $key)] = $value;
97105

98106
return $this;

tests/Routing/RouteRegistrarTest.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Routing\Router;
1010
use Mockery as m;
1111
use PHPUnit\Framework\TestCase;
12+
use Stringable;
1213

1314
class RouteRegistrarTest extends TestCase
1415
{
@@ -60,6 +61,42 @@ public function testMiddlewareFluentRegistration()
6061
$this->assertEquals(['seven'], $this->getRoute()->middleware());
6162
}
6263

64+
public function testMiddlewareAsStringableObject()
65+
{
66+
$one = new class implements Stringable
67+
{
68+
public function __toString()
69+
{
70+
return 'one';
71+
}
72+
};
73+
74+
$this->router->middleware($one)->get('users', function () {
75+
return 'all-users';
76+
});
77+
78+
$this->seeResponse('all-users', Request::create('users', 'GET'));
79+
$this->assertSame(['one'], $this->getRoute()->middleware());
80+
}
81+
82+
public function testMiddlewareAsArrayWithStringables()
83+
{
84+
$one = new class implements Stringable
85+
{
86+
public function __toString()
87+
{
88+
return 'one';
89+
}
90+
};
91+
92+
$this->router->middleware([$one, 'two'])->get('users', function () {
93+
return 'all-users';
94+
});
95+
96+
$this->seeResponse('all-users', Request::create('users', 'GET'));
97+
$this->assertSame(['one', 'two'], $this->getRoute()->middleware());
98+
}
99+
63100
public function testWithoutMiddlewareRegistration()
64101
{
65102
$this->router->middleware(['one', 'two'])->get('users', function () {
@@ -190,6 +227,26 @@ public function testCanRegisterGroupWithMiddleware()
190227
$this->seeMiddleware('group-middleware');
191228
}
192229

230+
public function testCanRegisterGroupWithStringableMiddleware()
231+
{
232+
$one = new class implements Stringable
233+
{
234+
public function __toString()
235+
{
236+
return 'one';
237+
}
238+
};
239+
240+
$this->router->middleware($one)->group(function ($router) {
241+
$router->get('users', function () {
242+
return 'all-users';
243+
});
244+
});
245+
246+
$this->seeResponse('all-users', Request::create('users', 'GET'));
247+
$this->seeMiddleware('one');
248+
}
249+
193250
public function testCanRegisterGroupWithNamespace()
194251
{
195252
$this->router->namespace('App\Http\Controllers')->group(function ($router) {
@@ -606,6 +663,27 @@ public function testResourceWithoutMiddlewareRegistration()
606663
$this->assertEquals(['one'], $this->getRoute()->excludedMiddleware());
607664
}
608665

666+
public function testResourceWithMiddlewareAsStringable()
667+
{
668+
$one = new class implements Stringable
669+
{
670+
public function __toString()
671+
{
672+
return 'one';
673+
}
674+
};
675+
676+
$this->router->resource('users', RouteRegistrarControllerStub::class)
677+
->only('index')
678+
->middleware([$one, 'two'])
679+
->withoutMiddleware('one');
680+
681+
$this->seeResponse('controller', Request::create('users', 'GET'));
682+
683+
$this->assertEquals(['one', 'two'], $this->getRoute()->middleware());
684+
$this->assertEquals(['one'], $this->getRoute()->excludedMiddleware());
685+
}
686+
609687
public function testResourceWheres()
610688
{
611689
$wheres = [

0 commit comments

Comments
 (0)