Skip to content

Commit

Permalink
Merge pull request slimphp#785 from qyanu/release-2.4.3
Browse files Browse the repository at this point in the history
branch release-2.4.1 dropped without merging fixes?
  • Loading branch information
Josh Lockhart committed Apr 4, 2014
2 parents 35b566e + b924ce7 commit 0505f97
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Slim/Slim.php
Original file line number Diff line number Diff line change
Expand Up @@ -1237,6 +1237,10 @@ public function clearHooks($name = null)
*/
public function add(\Slim\Middleware $newMiddleware)
{
if(in_array($newMiddleware, $this->middleware)) {
$middleware_class = get_class($newMiddleware);
throw new \RuntimeException("Circular Middleware setup detected. Tried to queue the same Middleware instance ({$middleware_class}) twice.");
}
$newMiddleware->setApplication($this);
$newMiddleware->setNextMiddleware($this->middleware[0]);
array_unshift($this->middleware, $newMiddleware);
Expand Down Expand Up @@ -1295,6 +1299,8 @@ public function run()
echo $body;
}

$this->applyHook('slim.after');

restore_error_handler();
}

Expand Down Expand Up @@ -1333,7 +1339,6 @@ public function call()
$this->stop();
} catch (\Slim\Exception\Stop $e) {
$this->response()->write(ob_get_clean());
$this->applyHook('slim.after');
} catch (\Exception $e) {
if ($this->config('debug')) {
throw $e;
Expand Down
19 changes: 19 additions & 0 deletions tests/SlimTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,25 @@ public function testAddMiddleware()
$this->assertEquals('Hello', $s->response()->header('X-Slim-Test'));
}

/**
* Test exception when adding circular middleware queues
*
* This asserts that the same middleware can NOT be queued twice (usually by accident).
* Circular middleware stack causes a troublesome to debug PHP Fatal error:
*
* > Fatal error: Maximum function nesting level of '100' reached. aborting!
*/
public function testFailureWhenAddingCircularMiddleware()
{
$this->setExpectedException('\RuntimeException');
$middleware = new CustomMiddleware;
$s = new \Slim\Slim;
$s->add($middleware);
$s->add(new CustomMiddleware);
$s->add($middleware);
$s->run();
}

/************************************************
* FLASH MESSAGING
************************************************/
Expand Down

0 comments on commit 0505f97

Please sign in to comment.