/
Dispatcher.php
90 lines (76 loc) · 2.46 KB
/
Dispatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace Middlewares\Utils;
use Closure;
use Interop\Http\Server\MiddlewareInterface;
use Interop\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use UnexpectedValueException;
class Dispatcher
{
/**
* @var MiddlewareInterface[]
*/
private $stack;
/**
* Static helper to create and dispatch a request.
*
* @param MiddlewareInterface[]
* @param ServerRequestInterface|null $request
*
* @return ResponseInterface
*/
public static function run(array $stack, ServerRequestInterface $request = null)
{
if ($request === null) {
$request = Factory::createServerRequest();
}
return (new static($stack))->dispatch($request);
}
/**
* @param MiddlewareInterface[] $stack middleware stack (with at least one middleware component)
*/
public function __construct(array $stack)
{
$this->stack = $stack;
}
/**
* Dispatches the middleware stack and returns the resulting `ResponseInterface`.
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function dispatch(ServerRequestInterface $request)
{
$resolved = $this->resolve(0);
return $resolved->handle($request);
}
/**
* @param int $index middleware stack index
*
* @return RequestHandlerInterface
*/
private function resolve($index)
{
return new RequestHandler(function (ServerRequestInterface $request) use ($index) {
$middleware = isset($this->stack[$index]) ? $this->stack[$index] : new CallableMiddleware(function () {
});
if ($middleware instanceof Closure) {
$middleware = new CallableMiddleware($middleware);
}
if (!($middleware instanceof MiddlewareInterface)) {
throw new UnexpectedValueException(
sprintf('The middleware must be an instance of %s', MiddlewareInterface::class)
);
}
$response = $middleware->process($request, $this->resolve($index + 1));
if (!($response instanceof ResponseInterface)) {
throw new UnexpectedValueException(
sprintf('The middleware must return an instance of %s', ResponseInterface::class)
);
}
return $response;
});
}
}