Skip to content

Add parameters support to terminable middleware #55488

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

Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/Illuminate/Foundation/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,12 @@ protected function terminateMiddleware($request, $response)
continue;
}

[$name] = $this->parseMiddleware($middleware);
[$name, $parameters] = $this->parseMiddleware($middleware);

$instance = $this->app->make($name);

if (method_exists($instance, 'terminate')) {
$instance->terminate($request, $response);
$instance->terminate($request, $response, ...$parameters);
}
}
}
Expand Down
40 changes: 40 additions & 0 deletions tests/Foundation/Http/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,46 @@ public function terminate($request, $response)
], $called);
}

public function testItTriggersTerminatingEventWithParameters()
{
$called = [];
$app = $this->getApplication();
$events = new Dispatcher($app);
$app->instance('events', $events);
$kernel = new Kernel($app, $this->getRouter());
$app->instance('terminating-middleware', new class($called)
{
public function __construct(private &$called)
{
//
}

public function terminate($request, $response, ...$parameters)
{
$this->called[] = $parameters;
$this->called[] = 'terminating middleware with parameters';
}
});
$kernel->setGlobalMiddleware([
'terminating-middleware:foo,bar',
]);
$events->listen(function (Terminating $terminating) use (&$called) {
$called[] = 'terminating event';
});
$app->terminating(function () use (&$called) {
$called[] = 'terminating callback';
});

$kernel->terminate(new Request(), new Response());

$this->assertSame([
'terminating event',
['foo', 'bar'],
'terminating middleware with parameters',
'terminating callback',
], $called);
}

/**
* @return \Illuminate\Contracts\Foundation\Application
*/
Expand Down