Skip to content

Commit

Permalink
Rename classes by intent
Browse files Browse the repository at this point in the history
Stack is actually a middleware pipe.

DelegateToCallableAdapter is actually a proxy.

Refs #4
  • Loading branch information
shadowhand committed Nov 23, 2016
1 parent 24134cd commit 399a2ab
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ composer require equip/dispatch

## Usage

The `Stack` is a container for middleware that acts as the entry point. It takes
The `MiddlewarePipe` is a container for middleware that acts as the entry point. It takes
two arguments:

- An array of `$middleware` which must be instances of server middleware.
Expand All @@ -36,7 +36,7 @@ the response for output.
### Example

```php
use Equip\Dispatch\Stack;
use Equip\Dispatch\MiddlewarePipe;

// Any implementation of PSR-15 ServerMiddlewareInterface
$middleware = [
Expand All @@ -50,7 +50,7 @@ $default = function (RequestInterface $request) {
return new Response();
};

$stack = new Stack($middleware);
$stack = new MiddlewarePipe($middleware);

// Any implementation of PSR-7 ServerRequestInterface
$request = ServerRequest::fromGlobals();
Expand Down
3 changes: 2 additions & 1 deletion src/DelegateToCallableAdapter.php → src/DelegateProxy.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

namespace Equip\Dispatch;

use Interop\Http\Middleware\DelegateInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class DelegateToCallableAdapter
class DelegateProxy
{
/**
* @var DelegateInterface
Expand Down
21 changes: 8 additions & 13 deletions src/Stack.php → src/MiddlewarePipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class Stack implements ServerMiddlewareInterface
class MiddlewarePipe implements ServerMiddlewareInterface
{
/**
* @var array
Expand Down Expand Up @@ -47,31 +47,26 @@ public function prepend(ServerMiddlewareInterface $middleware)
}

/**
* Process an incoming server request and return a response, optionally delegating
* to the next middleware component to create the response.
* Dispatch the middleware stack.
*
* @param ServerRequestInterface $request
* @param DelegateInterface $nextContainerDelegate
* @param callable $default to call when no middleware is available
*
* @return ResponseInterface
*/
public function process(ServerRequestInterface $request, DelegateInterface $nextContainerDelegate)
public function dispatch(ServerRequestInterface $request, callable $default)
{
$delegate = new Delegate($this->middleware, new DelegateToCallableAdapter($nextContainerDelegate));
$delegate = new Delegate($this->middleware, $default);

return $delegate->process($request);
}

/**
* Dispatch the middleware stack.
*
* @param ServerRequestInterface $request
* @param callable $default to call when no middleware is available
*
* @return ResponseInterface
* @inheritdoc
*/
public function dispatch(ServerRequestInterface $request, callable $default)
public function process(ServerRequestInterface $request, DelegateInterface $nextContainerDelegate)
{
$default = new DelegateProxy($nextContainerDelegate);
$delegate = new Delegate($this->middleware, $default);

return $delegate->process($request);
Expand Down
34 changes: 17 additions & 17 deletions tests/StackTest.php → tests/MiddlewarePipeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class StackTest extends TestCase
class MiddlewarePipeTest extends TestCase
{
public function testDefault()
{
Expand All @@ -18,8 +18,8 @@ public function testDefault()
$default = $this->defaultReturnsResponse($response);

// Run
$stack = new Stack();
$output = $stack->dispatch($request, $default);
$pipe = new MiddlewarePipe();
$output = $pipe->dispatch($request, $default);

// Verify
$this->assertSame($response, $output);
Expand Down Expand Up @@ -51,8 +51,8 @@ public function testMiddleware()
}, $mocks);

// Run
$stack = new Stack($middleware);
$output = $stack->dispatch($request, $default);
$pipe = new MiddlewarePipe($middleware);
$output = $pipe->dispatch($request, $default);

// Verify
Phony::inOrder(
Expand All @@ -73,27 +73,27 @@ public function testAppendAndPrepend()
$three = Phony::mock(ServerMiddlewareInterface::class)->get();

// Run
$stack = new Stack([$one]);
$accessible_stack = Liberator::liberate($stack);
$pipe = new MiddlewarePipe([$one]);
$accessible_pipe = Liberator::liberate($pipe);

// Verify
$this->assertCount(1, $accessible_stack->middleware);
$this->assertSame([$one], $accessible_stack->middleware);
$this->assertCount(1, $accessible_pipe->middleware);
$this->assertSame([$one], $accessible_pipe->middleware);

// Add another middleware to the end
$stack->append($two);
$pipe->append($two);

// Verify
$this->assertSame([$one, $two], $accessible_stack->middleware);
$this->assertSame([$one, $two], $accessible_pipe->middleware);

// Add another middleware to the beginning
$stack->prepend($three);
$pipe->prepend($three);

// Verify
$this->assertSame([$three, $one, $two], $accessible_stack->middleware);
$this->assertSame([$three, $one, $two], $accessible_pipe->middleware);
}

public function testStackAsMiddleware()
public function testMiddlewarePipeAsMiddleware()
{
$request = $this->mockRequest();
$response = $this->mockResponse();
Expand Down Expand Up @@ -121,10 +121,10 @@ public function testStackAsMiddleware()
return $middleware->get();
}, $mocks);

$stack = new Stack([
$pipe = new MiddlewarePipe([
$middleware[0],
$middleware[1],
new Stack([
new MiddlewarePipe([
$middleware[2],
$middleware[3],
]),
Expand All @@ -133,7 +133,7 @@ public function testStackAsMiddleware()
]);

// Run
$output = $stack->dispatch($request, $default);
$output = $pipe->dispatch($request, $default);

// Verify
Phony::inOrder(
Expand Down

0 comments on commit 399a2ab

Please sign in to comment.