Browse Source

Merge pull request #9168 from cakephp/middleware-rename

Rename MiddlewareStack
Mark Story 9 years ago
parent
commit
5c67728f45

+ 2 - 2
src/Http/BaseApplication.php

@@ -43,8 +43,8 @@ abstract class BaseApplication
     }
 
     /**
-     * @param \Cake\Http\MiddlewareStack $middleware The middleware stack to set in your App Class
-     * @return \Cake\Http\MiddlewareStack
+     * @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to set in your App Class
+     * @return \Cake\Http\MiddlewareQueue
      */
     abstract public function middleware($middleware);
 

+ 33 - 22
src/Http/MiddlewareStack.php

@@ -18,18 +18,17 @@ use Countable;
 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
+class MiddlewareQueue 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,47 @@ 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.
+     * Alias for MiddlewareQueue::add().
+     *
+     * @param callable $callable The middleware callable to append.
+     * @return $this
+     * @see MiddlewareQueue::add()
+     */
+    public function push(callable $callable)
+    {
+        return $this->add($callable);
+    }
+
+    /**
+     * 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 +96,7 @@ class MiddlewareStack implements Countable
      */
     public function insertAt($index, callable $callable)
     {
-        array_splice($this->stack, $index, 0, $callable);
+        array_splice($this->queue, $index, 0, $callable);
 
         return $this;
     }
@@ -95,16 +106,16 @@ class MiddlewareStack implements Countable
      *
      * 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
      * @return $this
      */
