master

laravel/framework

Last updated at: 29/12/2023 09:23

Pool.php

TLDR

The Pool.php file in the Illuminate\Http\Client namespace contains the Pool class. This class is used to create a pool of requests. It provides methods for adding requests to the pool, retrieving the requests, and executing the requests asynchronously.

Methods

__construct

This method is the constructor of the Pool class. It initializes the factory property with an instance of the Factory class if one is not provided. It also sets the handler property with a Guzzle client handler function.

as

This method adds a request to the pool with a specified key. It returns an instance of the PendingRequest class.

asyncRequest

This method retrieves a new asynchronous pending request. It returns an instance of the PendingRequest class.

getRequests

This method retrieves the requests in the pool. It returns an array of requests.

__call

This method adds a request to the pool with a numeric index. It is a magic method that forwards the method call to the asyncRequest method. It returns an instance of the PendingRequest class or a Promise object.

Classes

None

<?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
     */
    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->handler = Utils::chooseHandler();
    }

    /**
     * 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
     */
    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|\GuzzleHttp\Promise\Promise
     */
    public function __call($method, $parameters)
    {
        return $this->pool[] = $this->asyncRequest()->$method(...$parameters);
    }
}