Skip to content
Browse files

Ensure the app is always last in the middleware stack.

By ensuring the app is always bound after the event, event listeners can
be less careful. Without this plugins would always have to use
insertBefore() which is kind of awkward.
  • Loading branch information...
markstory committed May 30, 2016
1 parent 933f814 commit f9e013eddc4eb5e5939b0632ed77f6bef851a0d8
Showing with 12 additions and 5 deletions.
  1. +1 −1 src/Http/Server.php
  2. +11 −4 tests/TestCase/Http/ServerTest.php
@@ -77,8 +77,8 @@ public function run(ServerRequestInterface $request = null, ResponseInterface $r
if (!($middleware instanceof MiddlewareStack)) {
throw new RuntimeException('The application `middleware` method did not return a middleware stack.');
$this->dispatchEvent('Server.buildMiddleware', ['middleware' => $middleware]);
$response = $this->runner->run($middleware, $request, $response);
if (!($response instanceof ResponseInterface)) {
@@ -179,12 +179,19 @@ public function testBuildMiddlewareEvent()
$app = new MiddlewareApplication($this->config);
$server = new Server($app);
$called = false;
$server->eventManager()->on('Server.buildMiddleware', function ($event, $middleware) use (&$called) {
$called = true;
$this->called = false;
$server->eventManager()->on('Server.buildMiddleware', function ($event, $middleware) {
$this->assertInstanceOf('Cake\Http\MiddlewareStack', $middleware);
$middleware->push(function ($req, $res, $next) {
$this->called = true;
return $next($req, $res);
$this->middleware = $middleware;
$this->assertTrue($called, 'Event not triggered.');
$this->assertTrue($this->called, 'Middleware added in the event was not triggered.');
$this->assertInstanceOf('Closure', $this->middleware->get(3), '2nd last middleware is a clousure');
$this->assertSame($app, $this->middleware->get(4), 'Last middleware is an app instance');

0 comments on commit f9e013e

Please sign in to comment.
You can’t perform that action at this time.