e429bbb9cda4734f9c83db529e53afee50d4b3ca
[yaffs-website] / web / core / lib / Drupal / Core / Http / ClientFactory.php
1 <?php
2
3 namespace Drupal\Core\Http;
4
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Site\Settings;
7 use GuzzleHttp\Client;
8 use GuzzleHttp\HandlerStack;
9
10 /**
11  * Helper class to construct a HTTP client with Drupal specific config.
12  */
13 class ClientFactory {
14
15   /**
16    * The handler stack.
17    *
18    * @var \GuzzleHttp\HandlerStack
19    */
20   protected $stack;
21
22   /**
23    * Constructs a new ClientFactory instance.
24    *
25    * @param \GuzzleHttp\HandlerStack $stack
26    *   The handler stack.
27    */
28   public function __construct(HandlerStack $stack) {
29     $this->stack = $stack;
30   }
31
32   /**
33    * Constructs a new client object from some configuration.
34    *
35    * @param array $config
36    *   The config for the client.
37    *
38    * @return \GuzzleHttp\Client
39    *   The HTTP client.
40    */
41   public function fromOptions(array $config = []) {
42     $default_config = [
43       // Security consideration: we must not use the certificate authority
44       // file shipped with Guzzle because it can easily get outdated if a
45       // certificate authority is hacked. Instead, we rely on the certificate
46       // authority file provided by the operating system which is more likely
47       // going to be updated in a timely fashion. This overrides the default
48       // path to the pem file bundled with Guzzle.
49       'verify' => TRUE,
50       'timeout' => 30,
51       'headers' => [
52         'User-Agent' => 'Drupal/' . \Drupal::VERSION . ' (+https://www.drupal.org/) ' . \GuzzleHttp\default_user_agent(),
53       ],
54       'handler' => $this->stack,
55       // Security consideration: prevent Guzzle from using environment variables
56       // to configure the outbound proxy.
57       'proxy' => [
58         'http' => NULL,
59         'https' => NULL,
60         'no' => [],
61       ],
62     ];
63
64     $config = NestedArray::mergeDeep($default_config, Settings::get('http_client_config', []), $config);
65
66     return new Client($config);
67   }
68
69 }