-    public function insertBefore($class, $callable)
+    public function insertBefore($class, callable $callable)
     {
         $found = false;
-        foreach ($this->stack as $i => $object) {
+        foreach ($this->queue as $i => $object) {
             if (is_a($object, $class)) {
                 $found = true;
                 break;
@@ -121,16 +132,16 @@ class MiddlewareStack implements Countable
      *
      * 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
      * @return $this
      */
-    public function insertAfter($class, $callable)
+    public function insertAfter($class, callable $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 +151,7 @@ class MiddlewareStack implements Countable
             return $this->insertAt($i + 1, $callable);
         }
 
-        return $this->push($callable);
+        return $this->add($callable);
     }
 
     /**
@@ -152,6 +163,6 @@ class MiddlewareStack implements Countable
      */
     public function count()
     {
-        return count($this->stack);
+        return count($this->queue);
     }
 }

+ 7 - 7
src/Http/Runner.php

@@ -18,27 +18,27 @@ use Psr\Http\Message\ResponseInterface;
 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
+     * @var MiddlewareQueue
      */
     protected $middleware;
 
     /**
-     * @param \Cake\Http\MiddlewareStack $middleware The middleware stack
+     * @param \Cake\Http\MiddlewareQueue $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 @@ class Runner
             return $next($request, $response, $this);
         }
 
-        // End of the stack
+        // End of the queue
         return $response;
     }
 }

+ 5 - 5
src/Http/Server.php

@@ -60,7 +60,7 @@ class Server
      * - 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.
@@ -73,12 +73,12 @@ class Server
         $request = $request ?: ServerRequestFactory::fromGlobals();
         $response = $response ?: new Response();
 
-        $middleware = $this->app->middleware(new MiddlewareStack());
-        if (!($middleware instanceof MiddlewareStack)) {
-            throw new RuntimeException('The application `middleware` method did not return a middleware stack.');
+        $middleware = $this->app->middleware(new MiddlewareQueue());
+        if (!($middleware instanceof MiddlewareQueue)) {
+            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)) {

+ 31 - 31
tests/TestCase/Http/MiddlewareStackTest.php

@@ -14,14 +14,14 @@
  */
 namespace Cake\Test\TestCase\Http;
 
-use Cake\Http\MiddlewareStack;
+use Cake\Http\MiddlewareQueue;
 use Cake\TestSuite\TestCase;
 use TestApp\Middleware\SampleMiddleware;
 
 /**
- * Test case for the MiddlewareStack
+ * Test case for the MiddlewareQueue
  */
-class MiddlewareStackTest extends TestCase
+class MiddlewareQueueTest extends TestCase
 {
     /**
      * Test get()
@@ -30,30 +30,30 @@ class MiddlewareStackTest extends TestCase
      */
     public function testGet()
     {
-        $stack = new MiddlewareStack();
+        $stack = new MiddlewareQueue();
         $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
      */
     public function testPushReturn()
     {
-        $stack = new MiddlewareStack();
+        $stack = new MiddlewareQueue();
         $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
      */
@@ -64,13 +64,13 @@ class MiddlewareStackTest extends TestCase
         $two = function () {
         };
 
-        $stack = new MiddlewareStack();
+        $stack = new MiddlewareQueue();
         $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));
@@ -86,7 +86,7 @@ class MiddlewareStackTest extends TestCase
     {
         $cb = function () {
         };
-        $stack = new MiddlewareStack();
+        $stack = new MiddlewareQueue();
         $this->assertSame($stack, $stack->prepend($cb));
     }
 
@@ -102,10 +102,10 @@ class MiddlewareStackTest extends TestCase
         $two = function () {
         };
 
-        $stack = new MiddlewareStack();
+        $stack = new MiddlewareQueue();
         $this->assertCount(0, $stack);
 
-        $stack->push($one);
+        $stack->add($one);
         $this->assertCount(1, $stack);
 
         $stack->prepend($two);
@@ -129,14 +129,14 @@ class MiddlewareStackTest extends TestCase
         $three = function () {
         };
 
-        $stack = new MiddlewareStack();
-        $stack->push($one)->push($two)->insertAt(0, $three);
+        $stack = new MiddlewareQueue();
+        $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 = new MiddlewareQueue();
+        $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));
@@ -154,8 +154,8 @@ class MiddlewareStackTest extends TestCase
         $two = function () {
         };
 
-        $stack = new MiddlewareStack();
-        $stack->push($one)->insertAt(99, $two);
+        $stack = new MiddlewareQueue();
+        $stack->add($one)->insertAt(99, $two);
 
         $this->assertCount(2, $stack);
         $this->assertSame($one, $stack->get(0));
@@ -174,8 +174,8 @@ class MiddlewareStackTest extends TestCase
         $two = function () {
         };
 
-        $stack = new MiddlewareStack();
-        $stack->push($one)->insertAt(-1, $two);
+        $stack = new MiddlewareQueue();
+        $stack->add($one)->insertAt(-1, $two);
 
         $this->assertCount(2, $stack);
         $this->assertSame($two, $stack->get(0));
@@ -194,8 +194,8 @@ class MiddlewareStackTest extends TestCase
         $two = new SampleMiddleware();
         $three = function () {
         };
-        $stack = new MiddlewareStack();
-        $stack->push($one)->push($two)->insertBefore(SampleMiddleware::class, $three);
+        $stack = new MiddlewareQueue();
+        $stack->add($one)->add($two)->insertBefore(SampleMiddleware::class, $three);
 
         $this->assertCount(3, $stack);
         $this->assertSame($one, $stack->get(0));
@@ -217,8 +217,8 @@ class MiddlewareStackTest extends TestCase
         $two = new SampleMiddleware();
         $three = function () {
         };
-        $stack = new MiddlewareStack();
-        $stack->push($one)->push($two)->insertBefore('InvalidClassName', $three);
+        $stack = new MiddlewareQueue();
+        $stack->add($one)->add($two)->insertBefore('InvalidClassName', $three);
     }
 
     /**
@@ -233,8 +233,8 @@ class MiddlewareStackTest extends TestCase
         };
         $three = function () {
         };
-        $stack = new MiddlewareStack();
-        $stack->push($one)->push($two)->insertAfter(SampleMiddleware::class, $three);
+        $stack = new MiddlewareQueue();
+        $stack->add($one)->add($two)->insertAfter(SampleMiddleware::class, $three);
 
         $this->assertCount(3, $stack);
         $this->assertSame($one, $stack->get(0));
@@ -254,8 +254,8 @@ class MiddlewareStackTest extends TestCase
         };
         $three = function () {
         };
-        $stack = new MiddlewareStack();
-        $stack->push($one)->push($two)->insertAfter('InvalidClass', $three);
+        $stack = new MiddlewareQueue();
+        $stack->add($one)->add($two)->insertAfter('InvalidClass', $three);
 
         $this->assertCount(3, $stack);
         $this->assertSame($one, $stack->get(0));

+ 7 - 7
tests/TestCase/Http/RunnerTest.php

@@ -14,7 +14,7 @@
  */
 namespace Cake\Test\TestCase;
 
