HandlerStackConfigurator.php 2.6 KB

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