Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
105 changes: 55 additions & 50 deletions src/Illuminate/Http/Client/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@
*/
protected $factory;

/**
* The handler function for the Guzzle client.
*
* @var callable
*/
protected $handler;

/**
* The list of requests.
*
Expand Down Expand Up @@ -57,25 +50,11 @@
public $failedRequests = 0;

/**
* The date indicating when the batch was created.
*
* @var \Carbon\CarbonImmutable
*/
public $createdAt = null;

/**
* The date indicating when the batch was cancelled.
*
* @var \Carbon\CarbonImmutable|null
*/
public $cancelledAt = null;

/**
* The date indicating when the batch was finished.
* The handler function for the Guzzle client.
*
* @var \Carbon\CarbonImmutable|null
* @var callable
*/
public $finishedAt = null;
protected $handler;

/**
* The callback to run before the first request from the batch runs.
Expand Down Expand Up @@ -105,18 +84,44 @@
*/
protected $thenCallback = null;

/**
* The date when the batch was created.
*
* @var \Carbon\CarbonImmutable
*/
public $createdAt = null;

/**
* The date when the batch was cancelled.
*
* @var \Carbon\CarbonImmutable|null
*/
public $cancelledAt = null;

/**
* The date when the batch was finished.
*
* @var \Carbon\CarbonImmutable|null
*/
public $finishedAt = null;

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

/**
* Create a new request batch instance.
*
* @return void
*/
public function __construct(?Factory $factory = null)
{
$this->factory = $factory ?: new Factory();
$this->factory = $factory ?: new Factory;
$this->handler = Utils::chooseHandler();
$this->createdAt = new CarbonImmutable();
$this->createdAt = new CarbonImmutable;
}

/**
Expand All @@ -132,30 +137,6 @@
return $this->requests[$key] = $this->asyncRequest();
}

/**
* Retrieve the requests in the batch.
*
* @return array<array-key, \Illuminate\Http\Client\PendingRequest>
*/
public function getRequests(): array
{
return $this->requests;
}

/**
* Add a request to the batch with a numeric index.
*
* @param string $method
* @param array $parameters
* @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise
*/
public function __call(string $method, array $parameters)
{
$this->incrementPendingRequests();

return $this->requests[] = $this->asyncRequest()->$method(...$parameters);
}

/**
* Get the total number of requests that have been processed by the batch thus far.
*
Expand Down Expand Up @@ -387,7 +368,7 @@

return $result;
},
'rejected' => function ($reason, $key) use (&$results, $catchCallback) {

Check failure on line 371 in src/Illuminate/Http/Client/Batch.php

View workflow job for this annotation

GitHub Actions / Source Code

Anonymous function has an unused use $results.
$this->decrementPendingRequests();

if ($reason instanceof RequestException) {
Expand Down Expand Up @@ -458,4 +439,28 @@
{
$this->failedRequests++;
}

/**
* Get the requests in the batch.
*
* @return array<array-key, \Illuminate\Http\Client\PendingRequest>
*/
public function getRequests(): array
{
return $this->requests;
}

/**
* Add a request to the batch with a numeric index.
*
* @param string $method
* @param array $parameters
* @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise
*/
public function __call(string $method, array $parameters)
{
$this->incrementPendingRequests();

return $this->requests[] = $this->asyncRequest()->$method(...$parameters);
}
}
6 changes: 6 additions & 0 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,12 @@ public function pool(callable $callback)
return $results;
}

/**
* Send a pool of asynchronous requests concurrently, with callbacks for introspection.
*
* @param callable $callback
* @return \Illuminate\Http\Client\Batch
*/
public function batch(callable $callback): Batch
{
return tap(new Batch($this->factory), $callback);
Expand Down
Loading