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 StackPlugin must be used as first Plugin in a client stack. It's used to detect when a new request start by |
12
|
|
|
* creating a new Stack and pushing it to the Collector. |
13
|
|
|
* |
14
|
|
|
* @author Fabien Bourigault <[email protected]> |
15
|
|
|
* |
16
|
|
|
* @internal |
17
|
|
|
*/ |
18
|
|
|
class StackPlugin implements Plugin |
19
|
|
|
{ |
20
|
|
|
use Plugin\VersionBridgePlugin; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Collector |
24
|
|
|
*/ |
25
|
|
|
private $collector; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $client; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Formatter |
34
|
|
|
*/ |
35
|
|
|
private $formatter; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Collector $collector |
39
|
|
|
* @param Formatter $formatter |
40
|
12 |
|
* @param string $client |
41
|
|
|
*/ |
42
|
12 |
|
public function __construct(Collector $collector, Formatter $formatter, $client) |
43
|
12 |
|
{ |
44
|
12 |
|
$this->collector = $collector; |
45
|
12 |
|
$this->formatter = $formatter; |
46
|
|
|
$this->client = $client; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
protected function doHandleRequest(RequestInterface $request, callable $next, callable $first) |
|
|
|
|
50
|
8 |
|
{ |
51
|
|
|
$stack = new Stack($this->client, $this->formatter->formatRequest($request)); |
52
|
8 |
|
|
53
|
|
|
$this->collector->addStack($stack); |
54
|
8 |
|
$this->collector->activateStack($stack); |
55
|
8 |
|
|
56
|
|
|
$onFulfilled = function (ResponseInterface $response) use ($stack) { |
57
|
8 |
|
$stack->setResponse($this->formatter->formatResponse($response)); |
58
|
4 |
|
|
59
|
|
|
return $response; |
60
|
4 |
|
}; |
61
|
8 |
|
|
62
|
|
|
$onRejected = function (Exception $exception) use ($stack) { |
63
|
8 |
|
$stack->setResponse($this->formatter->formatException($exception)); |
64
|
1 |
|
$stack->setFailed(true); |
65
|
1 |
|
|
66
|
|
|
throw $exception; |
67
|
1 |
|
}; |
68
|
8 |
|
|
69
|
|
|
try { |
70
|
|
|
return $next($request)->then($onFulfilled, $onRejected); |
71
|
8 |
|
} finally { |
72
|
|
|
$this->collector->deactivateStack($stack); |
73
|
8 |
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.