|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Equip\Dispatch; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Http\Middleware\DelegateInterface; |
|
6
|
|
|
use Interop\Http\Middleware\ServerMiddlewareInterface; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\RequestInterface; |
|
9
|
|
|
|
|
10
|
|
|
class Stack implements DelegateInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var callable |
|
14
|
|
|
*/ |
|
15
|
|
|
private $default; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var array |
|
19
|
|
|
*/ |
|
20
|
|
|
private $middleware = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var integer |
|
24
|
|
|
*/ |
|
25
|
|
|
private $index = 0; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param callable $default to call when no middleware is available |
|
29
|
|
|
* @param array $middleware |
|
30
|
|
|
*/ |
|
31
|
3 |
|
public function __construct(callable $default, ...$middleware) |
|
32
|
|
|
{ |
|
33
|
3 |
|
$this->default = $default; |
|
34
|
3 |
|
$this->middleware = $middleware; |
|
35
|
3 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Add a middleware to the end of the stack. |
|
39
|
|
|
* |
|
40
|
|
|
* @param ServerMiddlewareInterface $middleware |
|
41
|
|
|
* |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function append(ServerMiddlewareInterface $middleware) |
|
45
|
|
|
{ |
|
46
|
1 |
|
array_push($this->middleware, $middleware); |
|
47
|
1 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Add a middleware to the beginning of the stack. |
|
51
|
|
|
* |
|
52
|
|
|
* @param ServerMiddlewareInterface $middleware |
|
53
|
|
|
* |
|
54
|
|
|
* @return void |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function prepend(ServerMiddlewareInterface $middleware) |
|
57
|
|
|
{ |
|
58
|
1 |
|
array_unshift($this->middleware, $middleware); |
|
59
|
1 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Process the request using the current middleware. |
|
63
|
|
|
* |
|
64
|
|
|
* @param RequestInterface $request |
|
65
|
|
|
* |
|
66
|
|
|
* @return ResponseInterface |
|
67
|
|
|
*/ |
|
68
|
2 |
|
public function process(RequestInterface $request) |
|
69
|
|
|
{ |
|
70
|
2 |
|
if (empty($this->middleware[$this->index])) { |
|
71
|
2 |
|
return call_user_func($this->default, $request); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
return $this->middleware[$this->index]->process($request, $this->nextDelegate()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get a delegate pointing to the next middleware. |
|
79
|
|
|
* |
|
80
|
|
|
* @return static |
|
81
|
|
|
*/ |
|
82
|
1 |
|
private function nextDelegate() |
|
83
|
|
|
{ |
|
84
|
1 |
|
$copy = clone $this; |
|
85
|
1 |
|
$copy->index++; |
|
86
|
|
|
|
|
87
|
1 |
|
return $copy; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Dispatch the middleware stack. |
|
92
|
|
|
* |
|
93
|
|
|
* @param RequestInterface $request |
|
94
|
|
|
* |
|
95
|
|
|
* @return ResponseInterface |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function dispatch(RequestInterface $request) |
|
98
|
|
|
{ |
|
99
|
2 |
|
return $this->process($request); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|