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 RequestMiddlewares |
10
|
|
|
* |
11
|
|
|
* @package Thruster\Component\HttpMiddleware |
12
|
|
|
* @author Aurimas Niekis <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class RequestMiddlewares |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Middlewares |
18
|
|
|
*/ |
19
|
|
|
protected $preMiddlewares; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Middlewares |
23
|
|
|
*/ |
24
|
|
|
protected $postMiddlewares; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return Middlewares |
28
|
|
|
*/ |
29
|
3 |
|
public function getPreMiddlewares() : Middlewares |
30
|
|
|
{ |
31
|
3 |
|
if ($this->preMiddlewares) { |
32
|
3 |
|
return $this->preMiddlewares; |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
$this->preMiddlewares = new Middlewares() ; |
36
|
|
|
|
37
|
2 |
|
return $this->preMiddlewares; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Middlewares $preMiddlewares |
42
|
|
|
* |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
1 |
|
public function setPreMiddlewares(Middlewares $preMiddlewares) |
46
|
|
|
{ |
47
|
1 |
|
$this->preMiddlewares = $preMiddlewares; |
48
|
|
|
|
49
|
1 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return Middlewares |
54
|
|
|
*/ |
55
|
3 |
|
public function getPostMiddlewares() : Middlewares |
56
|
|
|
{ |
57
|
3 |
|
if ($this->postMiddlewares) { |
58
|
3 |
|
return $this->postMiddlewares; |
59
|
|
|
} |
60
|
|
|
|
61
|
2 |
|
$this->postMiddlewares = new Middlewares(); |
62
|
|
|
|
63
|
2 |
|
return $this->postMiddlewares; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param Middlewares $postMiddlewares |
68
|
|
|
* |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
1 |
|
public function setPostMiddlewares(Middlewares $postMiddlewares) |
72
|
|
|
{ |
73
|
1 |
|
$this->postMiddlewares = $postMiddlewares; |
74
|
|
|
|
75
|
1 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param callable $middleware |
80
|
|
|
* |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
3 |
|
public function pre(callable $middleware) |
84
|
|
|
{ |
85
|
3 |
|
$this->getPreMiddlewares()->add($middleware); |
86
|
|
|
|
87
|
3 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param callable $middleware |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
3 |
|
public function post(callable $middleware) |
96
|
|
|
{ |
97
|
3 |
|
$this->getPostMiddlewares()->add($middleware); |
98
|
|
|
|
99
|
3 |
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param ServerRequestInterface $request |
104
|
|
|
* @param ResponseInterface $response |
105
|
|
|
* @param callable $requestHandler |
106
|
|
|
* @param callable $next |
107
|
|
|
* |
108
|
|
|
* @return ResponseInterface |
109
|
|
|
*/ |
110
|
2 |
|
public function __invoke( |
111
|
|
|
ServerRequestInterface $request, |
112
|
|
|
ResponseInterface $response, |
113
|
|
|
callable $requestHandler = null, |
114
|
|
|
callable $next = null |
115
|
|
|
) : ResponseInterface |
116
|
|
|
{ |
117
|
2 |
|
return $this->getPreMiddlewares()->__invoke( |
118
|
|
|
$request, |
119
|
|
|
$response, |
120
|
2 |
|
function (ServerRequestInterface $request, ResponseInterface $response) use ($requestHandler, $next) { |
121
|
2 |
|
if (null !== $requestHandler) { |
122
|
2 |
|
$response = $requestHandler($request, $response); |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
return $this->getPostMiddlewares()->__invoke($request, $response, $next); |
126
|
2 |
|
} |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|