-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathProfilePlugin.php
141 lines (118 loc) · 4.37 KB
/
ProfilePlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
namespace Http\HttplugBundle\Collector;
use Exception;
use Http\Client\Common\Plugin;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* The ProfilePlugin decorates any Plugin to fill Profile to keep representation of plugin input/output. Created profile
* is pushed in the current Stack.
*
* @author Fabien Bourigault <[email protected]>
*
* @internal
* @final
*/
class ProfilePlugin implements Plugin
{
use Plugin\VersionBridgePlugin;
/**
* @var Plugin
*/
private $plugin;
/**
* @var Collector
*/
private $collector;
/**
* @var Formatter
*/
private $formatter;
public function __construct(Plugin $plugin, Collector $collector, Formatter $formatter)
{
$this->plugin = $plugin;
$this->collector = $collector;
$this->formatter = $formatter;
}
protected function doHandleRequest(RequestInterface $request, callable $next, callable $first)
{
$profile = new Profile(get_class($this->plugin));
$stack = $this->collector->getActiveStack();
if (null === $stack) {
throw new \LogicException('No active stack');
}
$stack->addProfile($profile);
// wrap the next callback to profile the plugin request changes
$wrappedNext = function (RequestInterface $request) use ($next, $profile) {
$this->onOutgoingRequest($request, $profile);
return $next($request);
};
// wrap the first callback to profile the plugin request changes
$wrappedFirst = function (RequestInterface $request) use ($first, $profile) {
$this->onOutgoingRequest($request, $profile);
return $first($request);
};
try {
$promise = $this->plugin->handleRequest($request, $wrappedNext, $wrappedFirst);
} catch (Exception $e) {
$this->onException($request, $profile, $e, $stack);
throw $e;
}
return $promise->then(function (ResponseInterface $response) use ($profile, $request, $stack) {
$this->onOutgoingResponse($response, $profile, $request, $stack);
return $response;
}, function (Exception $exception) use ($profile, $request, $stack) {
$this->onException($request, $profile, $exception, $stack);
throw $exception;
});
}
private function onException(
RequestInterface $request,
Profile $profile,
Exception $exception,
Stack $stack
): void {
$profile->setFailed(true);
$profile->setResponse($this->formatter->formatException($exception));
$this->collectRequestInformation($request, $stack);
}
private function onOutgoingRequest(RequestInterface $request, Profile $profile): void
{
$profile->setRequest($this->formatter->formatRequest($request));
}
private function onOutgoingResponse(ResponseInterface $response, Profile $profile, RequestInterface $request, Stack $stack): void
{
$profile->setResponse($this->formatter->formatResponseForRequest($response, $request));
$this->collectRequestInformation($request, $stack);
}
/**
* Collect request information when not already done by the HTTP client. This happens when using the CachePlugin
* and the cache is hit without re-validation.
*/
private function collectRequestInformation(RequestInterface $request, Stack $stack): void
{
$uri = $request->getUri();
if (empty($stack->getRequestTarget())) {
$stack->setRequestTarget($request->getRequestTarget());
}
if (empty($stack->getRequestMethod())) {
$stack->setRequestMethod($request->getMethod());
}
if (empty($stack->getRequestScheme())) {
$stack->setRequestScheme($uri->getScheme());
}
if (null === $stack->getRequestPort()) {
$stack->setRequestPort($uri->getPort());
}
if (empty($stack->getRequestHost())) {
$stack->setRequestHost($uri->getHost());
}
if (empty($stack->getClientRequest())) {
$stack->setClientRequest($this->formatter->formatRequest($request));
}
if (empty($stack->getCurlCommand())) {
$stack->setCurlCommand($this->formatter->formatAsCurlCommand($request));
}
}
}