Skip to content

Commit

Permalink
[8.x] Allows Stringable objects as middleware. (#39439)
Browse files Browse the repository at this point in the history
* Allows Stringable objects as middleware.

* Style changes.

* Added test for stringable middleware on group.
  • Loading branch information
DarkGhostHunter authored Nov 2, 2021
1 parent 545181d commit 8febec7
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
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) {
$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
78 changes: 78 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 @@ -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) {
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit 8febec7

Please sign in to comment.