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