Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create HTTP Pool finally hook
  • Loading branch information
WendellAdriel committed Sep 5, 2025
commit d13816186956d63a823f3d29ab75346ebf791c63
5 changes: 5 additions & 0 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ public function pool(callable $callback)
$progressCallback = $pool->progressCallback();
$catchCallback = $pool->catchCallback();
$thenCallback = $pool->thenCallback();
$finallyCallback = $pool->finallyCallback();
$success = 0;

foreach ($requests as $key => $item) {
Expand All @@ -920,6 +921,10 @@ public function pool(callable $callback)
$thenCallback($results);
}

if ($finallyCallback !== null) {
$finallyCallback($results);
}

return $results;
}

Expand Down
28 changes: 28 additions & 0 deletions src/Illuminate/Http/Client/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class Pool
*/
protected $thenCallback = null;

/**
* The callback to run after all the requests from the pool finish.
*
* @var \Closure|null
*/
protected $finallyCallback = null;

/**
* Create a new requests pool.
*
Expand Down Expand Up @@ -168,4 +175,25 @@ public function thenCallback(): ?Closure
{
return $this->thenCallback;
}

/**
* Register a callback to run after all the requests from the pool finish.
*
* @param Closure $callback
* @return void
*/
public function finally(Closure $callback)
{
$this->finallyCallback = $callback;
}

/**
* Retrieve the finally callback in the pool.
*
* @return \Closure|null
*/
public function finallyCallback(): ?Closure
{
return $this->finallyCallback;
}
}
62 changes: 62 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3915,6 +3915,68 @@ public function testPoolThenHookIsNotCalled(): void
$this->assertCount(0, $thenCallback);
}

public function testPoolFinallyHookIsCalledWithoutErrors(): void
{
$this->factory->fake([
'https://200.com' => $this->factory::response('OK', 200),
'https://201.com' => $this->factory::response('Created', 201),
]);

$finallyCallback = [];

$responses = $this->factory->pool(function (Pool $pool) use (&$finallyCallback) {
$pool->finally(function (array $results) use (&$finallyCallback) {
$finallyCallback = $results;
});

return [
$pool->as('first')->get('https://200.com'),
$pool->as('second')->get('https://201.com'),
];
});

$this->assertSame(200, $responses['first']->status());
$this->assertSame(201, $responses['second']->status());

$this->assertCount(2, $finallyCallback);
$this->assertArrayHasKey('first', $finallyCallback);
$this->assertArrayHasKey('second', $finallyCallback);

$this->assertSame($responses['first'], $finallyCallback['first']);
$this->assertSame($responses['second'], $finallyCallback['second']);
}

public function testPoolFinallyHookIsCalledWithErrors(): void
{
$this->factory->fake([
'https://200.com' => $this->factory::response('OK', 200),
'https://500.com' => $this->factory::response('Error', 500),
]);

$finallyCallback = [];

$responses = $this->factory->pool(function (Pool $pool) use (&$finallyCallback) {
$pool->finally(function (array $results) use (&$finallyCallback) {
$finallyCallback = $results;
});

return [
$pool->as('first')->get('https://200.com'),
$pool->as('second')->get('https://500.com'),
];
});

$this->assertSame(200, $responses['first']->status());
$this->assertSame(500, $responses['second']->status());

$this->assertCount(2, $finallyCallback);
$this->assertArrayHasKey('first', $finallyCallback);
$this->assertArrayHasKey('second', $finallyCallback);

$this->assertSame($responses['first'], $finallyCallback['first']);
$this->assertSame($responses['second'], $finallyCallback['second']);
}

public static function methodsReceivingArrayableDataProvider()
{
return [
Expand Down