Skip to content

Commit a812232

Browse files
committed
Pass ID to curl.after_request hook for multiple requests
1 parent c494851 commit a812232

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

docs/hooks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Available Hooks
100100

101101
Alter the raw HTTP response before returning for parsing.
102102

103-
Parameters: `string &$headers`, `[array &$info]`
103+
Parameters: `string &$headers`, `[array &$info]`, `[int|string $id]`
104104

105105
The optional `$info` parameter contains the associated array as defined in
106106
the return value for [curl_getinfo()](https://www.php.net/curl-getinfo#refsect1-function.curl-getinfo-returnvalues).

src/Transport/Curl.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public function request_multiple($requests, $options) {
300300
$responses[$key] = $exception;
301301
$options['hooks']->dispatch('transport.internal.parse_error', [&$responses[$key], $requests[$key]]);
302302
} else {
303-
$responses[$key] = $subrequests[$key]->process_response($subrequests[$key]->response_data, $options);
303+
$responses[$key] = $subrequests[$key]->process_response($subrequests[$key]->response_data, $options, $key);
304304

305305
$options['hooks']->dispatch('transport.internal.parse_response', [&$responses[$key], $requests[$key]]);
306306
}
@@ -464,13 +464,14 @@ private function setup_handle($url, $headers, $data, $options) {
464464
*
465465
* @param string $response Response data from the body
466466
* @param array $options Request options
467+
* @param int|string|null $id ID of a multi-request
467468
* @return string|false HTTP response data including headers. False if non-blocking.
468469
* @throws \WpOrg\Requests\Exception If the request resulted in a cURL error.
469470
*/
470-
public function process_response($response, $options) {
471+
public function process_response($response, $options, $id = null) {
471472
if ($options['blocking'] === false) {
472473
$fake_headers = '';
473-
$options['hooks']->dispatch('curl.after_request', [&$fake_headers]);
474+
$options['hooks']->dispatch('curl.after_request', [&$fake_headers, null, $id]);
474475
return false;
475476
}
476477

@@ -492,7 +493,7 @@ public function process_response($response, $options) {
492493

493494
$this->info = curl_getinfo($this->handle);
494495

495-
$options['hooks']->dispatch('curl.after_request', [&$this->headers, &$this->info]);
496+
$options['hooks']->dispatch('curl.after_request', [&$this->headers, &$this->info, $id]);
496497
return $this->headers;
497498
}
498499

tests/Transport/CurlTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace WpOrg\Requests\Tests\Transport;
44

55
use WpOrg\Requests\Exception;
6+
use WpOrg\Requests\Hooks;
67
use WpOrg\Requests\Requests;
78
use WpOrg\Requests\Tests\Transport\BaseTestCase;
89
use WpOrg\Requests\Transport\Curl;
@@ -136,4 +137,38 @@ public function testSetsEmptyExpectHeaderIfBodySmallerThan1Mb() {
136137

137138
$this->assertFalse(isset($result['headers']['Expect']));
138139
}
140+
141+
public function testMultipleTriggersCurlAfterRequestHookWithId() {
142+
$requests = [
143+
'get' => [
144+
'url' => $this->httpbin('/get', true),
145+
],
146+
'post' => [
147+
'url' => $this->httpbin('/post', true),
148+
'type' => Requests::POST,
149+
'data' => 'test',
150+
],
151+
];
152+
153+
$mock = $this->getMockBuilder(stdClass::class)
154+
->setMethods(['after_request'])
155+
->getMock();
156+
157+
$mock->expects($this->atLeast(2))
158+
->method('after_request')
159+
->with(
160+
$this->isType('string'),
161+
$this->logicalAnd($this->isType('array'), $this->logicalNot($this->isEmpty())),
162+
$this->isType('string')
163+
);
164+
$hooks = new Hooks();
165+
$hooks->register('curl.after_request', [$mock, 'after_request']);
166+
167+
$options = [
168+
'hooks' => $hooks,
169+
];
170+
171+
Requests::request_multiple($requests, $this->getOptions($options));
172+
}
173+
139174
}

0 commit comments

Comments
 (0)