Skip to content

Commit fece452

Browse files
Add php version to user agent
1 parent 40190b4 commit fece452

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

src/Cloudinary.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ class Cloudinary
1212
const RANGE_RE = '/^(\d+\.)?\d+[%pP]?\.\.(\d+\.)?\d+[%pP]?$/';
1313

1414
const VERSION = "1.9.0";
15-
/** @internal Do not change this value */
16-
const USER_AGENT = "CloudinaryPHP/1.9.0";
15+
16+
/**
17+
* Contains information about SDK user agent. Passed to the Cloudinary servers.
18+
*
19+
* Initialized on the first call to {@see self::userAgent()}
20+
*
21+
* Sample value: CloudinaryPHP/1.2.3 (PHP 5.6.7)
22+
*
23+
* @internal Do not change this value
24+
*/
25+
private static $USER_AGENT = "";
1726

1827
/**
1928
* Additional information to be passed with the USER_AGENT, e.g. "CloudinaryMagento/1.0.1".
@@ -23,7 +32,7 @@ class Cloudinary
2332
* The format of the value should be <ProductName>/Version[ (comment)].
2433
* @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.43
2534
*
26-
* <b>Do not set this value in application code!</b>
35+
* @internal <b>Do not set this value in application code!</b>
2736
*
2837
* @var string
2938
*/
@@ -32,6 +41,7 @@ class Cloudinary
3241
public static $DEFAULT_RESPONSIVE_WIDTH_TRANSFORMATION = array("width" => "auto", "crop" => "limit");
3342

3443
private static $config = null;
44+
3545
public static $JS_CONFIG_PARAMS = array(
3646
"api_key",
3747
"cloud_name",
@@ -41,19 +51,23 @@ class Cloudinary
4151
);
4252

4353
/**
44-
* Provides the USER_AGENT string that is passed to the Cloudinary servers.
54+
* Provides the {@see self::$USER_AGENT} string that is passed to the Cloudinary servers.
4555
*
46-
* Prepends {@link $USER_PLATFORM} if it is defined.
56+
* Prepends {@see self::$USER_PLATFORM} if it is defined.
4757
*
4858
* @return string
4959
*/
5060
public static function userAgent()
5161
{
52-
if (self::$USER_PLATFORM == "") {
53-
return self::USER_AGENT;
54-
} else {
55-
return self::$USER_PLATFORM . " " . self::USER_AGENT;
62+
if (empty(self::$USER_AGENT)) {
63+
self::$USER_AGENT = 'CloudinaryPHP/' . self::VERSION . ' (PHP ' . PHP_VERSION. ')';
5664
}
65+
66+
if (empty(self::$USER_PLATFORM)) {
67+
return self::$USER_AGENT;
68+
}
69+
70+
return self::$USER_PLATFORM . ' ' . self::$USER_AGENT;
5771
}
5872

5973
public static function is_not_null($var)

tests/CloudinaryTest.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,19 @@ public function test_cloud_name_options()
4343

4444
public function test_user_agent()
4545
{
46-
$tmp = \Cloudinary::$USER_PLATFORM;
46+
$user_agent = \Cloudinary::userAgent();
47+
48+
$this->assertRegExp("/^CloudinaryPHP\/\d+\.\d+\.\d+ \(PHP \d+\.\d+\.\d+\)$/", $user_agent);
49+
50+
$orig_user_platform = \Cloudinary::$USER_PLATFORM;
4751
$platform_information = 'TestPlatformInformation (From \"CloudinaryTest.php\")';
4852
\Cloudinary::$USER_PLATFORM = $platform_information;
49-
$userAgent = \Cloudinary::userAgent();
50-
\Cloudinary::$USER_PLATFORM = $tmp; // reset value
51-
$this->assertRegExp("/CloudinaryPHP\/\d+\.\d+\.\d+/", $userAgent);
52-
$this->assertContains(
53-
$platform_information,
54-
$userAgent,
53+
$full_user_agent = \Cloudinary::userAgent();
54+
\Cloudinary::$USER_PLATFORM = $orig_user_platform; // reset value
55+
56+
$this->assertEquals(
57+
$platform_information . ' ' . $user_agent,
58+
$full_user_agent,
5559
"USER_AGENT should include platform information if set"
5660
);
5761
}

0 commit comments

Comments
 (0)