Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions src/Cloudinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@ class Cloudinary
const RANGE_RE = '/^(\d+\.)?\d+[%pP]?\.\.(\d+\.)?\d+[%pP]?$/';

const VERSION = "1.9.0";
/** @internal Do not change this value */
const USER_AGENT = "CloudinaryPHP/1.9.0";

/**
* Contains information about SDK user agent. Passed to the Cloudinary servers.
*
* Initialized on the first call to {@see self::userAgent()}
*
* Sample value: CloudinaryPHP/1.2.3 (PHP 5.6.7)
*
* @internal Do not change this value
*/
private static $USER_AGENT = "";

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

private static $config = null;

public static $JS_CONFIG_PARAMS = array(
"api_key",
"cloud_name",
Expand All @@ -41,19 +51,23 @@ class Cloudinary
);

/**
* Provides the USER_AGENT string that is passed to the Cloudinary servers.
* Provides the {@see self::$USER_AGENT} string that is passed to the Cloudinary servers.
*
* Prepends {@link $USER_PLATFORM} if it is defined.
* Prepends {@see self::$USER_PLATFORM} if it is defined.
*
* @return string
*/
public static function userAgent()
{
if (self::$USER_PLATFORM == "") {
return self::USER_AGENT;
} else {
return self::$USER_PLATFORM . " " . self::USER_AGENT;
if (empty(self::$USER_AGENT)) {
self::$USER_AGENT = 'CloudinaryPHP/' . self::VERSION . ' (PHP ' . PHP_VERSION. ')';
}

if (empty(self::$USER_PLATFORM)) {
return self::$USER_AGENT;
}

return self::$USER_PLATFORM . ' ' . self::$USER_AGENT;
}

public static function is_not_null($var)
Expand Down
26 changes: 19 additions & 7 deletions tests/CloudinaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class CloudinaryTest extends TestCase
const DEFAULT_UPLOAD_PATH = 'http://res.cloudinary.com/test123/image/upload/';
const VIDEO_UPLOAD_PATH = 'http://res.cloudinary.com/test123/video/upload/';

private $original_user_platform;

public function setUp()
{
Cloudinary::reset_config();
Expand All @@ -25,6 +27,14 @@ public function setUp()
"cname" => null
)
);

$this->original_user_platform = \Cloudinary::$USER_PLATFORM;
}

public function tearDown()
{
parent::TearDown();
\Cloudinary::$USER_PLATFORM = $this->original_user_platform;
}

public function test_cloud_name()
Expand All @@ -43,15 +53,17 @@ public function test_cloud_name_options()

public function test_user_agent()
{
$tmp = \Cloudinary::$USER_PLATFORM;
$user_agent = \Cloudinary::userAgent();

$this->assertRegExp("/^CloudinaryPHP\/\d+\.\d+\.\d+ \(PHP \d+\.\d+\.\d+\)$/", $user_agent);

$platform_information = 'TestPlatformInformation (From \"CloudinaryTest.php\")';
\Cloudinary::$USER_PLATFORM = $platform_information;
$userAgent = \Cloudinary::userAgent();
\Cloudinary::$USER_PLATFORM = $tmp; // reset value
$this->assertRegExp("/CloudinaryPHP\/\d+\.\d+\.\d+/", $userAgent);
$this->assertContains(
$platform_information,
$userAgent,
$full_user_agent = \Cloudinary::userAgent();

$this->assertEquals(
$platform_information . ' ' . $user_agent,
$full_user_agent,
"USER_AGENT should include platform information if set"
);
}
Expand Down