UserAgent.php 2.7 KB

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