|
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
|
7 |
|
public function __construct(array $middlewares = []) |
|
22
|
|
|
{ |
|
23
|
7 |
|
$this->middlewares = []; |
|
24
|
|
|
|
|
25
|
7 |
|
foreach ($middlewares as $middleware) { |
|
26
|
1 |
|
$this->add($middleware); |
|
27
|
|
|
} |
|
28
|
7 |
|
} |
|
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
|
5 |
|
public function add(callable $middleware) : self |
|
44
|
|
|
{ |
|
45
|
5 |
|
$this->middlewares[] = $middleware; |
|
46
|
|
|
|
|
47
|
5 |
|
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
|
|
|
$dispatcher = new class($this->middlewares) |
|
84
|
|
|
{ |
|
85
|
|
|
/** |
|
86
|
|
|
* @var callable[] |
|
87
|
|
|
*/ |
|
88
|
|
|
protected $middlewares; |
|
89
|
|
|
|
|
90
|
4 |
|
public function __construct($middlewares) |
|
91
|
|
|
{ |
|
92
|
4 |
|
$this->middlewares = $middlewares; |
|
93
|
4 |
|
} |
|
94
|
|
|
|
|
95
|
4 |
|
public function __invoke( |
|
96
|
|
|
ServerRequestInterface $request, |
|
97
|
|
|
ResponseInterface $response, |
|
98
|
|
|
callable $next = null |
|
99
|
|
|
) : ResponseInterface { |
|
100
|
|
|
/** @var callable $middleware */ |
|
101
|
4 |
|
$middleware = array_shift($this->middlewares); |
|
102
|
|
|
|
|
103
|
4 |
|
if (null !== $middleware) { |
|
104
|
4 |
|
$response = $middleware($request, $response, $this); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
4 |
|
if (null === $next) { |
|
108
|
4 |
|
return $response; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
4 |
|
$skip = function (ServerRequestInterface $request, ResponseInterface $response) { |
|
112
|
4 |
|
return $response; |
|
113
|
4 |
|
}; |
|
114
|
|
|
|
|
115
|
4 |
|
return $next($request, $response, $skip); |
|
116
|
|
|
} |
|
117
|
|
|
}; |
|
118
|
|
|
|
|
119
|
4 |
|
if (null === $next) { |
|
120
|
|
|
$next = function (ServerRequestInterface $request, ResponseInterface $response) { |
|
121
|
|
|
return $response; |
|
122
|
|
|
}; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
4 |
|
return $dispatcher($request, $response, $next); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|