1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Http\HttplugBundle\Collector; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Http\Client\Common\Plugin; |
7
|
|
|
use Http\Client\Exception; |
8
|
|
|
use Psr\Http\Message\RequestInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
|
11
|
|
|
class DebugPluginDecorator implements Plugin |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Plugin |
15
|
|
|
*/ |
16
|
|
|
private $plugin; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Collector |
20
|
|
|
*/ |
21
|
|
|
private $collector; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Plugin $plugin |
25
|
|
|
* @param Collector $collector |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Plugin $plugin, Collector $collector) |
28
|
|
|
{ |
29
|
|
|
$this->plugin = $plugin; |
30
|
|
|
$this->collector = $collector; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
|
|
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
37
|
|
|
{ |
38
|
|
|
$profile = new Profile(); |
39
|
|
|
$profile->setPlugin($this->plugin); |
40
|
|
|
$profile->setRequest($request); |
41
|
|
|
|
42
|
|
|
$this->collector->getCurrentStack()->addProfile($profile); |
43
|
|
|
|
44
|
|
|
$this->plugin->handleRequest($request, $this->wrap($next, $profile), $this->wrap($first, $profile)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param callable $callable |
49
|
|
|
* @param Profile $profile |
50
|
|
|
* |
51
|
|
|
* @return Closure |
52
|
|
|
*/ |
53
|
|
|
private function wrap(callable $callable, Profile $profile) |
54
|
|
|
{ |
55
|
|
|
return function (RequestInterface $request) use ($callable, $profile) { |
56
|
|
|
return $callable($request)->then(function (ResponseInterface $response) use ($profile) { |
57
|
|
|
$profile->setResponse($response); |
58
|
|
|
|
59
|
|
|
return $response; |
60
|
|
|
}, function (Exception $exception) use ($profile) { |
61
|
|
|
$profile->setException($exception); |
62
|
|
|
throw $exception; |
63
|
|
|
}); |
64
|
|
|
}; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|