-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/http-pool' into 8.x
- Loading branch information
Showing
5 changed files
with
302 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Illuminate\Http\Client; | ||
|
||
class Pool | ||
{ | ||
/** | ||
* The factory instance. | ||
* | ||
* @var \Illuminate\Http\Client\Factory | ||
*/ | ||
protected $factory; | ||
|
||
/** | ||
* The client instance. | ||
* | ||
* @var \GuzzleHttp\Client | ||
*/ | ||
protected $client; | ||
|
||
/** | ||
* The pool of requests. | ||
* | ||
* @var array | ||
*/ | ||
protected $pool = []; | ||
|
||
/** | ||
* Create a new requests pool. | ||
* | ||
* @param \Illuminate\Http\Client\Factory|null $factory | ||
* @return void | ||
*/ | ||
public function __construct(Factory $factory = null) | ||
{ | ||
$this->factory = $factory ?: new Factory(); | ||
|
||
$this->client = $this->factory->buildClient(); | ||
} | ||
|
||
/** | ||
* 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->setClient($this->client)->async(); | ||
} | ||
|
||
/** | ||
* Retrieve the requests in the pool. | ||
* | ||
* @return array | ||
*/ | ||
public function getRequests() | ||
{ | ||
return $this->pool; | ||
} | ||
|
||
/** | ||
* Add a request to the pool with a numeric index. | ||
* | ||
* @param string $method | ||
* @param array $parameters | ||
* @return \Illuminate\Http\Client\PendingRequest | ||
*/ | ||
public function __call($method, $parameters) | ||
{ | ||
return $this->pool[] = $this->asyncRequest()->$method(...$parameters); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.