|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Ivory Http Adapter package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ivory\HttpAdapter\Event\Subscriber; |
|
13
|
|
|
|
|
14
|
|
|
use Ivory\HttpAdapter\Event\Events; |
|
15
|
|
|
use Ivory\HttpAdapter\Event\MultiRequestCreatedEvent; |
|
16
|
|
|
use Ivory\HttpAdapter\Event\MultiRequestErroredEvent; |
|
17
|
|
|
use Ivory\HttpAdapter\Event\MultiRequestSentEvent; |
|
18
|
|
|
use Ivory\HttpAdapter\Event\RequestCreatedEvent; |
|
19
|
|
|
use Ivory\HttpAdapter\Event\RequestErroredEvent; |
|
20
|
|
|
use Ivory\HttpAdapter\Event\RequestSentEvent; |
|
21
|
|
|
use Ivory\HttpAdapter\HttpAdapterInterface; |
|
22
|
|
|
use Ivory\HttpAdapter\Message\InternalRequestInterface; |
|
23
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
24
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @author GeLo <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class StopwatchSubscriber implements EventSubscriberInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var Stopwatch |
|
33
|
|
|
*/ |
|
34
|
|
|
private $stopwatch; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param Stopwatch $stopwatch |
|
38
|
|
|
*/ |
|
39
|
72 |
|
public function __construct(Stopwatch $stopwatch) |
|
40
|
|
|
{ |
|
41
|
72 |
|
$this->stopwatch = $stopwatch; |
|
42
|
72 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return Stopwatch |
|
46
|
|
|
*/ |
|
47
|
9 |
|
public function getStopwatch() |
|
48
|
|
|
{ |
|
49
|
9 |
|
return $this->stopwatch; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param RequestCreatedEvent $event |
|
54
|
|
|
*/ |
|
55
|
9 |
|
public function onRequestCreated(RequestCreatedEvent $event) |
|
56
|
|
|
{ |
|
57
|
9 |
|
$this->stopwatch->start($this->getStopwatchName($event->getHttpAdapter(), $event->getRequest())); |
|
58
|
9 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param RequestSentEvent $event |
|
62
|
|
|
*/ |
|
63
|
9 |
|
public function onRequestSent(RequestSentEvent $event) |
|
64
|
|
|
{ |
|
65
|
9 |
|
if (!$event->hasException()) { |
|
66
|
9 |
|
$this->stopwatch->stop($this->getStopwatchName($event->getHttpAdapter(), $event->getRequest())); |
|
67
|
7 |
|
} |
|
68
|
9 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param RequestErroredEvent $event |
|
72
|
|
|
*/ |
|
73
|
9 |
|
public function onRequestErrored(RequestErroredEvent $event) |
|
74
|
|
|
{ |
|
75
|
9 |
|
$this->stopwatch->stop($this->getStopwatchName($event->getHttpAdapter(), $event->getException()->getRequest())); |
|
|
|
|
|
|
76
|
9 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param MultiRequestCreatedEvent $event |
|
80
|
|
|
*/ |
|
81
|
9 |
|
public function onMultiRequestCreated(MultiRequestCreatedEvent $event) |
|
82
|
|
|
{ |
|
83
|
9 |
|
foreach ($event->getRequests() as $request) { |
|
84
|
9 |
|
$this->stopwatch->start($this->getStopwatchName($event->getHttpAdapter(), $request)); |
|
85
|
7 |
|
} |
|
86
|
9 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param MultiRequestSentEvent $event |
|
90
|
|
|
*/ |
|
91
|
9 |
|
public function onMultiRequestSent(MultiRequestSentEvent $event) |
|
92
|
|
|
{ |
|
93
|
9 |
|
foreach ($event->getResponses() as $response) { |
|
94
|
9 |
|
$this->stopwatch->stop( |
|
95
|
9 |
|
$this->getStopwatchName($event->getHttpAdapter(), $response->getParameter('request')) |
|
96
|
7 |
|
); |
|
97
|
7 |
|
} |
|
98
|
9 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @param MultiRequestErroredEvent $event |
|
102
|
|
|
*/ |
|
103
|
9 |
|
public function onMultiResponseErrored(MultiRequestErroredEvent $event) |
|
104
|
|
|
{ |
|
105
|
9 |
|
foreach ($event->getResponses() as $response) { |
|
106
|
9 |
|
$this->stopwatch->stop( |
|
107
|
9 |
|
$this->getStopwatchName($event->getHttpAdapter(), $response->getParameter('request')) |
|
108
|
7 |
|
); |
|
109
|
7 |
|
} |
|
110
|
|
|
|
|
111
|
9 |
|
foreach ($event->getExceptions() as $exception) { |
|
112
|
9 |
|
$this->stopwatch->stop( |
|
113
|
9 |
|
$this->getStopwatchName($event->getHttpAdapter(), $exception->getRequest()) |
|
|
|
|
|
|
114
|
7 |
|
); |
|
115
|
7 |
|
} |
|
116
|
9 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritdoc} |
|
120
|
|
|
*/ |
|
121
|
9 |
View Code Duplication |
public static function getSubscribedEvents() |
|
|
|
|
|
|
122
|
|
|
{ |
|
123
|
|
|
return [ |
|
124
|
9 |
|
Events::REQUEST_CREATED => ['onRequestCreated', 10000], |
|
125
|
7 |
|
Events::REQUEST_SENT => ['onRequestSent', -10000], |
|
126
|
7 |
|
Events::REQUEST_ERRORED => ['onRequestErrored', -10000], |
|
127
|
7 |
|
Events::MULTI_REQUEST_CREATED => ['onMultiRequestCreated', 10000], |
|
128
|
7 |
|
Events::MULTI_REQUEST_SENT => ['onMultiRequestSent', -10000], |
|
129
|
7 |
|
Events::MULTI_REQUEST_ERRORED => ['onMultiResponseErrored', -10000], |
|
130
|
7 |
|
]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param HttpAdapterInterface $httpAdapter |
|
135
|
|
|
* @param InternalRequestInterface $internalRequest |
|
136
|
|
|
* |
|
137
|
|
|
* @return string |
|
138
|
|
|
*/ |
|
139
|
54 |
|
private function getStopwatchName(HttpAdapterInterface $httpAdapter, InternalRequestInterface $internalRequest) |
|
140
|
|
|
{ |
|
141
|
54 |
|
return sprintf('ivory.http_adapter.%s (%s)', $httpAdapter->getName(), (string) $internalRequest->getUri()); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: