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