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