Skip to content
Permalink
Browse files

Rename $stack to $queue and push() to add() in MiddlewareStack.

  • Loading branch information...
ADmad committed Jul 25, 2016
1 parent e0d5c90 commit c97e14cab161dc78e6baf46e0e38a0c6b95e1fc4
@@ -43,7 +43,7 @@ public function __construct($configDir)
}
/**
* @param \Cake\Http\MiddlewareStack $middleware The middleware stack to set in your App Class
* @param \Cake\Http\MiddlewareStack $middleware The middleware queue to set in your App Class
* @return \Cake\Http\MiddlewareStack
*/
abstract public function middleware($middleware);
@@ -18,18 +18,17 @@
use LogicException;
/**
* Provides methods for creating and manipulating a 'stack' of
* middleware callables. This stack is used to process a request and response
* via \Cake\Http\Runner.
* Provides methods for creating and manipulating a "queue" of middleware callables.
* This queue is used to process a request and response via \Cake\Http\Runner.
*/
class MiddlewareStack implements Countable
{
/**
* The stack of middleware callables.
* The queue of middleware callables.
*
* @var array
*/
protected $stack = [];
protected $queue = [];
/**
* Get the middleware object at the provided index.
@@ -40,35 +39,35 @@ class MiddlewareStack implements Countable
*/
public function get($index)
{
if (isset($this->stack[$index])) {
return $this->stack[$index];
if (isset($this->queue[$index])) {
return $this->queue[$index];
}
return null;
}
/**
* Append a middleware callable to the end of the stack.
* Append a middleware callable to the end of the queue.
*
* @param callable $callable The middleware callable to append.
* @return $this
*/
public function push(callable $callable)
public function add(callable $callable)
{
$this->stack[] = $callable;
$this->queue[] = $callable;
return $this;
}
/**
* Prepend a middleware callable to the start of the stack.
* Prepend a middleware callable to the start of the queue.
*
* @param callable $callable The middleware callable to prepend.
* @return $this
*/
public function prepend(callable $callable)
{
array_unshift($this->stack, $callable);
array_unshift($this->queue, $callable);
return $this;
}
@@ -85,7 +84,7 @@ public function prepend(callable $callable)
*/
public function insertAt($index, callable $callable)
{
array_splice($this->stack, $index, 0, $callable);
array_splice($this->queue, $index, 0, $callable);
return $this;
}
@@ -95,7 +94,7 @@ public function insertAt($index, callable $callable)
*
* Finds the index of the first middleware that matches the provided class,
* and inserts the supplied callable before it. If the class is not found,
* this method will behave like push().
* this method will behave like add().
*
* @param string $class The classname to insert the middleware before.
* @param callable $callable The middleware to insert
@@ -104,7 +103,7 @@ public function insertAt($index, callable $callable)
public function insertBefore($class, $callable)
{
$found = false;
foreach ($this->stack as $i => $object) {
foreach ($this->queue as $i => $object) {
if (is_a($object, $class)) {
$found = true;
break;
@@ -121,7 +120,7 @@ public function insertBefore($class, $callable)
*
* Finds the index of the first middleware that matches the provided class,
* and inserts the supplied callable after it. If the class is not found,
* this method will behave like push().
* this method will behave like add().
*
* @param string $class The classname to insert the middleware before.
* @param callable $callable The middleware to insert
@@ -130,7 +129,7 @@ public function insertBefore($class, $callable)
public function insertAfter($class, $callable)
{
$found = false;
foreach ($this->stack as $i => $object) {
foreach ($this->queue as $i => $object) {
if (is_a($object, $class)) {
$found = true;
break;
@@ -140,7 +139,7 @@ public function insertAfter($class, $callable)
return $this->insertAt($i + 1, $callable);
}
return $this->push($callable);
return $this->add($callable);
}
/**
@@ -152,6 +151,6 @@ public function insertAfter($class, $callable)
*/
public function count()
{
return count($this->stack);
return count($this->queue);
}
}
@@ -18,27 +18,27 @@
use Psr\Http\Message\ServerRequestInterface;
/**
* Executes the middleware stack and provides the `next` callable
* that allows the stack to be iterated.
* Executes the middleware queue and provides the `next` callable
* that allows the queue to be iterated.
*/
class Runner
{
/**
* The current index in the middleware stack.
* The current index in the middleware queue.
*
* @var int
*/
protected $index;
/**
* The middleware stack being run.
* The middleware queue being run.
*
* @var MiddlewareStack
*/
protected $middleware;
/**
* @param \Cake\Http\MiddlewareStack $middleware The middleware stack
* @param \Cake\Http\MiddlewareStack $middleware The middleware queue
* @param \Psr\Http\Message\ServerRequestInterface $request The Server Request
* @param \Psr\Http\Message\ResponseInterface $response The response
* @return \Psr\Http\Message\ResponseInterface A response object
@@ -65,7 +65,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
return $next($request, $response, $this);
}
// End of the stack
// End of the queue
return $response;
}
}
@@ -60,7 +60,7 @@ public function __construct(BaseApplication $app)
* - App->middleware() - Attach any application middleware here.
* - Trigger the 'Server.buildMiddleware' event. You can use this to modify the
* from event listeners.
* - Run the middleware stack including the application.
* - Run the middleware queue including the application.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request to use or null.
* @param \Psr\Http\Message\ResponseInterface $response The response to use or null.
@@ -75,10 +75,10 @@ public function run(ServerRequestInterface $request = null, ResponseInterface $r
$middleware = $this->app->middleware(new MiddlewareStack());
if (!($middleware instanceof MiddlewareStack)) {
throw new RuntimeException('The application `middleware` method did not return a middleware stack.');
throw new RuntimeException('The application `middleware` method did not return a middleware queue.');
}
$this->dispatchEvent('Server.buildMiddleware', ['middleware' => $middleware]);
$middleware->push($this->app);
$middleware->add($this->app);
$response = $this->runner->run($middleware, $request, $response);
if (!($response instanceof ResponseInterface)) {
@@ -33,14 +33,14 @@ public function testGet()
$stack = new MiddlewareStack();
$cb = function () {
};
$stack->push($cb);
$stack->add($cb);
$this->assertSame($cb, $stack->get(0));
$this->assertNull($stack->get(1));
}
/**
* Test the return value of push()
* Test the return value of add()
*
* @return void
*/
@@ -49,11 +49,11 @@ public function testPushReturn()
$stack = new MiddlewareStack();
$cb = function () {
};
$this->assertSame($stack, $stack->push($cb));
$this->assertSame($stack, $stack->add($cb));
}
/**
* Test the push orders correctly
* Test the add orders correctly
*
* @return void
*/
@@ -67,10 +67,10 @@ public function testPushOrdering()
$stack = new MiddlewareStack();
$this->assertCount(0, $stack);
$stack->push($one);
$stack->add($one);
$this->assertCount(1, $stack);
$stack->push($two);
$stack->add($two);
$this->assertCount(2, $stack);
$this->assertSame($one, $stack->get(0));
@@ -105,7 +105,7 @@ public function testPrependOrdering()
$stack = new MiddlewareStack();
$this->assertCount(0, $stack);
$stack->push($one);
$stack->add($one);
$this->assertCount(1, $stack);
$stack->prepend($two);
@@ -130,13 +130,13 @@ public function testInsertAt()
};
$stack = new MiddlewareStack();
$stack->push($one)->push($two)->insertAt(0, $three);
$stack->add($one)->add($two)->insertAt(0, $three);
$this->assertSame($three, $stack->get(0));
$this->assertSame($one, $stack->get(1));
$this->assertSame($two, $stack->get(2));
$stack = new MiddlewareStack();
$stack->push($one)->push($two)->insertAt(1, $three);
$stack->add($one)->add($two)->insertAt(1, $three);
$this->assertSame($one, $stack->get(0));
$this->assertSame($three, $stack->get(1));
$this->assertSame($two, $stack->get(2));
@@ -155,7 +155,7 @@ public function testInsertAtOutOfBounds()
};
$stack = new MiddlewareStack();
$stack->push($one)->insertAt(99, $two);
$stack->add($one)->insertAt(99, $two);
$this->assertCount(2, $stack);
$this->assertSame($one, $stack->get(0));
@@ -175,7 +175,7 @@ public function testInsertAtNegative()
};
$stack = new MiddlewareStack();
$stack->push($one)->insertAt(-1, $two);
$stack->add($one)->insertAt(-1, $two);
$this->assertCount(2, $stack);
$this->assertSame($two, $stack->get(0));
@@ -195,7 +195,7 @@ public function testInsertBefore()
$three = function () {
};
$stack = new MiddlewareStack();
$stack->push($one)->push($two)->insertBefore(SampleMiddleware::class, $three);
$stack->add($one)->add($two)->insertBefore(SampleMiddleware::class, $three);
$this->assertCount(3, $stack);
$this->assertSame($one, $stack->get(0));
@@ -218,7 +218,7 @@ public function testInsertBeforeInvalid()
$three = function () {
};
$stack = new MiddlewareStack();
$stack->push($one)->push($two)->insertBefore('InvalidClassName', $three);
$stack->add($one)->add($two)->insertBefore('InvalidClassName', $three);
}
/**
@@ -234,7 +234,7 @@ public function testInsertAfter()
$three = function () {
};
$stack = new MiddlewareStack();
$stack->push($one)->push($two)->insertAfter(SampleMiddleware::class, $three);
$stack->add($one)->add($two)->insertAfter(SampleMiddleware::class, $three);
$this->assertCount(3, $stack);
$this->assertSame($one, $stack->get(0));
@@ -255,7 +255,7 @@ public function testInsertAfterInvalid()
$three = function () {
};
$stack = new MiddlewareStack();
$stack->push($one)->push($two)->insertAfter('InvalidClass', $three);
$stack->add($one)->add($two)->insertAfter('InvalidClass', $three);
$this->assertCount(3, $stack);
$this->assertSame($one, $stack->get(0));
@@ -54,7 +54,7 @@ public function setUp()
*/
public function testRunSingle()
{
$this->stack->push($this->ok);
$this->stack->add($this->ok);
$req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
$res = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->getMock();
@@ -75,7 +75,7 @@ public function testRunResponseReplace()
return $next($req, $res);
};
$this->stack->push($one);
$this->stack->add($one);
$runner = new Runner();
$req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
@@ -109,7 +109,7 @@ public function testRunSequencing()
return $next($req, $res);
};
$this->stack->push($one)->push($two)->push($three);
$this->stack->add($one)->add($two)->add($three);
$runner = new Runner();
$req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
@@ -130,7 +130,7 @@ public function testRunSequencing()
*/
public function testRunExceptionInMiddleware()
{
$this->stack->push($this->ok)->push($this->fail);
$this->stack->add($this->ok)->add($this->fail);
$req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
$res = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->getMock();
@@ -145,7 +145,7 @@ public function testRunExceptionInMiddleware()
*/
public function testRunNextNotCalled()
{
$this->stack->push($this->noNext);
$this->stack->add($this->noNext);
$req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
$res = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->getMock();
@@ -185,7 +185,7 @@ public function testBuildMiddlewareEvent()
$server->eventManager()->on('Server.buildMiddleware', function ($event, $middleware) {
$this->assertInstanceOf('Cake\Http\MiddlewareStack', $middleware);
$middleware->push(function ($req, $res, $next) {
$middleware->add(function ($req, $res, $next) {
$this->called = true;
return $next($req, $res);
@@ -13,7 +13,7 @@ class BadResponseApplication extends BaseApplication
*/
public function middleware($middleware)
{
$middleware->push(function ($req, $res, $next) {
$middleware->add(function ($req, $res, $next) {
return 'Not a response';
});
Oops, something went wrong.

0 comments on commit c97e14c

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