4a0bfdefa15f362caca207f549c59fc220e0d36b
[yaffs-website] / web / core / lib / Drupal / Core / Http / HandlerStackConfigurator.php
1 <?php
2
3 namespace Drupal\Core\Http;
4
5 use GuzzleHttp\HandlerStack;
6 use Symfony\Component\DependencyInjection\ContainerInterface;
7
8 /**
9  * Defines a class for configuring middlewares on the http handler stack.
10  *
11  * The http_client service requires a handler stack to perform http requests.
12  * This is provided by the http_handler_stack service. Modules wishing to add
13  * additional middlewares to the handler stack can create services and tag them
14  * as http_client_middleware. Each service must contain an __invoke method that
15  * returns a closure which will serve as the middleware.
16  *
17  * @see https://guzzle.readthedocs.org/en/latest/handlers-and-middleware.html
18  *
19  * @see \Drupal\Core\Http\Client
20  * @see \Drupal\Core\Test\HttpClientMiddleware\TestHttpClientMiddleware
21  */
22 class HandlerStackConfigurator {
23
24   /**
25    * Array of middlewares to add to the handler stack.
26    *
27    * @var callable[]
28    */
29   protected $middlewares = NULL;
30
31   /**
32    * A list of used middleware service IDs.
33    *
34    * @var string[]
35    */
36   protected $middlewareIds = [];
37
38   /**
39    * The service container.
40    *
41    * @var \Symfony\Component\DependencyInjection\ContainerInterface
42    */
43   protected $container;
44
45   /**
46    * Constructs a new HandlerStackConfigurator object.
47    *
48    * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
49    *   The service container.
50    * @param string[] $middleware_ids
51    *   The middleware IDs.
52    */
53   public function __construct(ContainerInterface $container, array $middleware_ids) {
54     $this->middlewareIds = $middleware_ids;
55     $this->container = $container;
56   }
57
58   /**
59    * Ensures that the middlewares are initialized.
60    */
61   protected function initializeMiddlewares() {
62     if (!isset($this->middlewares)) {
63       $this->middlewares = [];
64       foreach ($this->middlewareIds as $middleware_id) {
65         $middleware = $this->container->get($middleware_id);
66         if (is_callable($middleware)) {
67           $this->middlewares[$middleware_id] = $middleware();
68         }
69         else {
70           throw new \InvalidArgumentException('Middlewares need to implement __invoke, see https://guzzle.readthedocs.org/en/latest/handlers-and-middleware.html for more information about middlewares.');
71         }
72       }
73     }
74   }
75
76   /**
77    * Configures the stack using services tagged as http_client_middleware.
78    *
79    * @param \GuzzleHttp\HandlerStack $handler_stack
80    *   The handler stack
81    */
82   public function configure(HandlerStack $handler_stack) {
83     $this->initializeMiddlewares();
84     foreach ($this->middlewares as $middleware_id => $middleware) {
85       $handler_stack->push($middleware, $middleware_id);
86     }
87   }
88
89 }