1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AlibabaCloud\Client\Request; |
4
|
|
|
|
5
|
|
|
use AlibabaCloud\Client\AlibabaCloud; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class UserAgent |
10
|
|
|
* |
11
|
|
|
* @package AlibabaCloud\Client\Request |
12
|
|
|
*/ |
13
|
|
|
class UserAgent |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private static $userAgent = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private static $guard = [ |
25
|
|
|
'client', |
26
|
|
|
'guzzlehttp', |
27
|
|
|
'curl', |
28
|
|
|
'php', |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* UserAgent constructor. |
33
|
|
|
*/ |
34
|
76 |
|
private static function defaultFields() |
35
|
|
|
{ |
36
|
76 |
|
if (self::$userAgent === []) { |
37
|
10 |
|
self::$userAgent = [ |
38
|
10 |
|
'Client' => AlibabaCloud::VERSION, |
39
|
10 |
|
'GuzzleHttp' => Client::VERSION, |
40
|
10 |
|
'curl' => isset(\curl_version()['version']) |
41
|
10 |
|
? \curl_version()['version'] |
42
|
10 |
|
: 'none', |
43
|
10 |
|
'PHP' => \PHP_VERSION, |
44
|
10 |
|
]; |
45
|
10 |
|
} |
46
|
76 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param array $append |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
76 |
|
public static function toString(array $append = []) |
54
|
|
|
{ |
55
|
76 |
|
self::defaultFields(); |
56
|
|
|
|
57
|
76 |
|
$os = \PHP_OS; |
58
|
76 |
|
$os_version = php_uname('r'); |
59
|
76 |
|
$os_mode = php_uname('m'); |
60
|
76 |
|
$userAgent = "AlibabaCloud ($os $os_version; $os_mode) "; |
61
|
|
|
|
62
|
76 |
|
$newUserAgent = []; |
63
|
|
|
|
64
|
76 |
|
foreach ($append as $key => $value) { |
65
|
3 |
|
if (isset(self::$userAgent[$key])) { |
66
|
1 |
|
unset($append[$key]); |
67
|
1 |
|
} |
68
|
76 |
|
} |
69
|
|
|
|
70
|
76 |
|
$append = array_merge(self::$userAgent, $append); |
71
|
|
|
|
72
|
76 |
|
foreach ($append as $key => $value) { |
73
|
76 |
|
if ($value === null) { |
74
|
|
|
$newUserAgent[] = $key; |
75
|
|
|
continue; |
76
|
|
|
} |
77
|
76 |
|
$newUserAgent[] = "$key/$value"; |
78
|
76 |
|
} |
79
|
|
|
|
80
|
76 |
|
return $userAgent . \implode(' ', $newUserAgent); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* set User Agent of Alibaba Cloud. |
85
|
|
|
* |
86
|
|
|
* @param string $name |
87
|
|
|
* @param string $value |
88
|
|
|
*/ |
89
|
7 |
|
public static function append($name, $value) |
90
|
|
|
{ |
91
|
7 |
|
self::defaultFields(); |
92
|
|
|
|
93
|
7 |
|
if (!self::isGuarded($name)) { |
94
|
6 |
|
self::$userAgent[$name] = $value; |
95
|
6 |
|
} |
96
|
7 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array $userAgent |
100
|
|
|
*/ |
101
|
2 |
|
public static function with(array $userAgent) |
102
|
|
|
{ |
103
|
2 |
|
foreach ($userAgent as $key => $value) { |
104
|
2 |
|
self::append($key, $value); |
105
|
2 |
|
} |
106
|
2 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Clear all of the User Agent. |
110
|
|
|
*/ |
111
|
9 |
|
public static function clear() |
112
|
|
|
{ |
113
|
9 |
|
self::$userAgent = []; |
114
|
9 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param $name |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
7 |
|
public static function isGuarded($name) |
122
|
|
|
{ |
123
|
7 |
|
return in_array(strtolower($name), self::$guard, true); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|