Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middleware #141

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion src/Browser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Clue\React\Buzz\Io\Sender;
use Clue\React\Buzz\Io\Transaction;
use Clue\React\Buzz\Message\MessageFactory;
use Clue\React\Buzz\Middleware\MiddlewareInterface;
use InvalidArgumentException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
Expand All @@ -22,6 +23,9 @@ class Browser
/** @var LoopInterface $loop */
private $loop;

/** @var MiddlewareInterface[] */
private $middleware = array();

/**
* The `Browser` is responsible for sending HTTP requests to your HTTP server
* and keeps track of pending incoming HTTP responses.
Expand Down Expand Up @@ -232,7 +236,38 @@ public function send(RequestInterface $request)
$request = $request->withUri($this->messageFactory->expandBase($request->getUri(), $this->baseUri));
}

return $this->transaction->send($request);
$transaction = $this->transaction;
$chain = $this->createMiddlewareChain($this->middleware, function (RequestInterface $request) use ($transaction) {
return $transaction->send($request);
});

// Call the chain
return $chain($request);
}

/**
* Creates the middleware chain.
*
* @param MiddlewareInterface[] $middleware
* @param callable $requestChainLast
*
* @return callable
*/
private function createMiddlewareChain(array $middleware, $requestChainLast)
{
$middleware = array_reverse($middleware);

// Build request chain
$requestChainNext = $requestChainLast;
foreach ($middleware as $m) {
$lastCallable = function (RequestInterface $request) use ($m, $requestChainNext) {
return $m->handleRequest($request, $requestChainNext);
};

$requestChainNext = $lastCallable;
}

return $requestChainNext;
}

/**
Expand Down Expand Up @@ -335,4 +370,23 @@ public function withOptions(array $options)

return $browser;
}

/**
* Adds a [`MiddlewareInterface`] to the [`Browser`](#browser).
*
* Notice that the [`Browser`](#browser) is an immutable object, i.e. this
* method actually returns a *new* [`Browser`](#browser) instance with the
* options applied.
*
* @param MiddlewareInterface $middleware
*
* @return Browser
*/
public function withMiddleware(MiddlewareInterface $middleware)
{
$browser = clone $this;
$browser->middleware[] = $middleware;

return $browser;
}
}
27 changes: 27 additions & 0 deletions src/Middleware/MiddlewareInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Clue\React\Buzz\Middleware;

use Psr\Http\Message\RequestInterface;

/**
* Interface MiddlewareInterface
*
* @package Clue\React\Buzz\Middleware
*/
interface MiddlewareInterface
{

/**
* Handle a request.
*
* End this function by calling:
* <code>
* return $next($request);
* </code
*
* @param RequestInterface $request
* @param callable $next Next middleware
*/
public function handleRequest(RequestInterface $request, callable $next);
}