-use Cake\Http\MiddlewareStack;
+use Cake\Http\MiddlewareQueue;
 use Cake\Http\Runner;
 use Cake\TestSuite\TestCase;
 use RuntimeException;
@@ -32,7 +32,7 @@ class RunnerTest extends TestCase
     public function setUp()
     {
         parent::setUp();
-        $this->stack = new MiddlewareStack();
+        $this->stack = new MiddlewareQueue();
 
         $this->ok = function ($req, $res, $next) {
             return $next($req, $res);
@@ -54,7 +54,7 @@ class RunnerTest extends TestCase
      */
     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 @@ class RunnerTest extends TestCase
 
             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 @@ class RunnerTest extends TestCase
 
             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 @@ class RunnerTest extends TestCase
      */
     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 @@ class RunnerTest extends TestCase
      */
     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();
 

+ 2 - 2
tests/TestCase/Http/ServerTest.php

@@ -184,8 +184,8 @@ class ServerTest extends TestCase
         $this->called = false;
 
         $server->eventManager()->on('Server.buildMiddleware', function ($event, $middleware) {
-            $this->assertInstanceOf('Cake\Http\MiddlewareStack', $middleware);
-            $middleware->push(function ($req, $res, $next) {
+            $this->assertInstanceOf('Cake\Http\MiddlewareQueue', $middleware);
+            $middleware->add(function ($req, $res, $next) {
                 $this->called = true;
 
                 return $next($req, $res);

+ 2 - 2
tests/test_app/TestApp/Application.php

@@ -33,8 +33,8 @@ class Application extends BaseApplication
 
     public function middleware($middleware)
     {
-        $middleware->push(new RoutingMiddleware());
-        $middleware->push(function ($req, $res, $next) {
+        $middleware->add(new RoutingMiddleware());
+        $middleware->add(function ($req, $res, $next) {
             $res = $next($req, $res);
 
             return $res->withHeader('X-Middleware', 'true');

+ 3 - 3
tests/test_app/TestApp/Http/BadResponseApplication.php

@@ -8,12 +8,12 @@ use Psr\Http\Message\ServerRequestInterface;
 class BadResponseApplication extends BaseApplication
 {
     /**
-     * @param \Cake\Http\MiddlewareStack $middleware The middleware stack to set in your App Class
-     * @return \Cake\Http\MiddlewareStack
+     * @param \Cake\Http\MiddlewareQueue $middleware The middleware stack to set in your App Class
+     * @return \Cake\Http\MiddlewareQueue
      */
     public function middleware($middleware)
     {
-        $middleware->push(function ($req, $res, $next) {
+        $middleware->add(function ($req, $res, $next) {
             return 'Not a response';
         });
 

+ 1 - 1
tests/test_app/TestApp/Http/InvalidMiddlewareApplication.php

@@ -6,7 +6,7 @@ use Cake\Http\BaseApplication;
 class InvalidMiddlewareApplication extends BaseApplication
 {
     /**
-     * @param \Cake\Http\MiddlewareStack $middleware The middleware stack to set in your App Class
+     * @param \Cake\Http\MiddlewareQueue $middleware The middleware stack to set in your App Class
      * @return null
      */
     public function middleware($middleware)

+ 5 - 5
tests/test_app/TestApp/Http/MiddlewareApplication.php

@@ -8,23 +8,23 @@ use Psr\Http\Message\ServerRequestInterface;
 class MiddlewareApplication extends BaseApplication
 {
     /**
-     * @param \Cake\Http\MiddlewareStack $middleware The middleware stack to set in your App Class
-     * @return \Cake\Http\MiddlewareStack
+     * @param \Cake\Http\MiddlewareQueue $middleware The middleware stack to set in your App Class
+     * @return \Cake\Http\MiddlewareQueue
      */
     public function middleware($middleware)
     {
         $middleware
-            ->push(function ($req, $res, $next) {
+            ->add(function ($req, $res, $next) {
                 $res = $res->withHeader('X-First', 'first');
 
                 return $next($req, $res);
             })
-            ->push(function ($req, $res, $next) {
+            ->add(function ($req, $res, $next) {
                 $res = $res->withHeader('X-Second', 'second');
 
                 return $next($req, $res);
             })
-            ->push(function ($req, $res, $next) {
+            ->add(function ($req, $res, $next) {
                 if ($req->hasHeader('X-pass')) {
                     $res = $res->withHeader('X-pass', $req->getHeaderLine('X-pass'));
                 }