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