Skip to content

Commit c97e14c

Browse files
committed
Rename $stack to $queue and push() to add() in MiddlewareStack.
1 parent e0d5c90 commit c97e14c

File tree

9 files changed

+53
-54
lines changed

9 files changed

+53
-54
lines changed

src/Http/BaseApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function __construct($configDir)
4343
}
4444

4545
/**
46-
* @param \Cake\Http\MiddlewareStack $middleware The middleware stack to set in your App Class
46+
* @param \Cake\Http\MiddlewareStack $middleware The middleware queue to set in your App Class
4747
* @return \Cake\Http\MiddlewareStack
4848
*/
4949
abstract public function middleware($middleware);

src/Http/MiddlewareStack.php

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
1818
use LogicException;
1919

2020
/**
21-
* Provides methods for creating and manipulating a 'stack' of
22-
* middleware callables. This stack is used to process a request and response
23-
* via \Cake\Http\Runner.
21+
* Provides methods for creating and manipulating a "queue" of middleware callables.
22+
* This queue is used to process a request and response via \Cake\Http\Runner.
2423
*/
2524
class MiddlewareStack implements Countable
2625
{
2726
/**
28-
* The stack of middleware callables.
27+
* The queue of middleware callables.
2928
*
3029
* @var array
3130
*/
32-
protected $stack = [];
31+
protected $queue = [];
3332

3433
/**
3534
* Get the middleware object at the provided index.
@@ -40,35 +39,35 @@ class MiddlewareStack implements Countable
4039
*/
4140
public function get($index)
4241
{
43-
if (isset($this->stack[$index])) {
44-
return $this->stack[$index];
42+
if (isset($this->queue[$index])) {
43+
return $this->queue[$index];
4544
}
4645

4746
return null;
4847
}
4948

5049
/**
51-
* Append a middleware callable to the end of the stack.
50+
* Append a middleware callable to the end of the queue.
5251
*
5352
* @param callable $callable The middleware callable to append.
5453
* @return $this
5554
*/
56-
public function push(callable $callable)
55+
public function add(callable $callable)
5756
{
58-
$this->stack[] = $callable;
57+
$this->queue[] = $callable;
5958

6059
return $this;
6160
}
6261

6362
/**
64-
* Prepend a middleware callable to the start of the stack.
63+
* Prepend a middleware callable to the start of the queue.
6564
*
6665
* @param callable $callable The middleware callable to prepend.
6766
* @return $this
6867
*/
6968
public function prepend(callable $callable)
7069
{
71-
array_unshift($this->stack, $callable);
70+
array_unshift($this->queue, $callable);
7271

7372
return $this;
7473
}
@@ -85,7 +84,7 @@ public function prepend(callable $callable)
8584
*/
8685
public function insertAt($index, callable $callable)
8786
{
88-
array_splice($this->stack, $index, 0, $callable);
87+
array_splice($this->queue, $index, 0, $callable);
8988

9089
return $this;
9190
}
@@ -95,7 +94,7 @@ public function insertAt($index, callable $callable)
9594
*
9695
* Finds the index of the first middleware that matches the provided class,
9796
* and inserts the supplied callable before it. If the class is not found,
98-
* this method will behave like push().
97+
* this method will behave like add().
9998
*
10099
* @param string $class The classname to insert the middleware before.
101100
* @param callable $callable The middleware to insert
@@ -104,7 +103,7 @@ public function insertAt($index, callable $callable)
104103
public function insertBefore($class, $callable)
105104
{
106105
$found = false;
107-
foreach ($this->stack as $i => $object) {
106+
foreach ($this->queue as $i => $object) {
108107
if (is_a($object, $class)) {
109108
$found = true;
110109
break;
@@ -121,7 +120,7 @@ public function insertBefore($class, $callable)
121120
*
122121
* Finds the index of the first middleware that matches the provided class,
123122
* and inserts the supplied callable after it. If the class is not found,
124-
* this method will behave like push().
123+
* this method will behave like add().
125124
*
126125
* @param string $class The classname to insert the middleware before.
127126
* @param callable $callable The middleware to insert
@@ -130,7 +129,7 @@ public function insertBefore($class, $callable)
130129
public function insertAfter($class, $callable)
131130
{
132131
$found = false;
133-
foreach ($this->stack as $i => $object) {
132+
foreach ($this->queue as $i => $object) {
134133
if (is_a($object, $class)) {
135134
$found = true;
136135
break;
@@ -140,7 +139,7 @@ public function insertAfter($class, $callable)
140139
return $this->insertAt($i + 1, $callable);
141140
}
142141

143-
return $this->push($callable);
142+
return $this->add($callable);
144143
}
145144

146145
/**
@@ -152,6 +151,6 @@ public function insertAfter($class, $callable)
152151
*/
153152
public function count()
154153
{
155-
return count($this->stack);
154+
return count($this->queue);
156155
}
157156
}

src/Http/Runner.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@
1818
use Psr\Http\Message\ServerRequestInterface;
1919

2020
/**
21-
* Executes the middleware stack and provides the `next` callable
22-
* that allows the stack to be iterated.
21+
* Executes the middleware queue and provides the `next` callable
22+
* that allows the queue to be iterated.
2323
*/
2424
class Runner
2525
{
2626
/**
27-
* The current index in the middleware stack.
27+
* The current index in the middleware queue.
2828
*
2929
* @var int
3030
*/
3131
protected $index;
3232

3333
/**
34-
* The middleware stack being run.
34+
* The middleware queue being run.
3535
*
3636
* @var MiddlewareStack
3737
*/
3838
protected $middleware;
3939

4040
/**
41-
* @param \Cake\Http\MiddlewareStack $middleware The middleware stack
41+
* @param \Cake\Http\MiddlewareStack $middleware The middleware queue
4242
* @param \Psr\Http\Message\ServerRequestInterface $request The Server Request
4343
* @param \Psr\Http\Message\ResponseInterface $response The response
4444
* @return \Psr\Http\Message\ResponseInterface A response object
@@ -65,7 +65,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
6565
return $next($request, $response, $this);
6666
}
6767

68-
// End of the stack
68+
// End of the queue
6969
return $response;
7070
}
7171
}

src/Http/Server.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function __construct(BaseApplication $app)
6060
* - App->middleware() - Attach any application middleware here.
6161
* - Trigger the 'Server.buildMiddleware' event. You can use this to modify the
6262
* from event listeners.
63-
* - Run the middleware stack including the application.
63+
* - Run the middleware queue including the application.
6464
*
6565
* @param \Psr\Http\Message\ServerRequestInterface $request The request to use or null.
6666
* @param \Psr\Http\Message\ResponseInterface $response The response to use or null.
@@ -75,10 +75,10 @@ public function run(ServerRequestInterface $request = null, ResponseInterface $r
7575

7676
$middleware = $this->app->middleware(new MiddlewareStack());
7777
if (!($middleware instanceof MiddlewareStack)) {
78-
throw new RuntimeException('The application `middleware` method did not return a middleware stack.');
78+
throw new RuntimeException('The application `middleware` method did not return a middleware queue.');
7979
}
8080
$this->dispatchEvent('Server.buildMiddleware', ['middleware' => $middleware]);
81-
$middleware->push($this->app);
81+
$middleware->add($this->app);
8282
$response = $this->runner->run($middleware, $request, $response);
8383

8484
if (!($response instanceof ResponseInterface)) {

tests/TestCase/Http/MiddlewareStackTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public function testGet()
3333
$stack = new MiddlewareStack();
3434
$cb = function () {
3535
};
36-
$stack->push($cb);
36+
$stack->add($cb);
3737
$this->assertSame($cb, $stack->get(0));
3838
$this->assertNull($stack->get(1));
3939
}
4040

4141

4242
/**
43-
* Test the return value of push()
43+
* Test the return value of add()
4444
*
4545
* @return void
4646
*/
@@ -49,11 +49,11 @@ public function testPushReturn()
4949
$stack = new MiddlewareStack();
5050
$cb = function () {
5151
};
52-
$this->assertSame($stack, $stack->push($cb));
52+
$this->assertSame($stack, $stack->add($cb));
5353
}
5454

5555
/**
56-
* Test the push orders correctly
56+
* Test the add orders correctly
5757
*
5858
* @return void
5959
*/
@@ -67,10 +67,10 @@ public function testPushOrdering()
6767
$stack = new MiddlewareStack();
6868
$this->assertCount(0, $stack);
6969

70-
$stack->push($one);
70+
$stack->add($one);
7171
$this->assertCount(1, $stack);
7272

73-
$stack->push($two);
73+
$stack->add($two);
7474
$this->assertCount(2, $stack);
7575

7676
$this->assertSame($one, $stack->get(0));
@@ -105,7 +105,7 @@ public function testPrependOrdering()
105105
$stack = new MiddlewareStack();
106106
$this->assertCount(0, $stack);
107107

108-
$stack->push($one);
108+
$stack->add($one);
109109
$this->assertCount(1, $stack);
110110

111111
$stack->prepend($two);
@@ -130,13 +130,13 @@ public function testInsertAt()
130130
};
131131

132132
$stack = new MiddlewareStack();
133-
$stack->push($one)->push($two)->insertAt(0, $three);
133+
$stack->add($one)->add($two)->insertAt(0, $three);
134134
$this->assertSame($three, $stack->get(0));
135135
$this->assertSame($one, $stack->get(1));
136136
$this->assertSame($two, $stack->get(2));
137137

138138
$stack = new MiddlewareStack();
139-
$stack->push($one)->push($two)->insertAt(1, $three);
139+
$stack->add($one)->add($two)->insertAt(1, $three);
140140
$this->assertSame($one, $stack->get(0));
141141
$this->assertSame($three, $stack->get(1));
142142
$this->assertSame($two, $stack->get(2));
@@ -155,7 +155,7 @@ public function testInsertAtOutOfBounds()
155155
};
156156

157157
$stack = new MiddlewareStack();
158-
$stack->push($one)->insertAt(99, $two);
158+
$stack->add($one)->insertAt(99, $two);
159159

160160
$this->assertCount(2, $stack);
161161
$this->assertSame($one, $stack->get(0));
@@ -175,7 +175,7 @@ public function testInsertAtNegative()
175175
};
176176

177177
$stack = new MiddlewareStack();
178-
$stack->push($one)->insertAt(-1, $two);
178+
$stack->add($one)->insertAt(-1, $two);
179179

180180
$this->assertCount(2, $stack);
181181
$this->assertSame($two, $stack->get(0));
@@ -195,7 +195,7 @@ public function testInsertBefore()
195195
$three = function () {
196196
};
197197
$stack = new MiddlewareStack();
198-
$stack->push($one)->push($two)->insertBefore(SampleMiddleware::class, $three);
198+
$stack->add($one)->add($two)->insertBefore(SampleMiddleware::class, $three);
199199

200200
$this->assertCount(3, $stack);
201201
$this->assertSame($one, $stack->get(0));
@@ -218,7 +218,7 @@ public function testInsertBeforeInvalid()
218218
$three = function () {
219219
};
220220
$stack = new MiddlewareStack();
221-
$stack->push($one)->push($two)->insertBefore('InvalidClassName', $three);
221+
$stack->add($one)->add($two)->insertBefore('InvalidClassName', $three);
222222
}
223223

224224
/**
@@ -234,7 +234,7 @@ public function testInsertAfter()
234234
$three = function () {
235235
};
236236
$stack = new MiddlewareStack();
237-
$stack->push($one)->push($two)->insertAfter(SampleMiddleware::class, $three);
237+
$stack->add($one)->add($two)->insertAfter(SampleMiddleware::class, $three);
238238

239239
$this->assertCount(3, $stack);
240240
$this->assertSame($one, $stack->get(0));
@@ -255,7 +255,7 @@ public function testInsertAfterInvalid()
255255
$three = function () {
256256
};
257257
$stack = new MiddlewareStack();
258-
$stack->push($one)->push($two)->insertAfter('InvalidClass', $three);
258+
$stack->add($one)->add($two)->insertAfter('InvalidClass', $three);
259259

260260
$this->assertCount(3, $stack);
261261
$this->assertSame($one, $stack->get(0));

tests/TestCase/Http/RunnerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public function setUp()
5454
*/
5555
public function testRunSingle()
5656
{
57-
$this->stack->push($this->ok);
57+
$this->stack->add($this->ok);
5858
$req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
5959
$res = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->getMock();
6060

@@ -75,7 +75,7 @@ public function testRunResponseReplace()
7575

7676
return $next($req, $res);
7777
};
78-
$this->stack->push($one);
78+
$this->stack->add($one);
7979
$runner = new Runner();
8080

8181
$req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
@@ -109,7 +109,7 @@ public function testRunSequencing()
109109

110110
return $next($req, $res);
111111
};
112-
$this->stack->push($one)->push($two)->push($three);
112+
$this->stack->add($one)->add($two)->add($three);
113113
$runner = new Runner();
114114

115115
$req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
@@ -130,7 +130,7 @@ public function testRunSequencing()
130130
*/
131131
public function testRunExceptionInMiddleware()
132132
{
133-
$this->stack->push($this->ok)->push($this->fail);
133+
$this->stack->add($this->ok)->add($this->fail);
134134
$req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
135135
$res = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->getMock();
136136

@@ -145,7 +145,7 @@ public function testRunExceptionInMiddleware()
145145
*/
146146
public function testRunNextNotCalled()
147147
{
148-
$this->stack->push($this->noNext);
148+
$this->stack->add($this->noNext);
149149
$req = $this->getMockBuilder('Psr\Http\Message\ServerRequestInterface')->getMock();
150150
$res = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->getMock();
151151

tests/TestCase/Http/ServerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public function testBuildMiddlewareEvent()
185185

186186
$server->eventManager()->on('Server.buildMiddleware', function ($event, $middleware) {
187187
$this->assertInstanceOf('Cake\Http\MiddlewareStack', $middleware);
188-
$middleware->push(function ($req, $res, $next) {
188+
$middleware->add(function ($req, $res, $next) {
189189
$this->called = true;
190190

191191
return $next($req, $res);

tests/test_app/TestApp/Http/BadResponseApplication.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class BadResponseApplication extends BaseApplication
1313
*/
1414
public function middleware($middleware)
1515
{
16-
$middleware->push(function ($req, $res, $next) {
16+
$middleware->add(function ($req, $res, $next) {
1717
return 'Not a response';
1818
});
1919

0 commit comments

Comments
 (0)