|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thruster\Component\HttpMiddleware; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Middlewares |
|
10
|
|
|
* |
|
11
|
|
|
* @package Thruster\Component\HttpMiddleware |
|
12
|
|
|
* @author Aurimas Niekis <[email protected]> |
|
13
|
|
|
*/ |
|
14
|
|
|
class Middlewares |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var callable[] |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $middlewares; |
|
20
|
|
|
|
|
21
|
8 |
|
public function __construct(array $middlewares = []) |
|
22
|
|
|
{ |
|
23
|
8 |
|
$this->middlewares = []; |
|
24
|
|
|
|
|
25
|
8 |
|
foreach ($middlewares as $middleware) { |
|
26
|
1 |
|
$this->add($middleware); |
|
27
|
|
|
} |
|
28
|
8 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @return callable[] |
|
32
|
|
|
*/ |
|
33
|
1 |
|
public function all() : array |
|
34
|
|
|
{ |
|
35
|
1 |
|
return $this->middlewares; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param callable $middleware |
|
40
|
|
|
* |
|
41
|
|
|
* @return Middlewares |
|
42
|
|
|
*/ |
|
43
|
6 |
|
public function add(callable $middleware) : self |
|
44
|
|
|
{ |
|
45
|
6 |
|
$this->middlewares[] = $middleware; |
|
46
|
|
|
|
|
47
|
6 |
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param callable $middleware |
|
52
|
|
|
* |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function has(callable $middleware) : bool |
|
56
|
|
|
{ |
|
57
|
1 |
|
return false !== array_search($middleware, $this->middlewares, true); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param callable $middleware |
|
62
|
|
|
* |
|
63
|
|
|
* @return Middlewares |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function remove(callable $middleware) : self |
|
66
|
|
|
{ |
|
67
|
1 |
|
$key = array_search($middleware, $this->middlewares, true); |
|
68
|
|
|
|
|
69
|
1 |
|
if (false === $key) { |
|
70
|
1 |
|
return $this; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
unset($this->middlewares[$key]); |
|
74
|
|
|
|
|
75
|
1 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function __invoke( |
|
79
|
|
|
ServerRequestInterface $request, |
|
80
|
|
|
ResponseInterface $response, |
|
81
|
|
|
callable $next = null |
|
82
|
|
|
) : ResponseInterface |
|
83
|
|
|
{ |
|
84
|
|
|
$dispatcher = new class($this->middlewares) |
|
85
|
|
|
{ |
|
86
|
|
|
/** |
|
87
|
|
|
* @var callable[] |
|
88
|
|
|
*/ |
|
89
|
|
|
protected $middlewares; |
|
90
|
|
|
|
|
91
|
5 |
|
public function __construct($middlewares) |
|
92
|
|
|
{ |
|
93
|
5 |
|
$this->middlewares = $middlewares; |
|
94
|
5 |
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
public function __invoke( |
|
97
|
|
|
ServerRequestInterface $request, |
|
98
|
|
|
ResponseInterface $response, |
|
99
|
|
|
callable $next = null |
|
100
|
|
|
) : ResponseInterface { |
|
101
|
|
|
/** @var callable $middleware */ |
|
102
|
5 |
|
$middleware = array_shift($this->middlewares); |
|
103
|
|
|
|
|
104
|
|
|
$skip = function (ServerRequestInterface $request, ResponseInterface $response) { |
|
105
|
5 |
|
return $response; |
|
106
|
5 |
|
}; |
|
107
|
|
|
|
|
108
|
5 |
|
if (count($this->middlewares) < 1) { |
|
109
|
5 |
|
if (null === $next) { |
|
110
|
1 |
|
$callback = $skip; |
|
111
|
|
|
} else { |
|
112
|
|
|
$callback = function ( |
|
113
|
|
|
ServerRequestInterface $request, |
|
114
|
|
|
ResponseInterface $response |
|
115
|
|
|
) use ( |
|
116
|
4 |
|
$next, |
|
117
|
4 |
|
$skip |
|
118
|
|
|
) { |
|
119
|
4 |
|
return $next($request, $response, $skip); |
|
120
|
4 |
|
}; |
|
121
|
|
|
|
|
122
|
4 |
|
if (null === $middleware) { |
|
123
|
5 |
|
return $callback($request, $response, $callback); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} else { |
|
127
|
5 |
|
$callback = function ( |
|
128
|
|
|
ServerRequestInterface $request, |
|
129
|
|
|
ResponseInterface $response |
|
130
|
|
|
) use ( |
|
131
|
5 |
|
$next |
|
132
|
|
|
) { |
|
133
|
5 |
|
return $this->__invoke($request, $response, $next); |
|
134
|
5 |
|
}; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
5 |
|
if (null !== $middleware) { |
|
138
|
5 |
|
$response = $middleware( |
|
139
|
|
|
$request, |
|
140
|
|
|
$response, |
|
141
|
|
|
$callback |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
5 |
|
return $response; |
|
146
|
|
|
} |
|
147
|
|
|
}; |
|
148
|
|
|
|
|
149
|
5 |
|
return $dispatcher($request, $response, $next); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
|