commercetools-sdk-php-v2  master
The platform, import-api and ml-api PHP sdks generated from our api reference.
UserAgentProvider.php
1 <?php
2 
3 
4 declare(strict_types=1);
11 namespace Commercetools\Client;
12 
13 use GuzzleHttp\ClientInterface;
14 
16 {
21  private $userAgent;
22  public const USER_AGENT = 'commercetools-sdk-php-v2';
23 
24  public function __construct(string $suffix = null)
25  {
26  if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
27  $clientVersion = (string) constant(ClientInterface::class . '::MAJOR_VERSION');
28  } else {
29  $clientVersion = (string) constant(ClientInterface::class . '::VERSION');
30  }
31 
32  $userAgent = self::USER_AGENT . $this->getPackageVersion();
33 
34  $userAgent .= ' (GuzzleHttp/' . $clientVersion;
35  if (extension_loaded('curl') && function_exists('curl_version')) {
36  $userAgent .= '; curl/' . (string) \curl_version()['version'];
37  }
38  $userAgent .= ') PHP/' . PHP_VERSION;
39  if (!is_null($suffix)) {
40  $userAgent .= ' ' . $suffix;
41  }
42  $this->userAgent = $userAgent;
43  }
44 
45  private function getPackageVersion(): string
46  {
47  if (class_exists("\Commercetools\Client\PackageVersion")) {
48  $version = PackageVersion::get();
49  if ($version != null) {
50  return "/" . $version;
51  }
52  }
53  return "";
54  }
55 
56  public function getUserAgent(): string
57  {
58  return $this->userAgent;
59  }
60 }