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 then hook
  • Loading branch information
WendellAdriel committed Sep 5, 2025
commit 31a229f3d9e5f8668fc740dd7c42e85649222e17
5 changes: 5 additions & 0 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,7 @@ public function pool(callable $callback)
$requests = $pool->getRequests();
$progressCallback = $pool->progressCallback();
$catchCallback = $pool->catchCallback();
$thenCallback = $pool->thenCallback();
$success = 0;

foreach ($requests as $key => $item) {
Expand All @@ -915,6 +916,10 @@ public function pool(callable $callback)
}
}

if ($success === count($requests) && $thenCallback !== null) {
$thenCallback($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 @@ -45,6 +45,13 @@ class Pool
*/
protected $catchCallback = null;

/**
* The callback to run if all the requests from the pool succeeded.
*
* @var \Closure|null
*/
protected $thenCallback = null;

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

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

/**
* Retrieve the then callback in the pool.
*
* @return \Closure|null
*/
public function thenCallback(): ?Closure
{
return $this->thenCallback;
}
}
57 changes: 57 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3858,6 +3858,63 @@ public function testPoolCatchHook(): void
$this->assertSame($responses['third'], $catchCallbacks['third']);
}

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

$thenCallback = [];

$responses = $this->factory->pool(function (Pool $pool) use (&$thenCallback) {
$pool->then(function (array $results) use (&$thenCallback) {
$thenCallback = $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, $thenCallback);
$this->assertArrayHasKey('first', $thenCallback);
$this->assertArrayHasKey('second', $thenCallback);

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

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

$thenCallback = [];

$responses = $this->factory->pool(function (Pool $pool) use (&$thenCallback) {
$pool->then(function (array $results) use (&$thenCallback) {
$thenCallback = $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(0, $thenCallback);
}

public static function methodsReceivingArrayableDataProvider()
{
return [
Expand Down