class HandlerStackConfigurator

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Http/HandlerStackConfigurator.php \Drupal\Core\Http\HandlerStackConfigurator
  2. 8.9.x core/lib/Drupal/Core/Http/HandlerStackConfigurator.php \Drupal\Core\Http\HandlerStackConfigurator
  3. 10 core/lib/Drupal/Core/Http/HandlerStackConfigurator.php \Drupal\Core\Http\HandlerStackConfigurator

Defines a class for configuring middlewares on the http handler stack.

The http_client service requires a handler stack to perform http requests. This is provided by the http_handler_stack service. Modules wishing to add additional middlewares to the handler stack can create services and tag them as http_client_middleware. Each service must contain an __invoke method that returns a closure which will serve as the middleware.

Hierarchy

Expanded class hierarchy of HandlerStackConfigurator

See also

https://guzzle.readthedocs.org/en/latest/handlers-and-middleware.html

\Drupal\Core\Http\Client

\Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware

File

core/lib/Drupal/Core/Http/HandlerStackConfigurator.php, line 22

Namespace

Drupal\Core\Http
View source
class HandlerStackConfigurator {
    
    /**
     * Array of middlewares to add to the handler stack.
     *
     * @var callable[]
     */
    protected $middlewares = NULL;
    
    /**
     * A list of used middleware service IDs.
     *
     * @var string[]
     */
    protected $middlewareIds = [];
    
    /**
     * The service container.
     *
     * @var \Symfony\Component\DependencyInjection\ContainerInterface
     */
    protected $container;
    
    /**
     * Constructs a new HandlerStackConfigurator object.
     *
     * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
     *   The service container.
     * @param string[] $middleware_ids
     *   The middleware IDs.
     */
    public function __construct(ContainerInterface $container, array $middleware_ids) {
        $this->middlewareIds = $middleware_ids;
        $this->container = $container;
    }
    
    /**
     * Ensures that the middlewares are initialized.
     */
    protected function initializeMiddlewares() {
        if (!isset($this->middlewares)) {
            $this->middlewares = [];
            foreach ($this->middlewareIds as $middleware_id) {
                $middleware = $this->container
                    ->get($middleware_id);
                if (is_callable($middleware)) {
                    $this->middlewares[$middleware_id] = $middleware();
                }
                else {
                    throw new \InvalidArgumentException('Middlewares need to implement __invoke, see https://guzzle.readthedocs.org/en/latest/handlers-and-middleware.html for more information about middlewares.');
                }
            }
        }
    }
    
    /**
     * Configures the stack using services tagged as http_client_middleware.
     *
     * @param \GuzzleHttp\HandlerStack $handler_stack
     *   The handler stack
     */
    public function configure(HandlerStack $handler_stack) {
        $this->initializeMiddlewares();
        foreach ($this->middlewares as $middleware_id => $middleware) {
            $handler_stack->push($middleware, $middleware_id);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
HandlerStackConfigurator::$container protected property The service container.
HandlerStackConfigurator::$middlewareIds protected property A list of used middleware service IDs.
HandlerStackConfigurator::$middlewares protected property Array of middlewares to add to the handler stack.
HandlerStackConfigurator::configure public function Configures the stack using services tagged as http_client_middleware.
HandlerStackConfigurator::initializeMiddlewares protected function Ensures that the middlewares are initialized.
HandlerStackConfigurator::__construct public function Constructs a new HandlerStackConfigurator object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.