1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Softonic\Laravel\Middleware\Psr15Bridge; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Http\Response; |
7
|
|
|
use Nyholm\Psr7\Factory\Psr17Factory; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
11
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory; |
12
|
|
|
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
class Psr15MiddlewareAdapter |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var MiddlewareInterface |
19
|
|
|
*/ |
20
|
|
|
private $psr15Middleware; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var PsrHttpFactory |
24
|
|
|
*/ |
25
|
|
|
private $psrHttpFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var HttpFoundationFactory |
29
|
|
|
*/ |
30
|
|
|
private $httpFoundationFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var NextHandlerFactory |
34
|
|
|
*/ |
35
|
|
|
private $nextHandlerFactory; |
36
|
|
|
|
37
|
2 |
|
public function __construct( |
38
|
|
|
NextHandlerFactory $nextHandlerFactory, |
39
|
|
|
PsrHttpFactory $psrHttpFactory, |
40
|
|
|
HttpFoundationFactory $httpFoundationFactory, |
41
|
|
|
MiddlewareInterface $psr15Middleware |
42
|
|
|
) { |
43
|
2 |
|
$this->psr15Middleware = $psr15Middleware; |
44
|
2 |
|
$this->psrHttpFactory = $psrHttpFactory; |
45
|
2 |
|
$this->httpFoundationFactory = $httpFoundationFactory; |
46
|
2 |
|
$this->nextHandlerFactory = $nextHandlerFactory; |
47
|
2 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Builder to do the class developer friendly. |
51
|
|
|
* |
52
|
|
|
* @param MiddlewareInterface $psr15Middleware |
53
|
|
|
* |
54
|
|
|
* @return Psr15MiddlewareAdapter |
55
|
|
|
*/ |
56
|
|
|
public static function adapt(MiddlewareInterface $psr15Middleware) |
57
|
|
|
{ |
58
|
|
|
$psr17Factory = new Psr17Factory(); |
59
|
|
|
|
60
|
|
|
return new self( |
61
|
|
|
new NextHandlerFactory(), |
62
|
|
|
new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory), |
63
|
|
|
new HttpFoundationFactory(), |
64
|
|
|
$psr15Middleware |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Handle an incoming request. |
70
|
|
|
* |
71
|
|
|
* Transform current FoundationRequest to PSR-7 to allow the PSR-15 to process it and wait for their response |
72
|
|
|
* that will be adapted from PSR-7 to HttpFoundation to allow previous middleware to process it. |
73
|
|
|
* |
74
|
|
|
* @param Request $foundationRequest |
75
|
|
|
* @param \Closure $next |
76
|
|
|
* |
77
|
|
|
* @return Response |
78
|
|
|
*/ |
79
|
2 |
|
public function handle(Request $foundationRequest, Closure $next): Response |
80
|
|
|
{ |
81
|
2 |
|
$psr7Request = $this->getPsr7Request($foundationRequest); |
82
|
2 |
|
$next = $this->getNextExecutionHandlerAdapter($foundationRequest, $next); |
83
|
|
|
|
84
|
2 |
|
$response = $this->psr15Middleware->process($psr7Request, $next); |
85
|
|
|
|
86
|
2 |
|
return $this->getResponse($response); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Hook the next execution handler to intercept it. |
91
|
|
|
* |
92
|
|
|
* The handler adapt the request and response to the needed objects |
93
|
|
|
* to allow PST-15 and Laravel middleware executions. |
94
|
|
|
* |
95
|
|
|
* @param Request $request |
96
|
|
|
* @param Closure $next |
97
|
|
|
* |
98
|
|
|
* @return NextHandlerAdapter |
99
|
|
|
*/ |
100
|
2 |
|
public function getNextExecutionHandlerAdapter(Request $request, Closure $next) |
101
|
|
|
{ |
102
|
2 |
|
return $this->nextHandlerFactory->getHandler( |
103
|
2 |
|
$this->httpFoundationFactory, |
104
|
2 |
|
$this->psrHttpFactory, |
105
|
1 |
|
$request, |
106
|
1 |
|
$next |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Transform an HttpFoundation request to a PSR-7 request. |
112
|
|
|
* |
113
|
|
|
* @param Request $request |
114
|
|
|
* |
115
|
|
|
* @return ServerRequestInterface |
116
|
|
|
*/ |
117
|
2 |
|
protected function getPsr7Request(Request $request) |
118
|
|
|
{ |
119
|
2 |
|
return $this->psrHttpFactory->createRequest($request); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Transform a PSR-7 response to a HttpFoundation response. |
124
|
|
|
* |
125
|
|
|
* @param ResponseInterface $psr7Response |
126
|
|
|
* |
127
|
|
|
* @return Response |
128
|
|
|
*/ |
129
|
2 |
|
protected function getResponse(ResponseInterface $psr7Response): Response |
130
|
|
|
{ |
131
|
2 |
|
$response = new \Illuminate\Http\Response(); |
132
|
2 |
|
$foundationResponse = $this->httpFoundationFactory->createResponse($psr7Response); |
133
|
|
|
|
134
|
2 |
|
foreach ($foundationResponse->headers as $key => $value) { |
135
|
2 |
|
$response->headers->set($key, $value); |
136
|
|
|
} |
137
|
|
|
|
138
|
2 |
|
$response->setContent($foundationResponse->getContent()); |
139
|
2 |
|
$response->setProtocolVersion($foundationResponse->getProtocolVersion()); |
140
|
2 |
|
$response->setStatusCode($foundationResponse->getStatusCode()); |
141
|
2 |
|
$response->setCharset($foundationResponse->getCharset() ?? ''); |
142
|
|
|
|
143
|
2 |
|
foreach ($foundationResponse->headers->getCookies() as $cookie) { |
144
|
|
|
$response->withCookie($cookie); |
145
|
|
|
} |
146
|
|
|
|
147
|
2 |
|
return $response; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|