|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Http\Client\Plugin; |
|
4
|
|
|
|
|
5
|
1 |
|
@trigger_error('The '.__NAMESPACE__.'\StopwatchPlugin class is deprecated since version 1.1 and will be removed in 2.0. Use Http\Client\Common\Plugin\StopwatchPlugin instead.', E_USER_DEPRECATED); |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Http\Client\Exception; |
|
8
|
|
|
use Psr\Http\Message\RequestInterface; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Measure the duration of a request call with the stopwatch component. |
|
14
|
|
|
* |
|
15
|
|
|
* @author Joel Wurtz <[email protected]> |
|
16
|
|
|
* |
|
17
|
|
|
* @deprecated since since version 1.1, and will be removed in 2.0. Use {@link \Http\Client\Common\Plugin\StopwatchPlugin} instead. |
|
18
|
|
|
*/ |
|
19
|
|
|
class StopwatchPlugin implements Plugin |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
const CATEGORY = 'php_http.request'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Stopwatch |
|
25
|
|
|
*/ |
|
26
|
|
|
private $stopwatch; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param Stopwatch $stopwatch |
|
30
|
|
|
*/ |
|
31
|
4 |
|
public function __construct(Stopwatch $stopwatch) |
|
32
|
|
|
{ |
|
33
|
4 |
|
$this->stopwatch = $stopwatch; |
|
34
|
4 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
40
|
|
|
{ |
|
41
|
2 |
|
$eventName = $this->getStopwatchEventName($request); |
|
42
|
2 |
|
$this->stopwatch->start($eventName, self::CATEGORY); |
|
43
|
|
|
|
|
44
|
|
|
return $next($request)->then(function (ResponseInterface $response) use ($eventName) { |
|
45
|
1 |
|
$this->stopwatch->stop($eventName); |
|
46
|
|
|
|
|
47
|
1 |
|
return $response; |
|
48
|
2 |
|
}, function (Exception $exception) use ($eventName) { |
|
49
|
1 |
|
$this->stopwatch->stop($eventName); |
|
50
|
|
|
|
|
51
|
1 |
|
throw $exception; |
|
52
|
2 |
|
}); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Generates the event name. |
|
57
|
|
|
* |
|
58
|
|
|
* @param RequestInterface $request |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
2 |
|
private function getStopwatchEventName(RequestInterface $request) |
|
63
|
|
|
{ |
|
64
|
2 |
|
return sprintf('%s %s', $request->getMethod(), $request->getRequestTarget()); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: