Skip to content

Commit

Permalink
Rename $stack to $queue and push() to add() in MiddlewareStack.
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Jul 25, 2016
1 parent e0d5c90 commit c97e14c
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/Http/BaseApplication.php
Expand Up @@ -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);
Expand Down
37 changes: 18 additions & 19 deletions src/Http/MiddlewareStack.php
Expand Up @@ -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.
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -140,7 +139,7 @@ public function insertAfter($class, $callable)
return $this->insertAt($i + 1, $callable);
}

return $this->push($callable);
return $this->add($callable);
}

/**
Expand All @@ -152,6 +151,6 @@ public function insertAfter($class, $callable)
*/
public function count()
{
return count($this->stack);
return count($this->queue);
}
}
12 changes: 6 additions & 6 deletions src/Http/Runner.php
Expand Up @@ -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
Expand All @@ -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;
}
}
6 changes: 3 additions & 3 deletions src/Http/Server.php
Expand Up @@ -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.
Expand All @@ -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)) {
Expand Down
30 changes: 15 additions & 15 deletions tests/TestCase/Http/MiddlewareStackTest.php
Expand Up @@ -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
*/
Expand All @@ -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
*/
Expand All @@ -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));
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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);
}

/**
Expand All @@ -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));
Expand All @@ -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));
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase/Http/RunnerTest.php
Expand Up @@ -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();

Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();

Expand All @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/Http/ServerTest.php
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tests/test_app/TestApp/Http/BadResponseApplication.php
Expand Up @@ -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';
});

Expand Down

0 comments on commit c97e14c

Please sign in to comment.