Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Allows Stringable objects as middleware. #39439

Merged
merged 3 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Illuminate/Routing/PendingResourceRegistration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
taylorotwell marked this conversation as resolved.
Show resolved Hide resolved
$middleware[$key] = (string) $value;
}
}

$this->options['middleware'] = $middleware;

return $this;
Expand Down
8 changes: 8 additions & 0 deletions src/Illuminate/Routing/RouteRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
58 changes: 58 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Routing\Router;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Stringable;

class RouteRegistrarTest extends TestCase
{
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -606,6 +643,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 = [
Expand Down