1 | <?php |
||
18 | class ProfilePlugin implements Plugin |
||
19 | { |
||
20 | /** |
||
21 | * @var Plugin |
||
22 | */ |
||
23 | private $plugin; |
||
24 | |||
25 | /** |
||
26 | * @var Collector |
||
27 | */ |
||
28 | private $collector; |
||
29 | |||
30 | /** |
||
31 | * @var Formatter |
||
32 | */ |
||
33 | private $formatter; |
||
34 | |||
35 | /** |
||
36 | * @param Plugin $plugin |
||
37 | * @param Collector $collector |
||
38 | * @param Formatter $formatter |
||
39 | */ |
||
40 | 12 | public function __construct(Plugin $plugin, Collector $collector, Formatter $formatter) |
|
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | 8 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
51 | { |
||
52 | 8 | $profile = new Profile(get_class($this->plugin)); |
|
53 | |||
54 | 8 | $stack = $this->collector->getActiveStack(); |
|
55 | 8 | $stack->addProfile($profile); |
|
56 | |||
57 | // wrap the next callback to profile the plugin request changes |
||
58 | 8 | $wrappedNext = function (RequestInterface $request) use ($next, $profile) { |
|
59 | 7 | $this->onOutgoingRequest($request, $profile); |
|
60 | |||
61 | 7 | return $next($request); |
|
62 | 8 | }; |
|
63 | |||
64 | // wrap the first callback to profile the plugin request changes |
||
65 | 8 | $wrappedFirst = function (RequestInterface $request) use ($first, $profile) { |
|
66 | $this->onOutgoingRequest($request, $profile); |
||
67 | |||
68 | return $first($request); |
||
69 | 8 | }; |
|
70 | |||
71 | try { |
||
72 | 8 | $promise = $this->plugin->handleRequest($request, $wrappedNext, $wrappedFirst); |
|
73 | 1 | } catch (Exception $e) { |
|
74 | 1 | $this->onException($request, $profile, $e, $stack); |
|
75 | |||
76 | 1 | throw $e; |
|
77 | } |
||
78 | |||
79 | 6 | return $promise->then(function (ResponseInterface $response) use ($profile, $request, $stack) { |
|
80 | 6 | $this->onOutgoingResponse($response, $profile, $request, $stack); |
|
81 | |||
82 | 6 | return $response; |
|
83 | }, function (Exception $exception) use ($profile, $request, $stack) { |
||
84 | 1 | $this->onException($request, $profile, $exception, $stack); |
|
85 | |||
86 | 1 | throw $exception; |
|
87 | 7 | }); |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param RequestInterface $request |
||
92 | * @param Profile $profile |
||
93 | * @param Exception $exception |
||
94 | * @param Stack $stack |
||
95 | */ |
||
96 | 2 | private function onException( |
|
106 | |||
107 | /** |
||
108 | * @param RequestInterface $request |
||
109 | * @param Profile $profile |
||
110 | */ |
||
111 | 7 | private function onOutgoingRequest(RequestInterface $request, Profile $profile) |
|
115 | |||
116 | /** |
||
117 | * @param ResponseInterface $response |
||
118 | * @param Profile $profile |
||
119 | * @param RequestInterface $request |
||
120 | * @param Stack $stack |
||
121 | */ |
||
122 | 6 | private function onOutgoingResponse(ResponseInterface $response, Profile $profile, RequestInterface $request, Stack $stack = null) |
|
127 | |||
128 | /** |
||
129 | * Collect request information when not already done by the HTTP client. This happens when using the CachePlugin |
||
130 | * and the cache is hit without re-validation. |
||
131 | * |
||
132 | * @param RequestInterface $request |
||
133 | * @param Stack|null $stack |
||
134 | */ |
||
135 | 8 | private function collectRequestInformation(RequestInterface $request, Stack $stack = null) |
|
156 | } |
||
157 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: