1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Arki\RequestId library. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandru Furculita <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE.md. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Arki\RequestId\Integrations\PSR7; |
13
|
|
|
|
14
|
|
|
use Arki\RequestId\Integrations\PSR7\Decorators\AddsRequestIdToHttpMessage; |
15
|
|
|
use Arki\RequestId\Integrations\PSR7\Decorators\HttpMessageDecorator; |
16
|
|
|
use Arki\RequestId\Providers\RequestAware; |
17
|
|
|
use Arki\RequestId\Providers\RequestIdProviderFactory; |
18
|
|
|
use Psr\Http\Message\MessageInterface; |
19
|
|
|
use Psr\Http\Message\RequestInterface; |
20
|
|
|
use Psr\Http\Message\ResponseInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* PSR7 Middleware that adds an id to requests and responses. |
24
|
|
|
*/ |
25
|
|
|
final class AddRequestId |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var HttpMessageDecorator |
29
|
|
|
*/ |
30
|
|
|
private $requestDecorator; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var HttpMessageDecorator |
34
|
|
|
*/ |
35
|
|
|
private $responseDecorator; |
36
|
|
|
/** |
37
|
|
|
* @var RequestIdProviderFactory |
38
|
|
|
*/ |
39
|
|
|
private $providerFactory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param RequestIdProviderFactory $providerFactory |
43
|
|
|
* @param HttpMessageDecorator $requestDecorator |
44
|
|
|
* @param HttpMessageDecorator $responseDecorator |
45
|
|
|
*/ |
46
|
|
|
public function __construct( |
47
|
|
|
RequestIdProviderFactory $providerFactory, |
48
|
|
|
HttpMessageDecorator $requestDecorator = null, |
49
|
|
|
HttpMessageDecorator $responseDecorator = null |
50
|
|
|
) { |
51
|
|
|
$this->requestDecorator = $requestDecorator; |
52
|
|
|
$this->responseDecorator = $responseDecorator; |
53
|
|
|
$this->providerFactory = $providerFactory; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param RequestInterface $request |
58
|
|
|
* @param ResponseInterface $response |
59
|
|
|
* @param callable $next |
60
|
|
|
* |
61
|
|
|
* @return ResponseInterface|MessageInterface |
62
|
|
|
* |
63
|
|
|
* @throws \InvalidArgumentException for invalid header values or names |
64
|
|
|
*/ |
65
|
|
|
public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) |
66
|
|
|
{ |
67
|
|
|
if ($this->providerFactory instanceof RequestAware) { |
68
|
|
|
$this->providerFactory->setRequest($request); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$provider = $this->providerFactory->create(); |
72
|
|
|
|
73
|
|
|
if (!$this->requestDecorator) { |
74
|
|
|
$this->requestDecorator = new AddsRequestIdToHttpMessage($provider); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if (!$this->responseDecorator) { |
78
|
|
|
$this->responseDecorator = new AddsRequestIdToHttpMessage($provider); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->responseDecorator->decorate($next($this->requestDecorator->decorate($request), $response)); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|