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