97 lines
2.1 KiB
PHP
97 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Illuminate\Http\Client;
|
|
|
|
use GuzzleHttp\Utils;
|
|
|
|
/**
|
|
* @mixin \Illuminate\Http\Client\Factory
|
|
*/
|
|
class Pool
|
|
{
|
|
/**
|
|
* The factory instance.
|
|
*
|
|
* @var \Illuminate\Http\Client\Factory
|
|
*/
|
|
protected $factory;
|
|
|
|
/**
|
|
* The handler function for the Guzzle client.
|
|
*
|
|
* @var callable
|
|
*/
|
|
protected $handler;
|
|
|
|
/**
|
|
* The pool of requests.
|
|
*
|
|
* @var array<array-key, \Illuminate\Http\Client\PendingRequest>
|
|
*/
|
|
protected $pool = [];
|
|
|
|
/**
|
|
* Create a new requests pool.
|
|
*
|
|
* @param \Illuminate\Http\Client\Factory|null $factory
|
|
*/
|
|
public function __construct(?Factory $factory = null)
|
|
{
|
|
$this->factory = $factory ?: new Factory();
|
|
$this->handler = Utils::chooseHandler();
|
|
}
|
|
|
|
/**
|
|
* Add a request to the pool with a numeric index.
|
|
*
|
|
* @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise
|
|
*/
|
|
public function newRequest()
|
|
{
|
|
return $this->pool[] = $this->asyncRequest();
|
|
}
|
|
|
|
/**
|
|
* Add a request to the pool with a key.
|
|
*
|
|
* @param string $key
|
|
* @return \Illuminate\Http\Client\PendingRequest
|
|
*/
|
|
public function as(string $key)
|
|
{
|
|
return $this->pool[$key] = $this->asyncRequest();
|
|
}
|
|
|
|
/**
|
|
* Retrieve a new async pending request.
|
|
*
|
|
* @return \Illuminate\Http\Client\PendingRequest
|
|
*/
|
|
protected function asyncRequest()
|
|
{
|
|
return $this->factory->setHandler($this->handler)->async();
|
|
}
|
|
|
|
/**
|
|
* Retrieve the requests in the pool.
|
|
*
|
|
* @return array<array-key, \Illuminate\Http\Client\PendingRequest>
|
|
*/
|
|
public function getRequests()
|
|
{
|
|
return $this->pool;
|
|
}
|
|
|
|
/**
|
|
* Add a request to the pool with a numeric index and forward the method call to the request.
|
|
*
|
|
* @param string $method
|
|
* @param array $parameters
|
|
* @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise
|
|
*/
|
|
public function __call($method, $parameters)
|
|
{
|
|
return $this->newRequest()->{$method}(...$parameters);
|
|
}
|
|
}
|