1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FAPI\Localise; |
11
|
|
|
|
12
|
|
|
use Http\Client\HttpClient; |
13
|
|
|
use Http\Client\Common\PluginClient; |
14
|
|
|
use Http\Discovery\HttpClientDiscovery; |
15
|
|
|
use Http\Discovery\UriFactoryDiscovery; |
16
|
|
|
use Http\Message\UriFactory; |
17
|
|
|
use Http\Client\Common\Plugin; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Configure an HTTP client. |
21
|
|
|
* |
22
|
|
|
* @author Tobias Nyholm <[email protected]> |
23
|
|
|
* |
24
|
|
|
* @internal This class should not be used outside of the API Client, it is not part of the BC promise. |
25
|
|
|
*/ |
26
|
|
|
final class HttpClientConfigurator |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $endpoint = 'https://localise.biz'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var UriFactory |
35
|
|
|
*/ |
36
|
|
|
private $uriFactory; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var HttpClient |
40
|
|
|
*/ |
41
|
|
|
private $httpClient; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Plugin[] |
45
|
|
|
*/ |
46
|
|
|
private $prependPlugins = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var Plugin[] |
50
|
|
|
*/ |
51
|
|
|
private $appendPlugins = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param HttpClient|null $httpClient |
55
|
|
|
* @param UriFactory|null $uriFactory |
56
|
|
|
*/ |
57
|
4 |
|
public function __construct(HttpClient $httpClient = null, UriFactory $uriFactory = null) |
58
|
|
|
{ |
59
|
4 |
|
$this->httpClient = $httpClient ?? HttpClientDiscovery::find(); |
60
|
4 |
|
$this->uriFactory = $uriFactory ?? UriFactoryDiscovery::find(); |
61
|
4 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return HttpClient |
65
|
|
|
*/ |
66
|
|
|
public function createConfiguredClient(): HttpClient |
67
|
|
|
{ |
68
|
|
|
$plugins = $this->prependPlugins; |
69
|
|
|
|
70
|
|
|
$plugins[] = new Plugin\AddHostPlugin($this->uriFactory->createUri($this->endpoint)); |
71
|
|
|
$plugins[] = new Plugin\HeaderDefaultsPlugin([ |
72
|
|
|
'User-Agent' => 'FriendsOfApi/Localise (https://github.com/FriendsOfApi/Localise)', |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
return new PluginClient($this->httpClient, array_merge($plugins, $this->appendPlugins)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $endpoint |
80
|
|
|
* |
81
|
|
|
* @return HttpClientConfigurator |
82
|
|
|
*/ |
83
|
|
|
public function setEndpoint(string $endpoint): self |
84
|
|
|
{ |
85
|
|
|
$this->endpoint = $endpoint; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param Plugin $plugin |
92
|
|
|
* |
93
|
|
|
* @return HttpClientConfigurator |
94
|
|
|
*/ |
95
|
2 |
|
public function appendPlugin(Plugin ...$plugin): self |
96
|
|
|
{ |
97
|
2 |
|
foreach ($plugin as $p) { |
98
|
2 |
|
$this->appendPlugins[] = $p; |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param Plugin $plugin |
106
|
|
|
* |
107
|
|
|
* @return HttpClientConfigurator |
108
|
|
|
*/ |
109
|
2 |
|
public function prependPlugin(Plugin ...$plugin): self |
110
|
|
|
{ |
111
|
2 |
|
$plugin = array_reverse($plugin); |
112
|
2 |
|
foreach ($plugin as $p) { |
113
|
2 |
|
array_unshift($this->prependPlugins, $p); |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|