1 | <?php |
||
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( |
|
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): Psr15MiddlewareAdapter |
||
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 |
|
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 PSR-15 and Laravel middleware executions. |
||
94 | * |
||
95 | * @param Request $request |
||
96 | * @param Closure $next |
||
97 | * |
||
98 | * @return NextHandlerAdapter |
||
99 | */ |
||
100 | 2 | private function getNextExecutionHandlerAdapter(Request $request, Closure $next): NextHandlerAdapter |
|
101 | { |
||
102 | 2 | return $this->nextHandlerFactory->getHandler( |
|
103 | 2 | $this->httpFoundationFactory, |
|
104 | 2 | $this->psrHttpFactory, |
|
105 | $request, |
||
106 | $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): ServerRequestInterface |
|
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 |
|
149 | } |
||
150 |