1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the overtrue/http. |
5
|
|
|
* |
6
|
|
|
* (c) overtrue <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Overtrue\Http; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Config. |
16
|
|
|
* |
17
|
|
|
* @author overtrue <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class Config |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @see http://docs.guzzlephp.org/en/latest/request-options.html |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $options = [ |
27
|
|
|
'base_uri' => null, |
28
|
|
|
'timeout' => 3000, |
29
|
|
|
'connect_timeout' => 3000, |
30
|
|
|
'proxy' => [], |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $autoTrimEndpointSlash = true; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Config constructor. |
40
|
|
|
* |
41
|
|
|
* @param array $options |
42
|
|
|
*/ |
43
|
|
|
public function __construct(array $options = []) |
44
|
|
|
{ |
45
|
|
|
$this->options = array_merge($this->options, $options); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getBaseUri(): string |
52
|
|
|
{ |
53
|
|
|
return $this->options['base_uri'] ?? ''; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $baseUri |
58
|
|
|
* |
59
|
|
|
* @return \Overtrue\Http\Config |
60
|
|
|
*/ |
61
|
|
|
public function setBaseUri($baseUri): self |
62
|
|
|
{ |
63
|
|
|
$this->options['base_uri'] = $baseUri; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return int |
70
|
|
|
*/ |
71
|
|
|
public function getTimeout(): int |
72
|
|
|
{ |
73
|
|
|
return $this->options['timeout'] ?? 3000; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param int $timeout |
78
|
|
|
* |
79
|
|
|
* @return \Overtrue\Http\Config |
80
|
|
|
*/ |
81
|
|
|
public function setTimeout($timeout): self |
82
|
|
|
{ |
83
|
|
|
$this->options['timeout'] = $timeout; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
|
|
public function getConnectTimeout(): int |
92
|
|
|
{ |
93
|
|
|
return $this->options['connect_timeout'] ?? 3000; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param int $connectTimeout |
98
|
|
|
* |
99
|
|
|
* @return \Overtrue\Http\Config |
100
|
|
|
*/ |
101
|
|
|
public function setConnectTimeout($connectTimeout): self |
102
|
|
|
{ |
103
|
|
|
$this->options['connect_timeout'] = $connectTimeout; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
|
|
public function getProxy(): array |
112
|
|
|
{ |
113
|
|
|
return $this->options['proxy'] ?? []; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $proxy |
118
|
|
|
* |
119
|
|
|
* @return \Overtrue\Http\Config |
120
|
|
|
*/ |
121
|
|
|
public function setProxy(array $proxy): self |
122
|
|
|
{ |
123
|
|
|
$this->options['proxy'] = $proxy; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public function toArray(): array |
132
|
|
|
{ |
133
|
|
|
return $this->options; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $key |
138
|
|
|
* @param mixed $value |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
|
|
public function setOption($key, $value): self |
143
|
|
|
{ |
144
|
|
|
$this->options[$key] = $value; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $key |
151
|
|
|
* @param mixed $default |
152
|
|
|
* |
153
|
|
|
* @return mixed |
154
|
|
|
*/ |
155
|
|
|
public function getOption($key, $default = null) |
156
|
|
|
{ |
157
|
|
|
return $this->options[$key] ?? $default; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param array $options |
162
|
|
|
* |
163
|
|
|
* @return $this |
164
|
|
|
*/ |
165
|
|
|
public function mergeOptions(array $options): self |
166
|
|
|
{ |
167
|
|
|
$this->options = array_merge($this->options, $options); |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param array $options |
174
|
|
|
* |
175
|
|
|
* @return $this |
176
|
|
|
*/ |
177
|
|
|
public function setOptions(array $options): self |
178
|
|
|
{ |
179
|
|
|
$this->options = $options; |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return array |
186
|
|
|
*/ |
187
|
|
|
public function getOptions(): array |
188
|
|
|
{ |
189
|
|
|
return $this->options; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|