Skip to content

Commit

Permalink
Merge pull request #7 from imgix/7-ixlib-param
Browse files Browse the repository at this point in the history
Add the `ixlib` parameter to every request
  • Loading branch information
kellysutton committed Jun 4, 2015
2 parents a8ab87b + b6fa07c commit fa45726
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "imgix/imgix-php",
"description": "A PHP client library for generating URLs with imgix.",
"type": "library",
"version": "1.0.5",
"keywords": [
"imgix"
],
Expand Down
8 changes: 7 additions & 1 deletion src/Imgix/UrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

class UrlBuilder {

private $currentVersion = "1.0.5";
private $domains;
private $useHttps;
private $signKey;
private $shardStrategy;

private $shardCycleNextIndex = 0;

public function __construct($domains, $useHttps = false, $signKey = "", $shardStrategy = ShardStrategy::CRC) {
public function __construct($domains, $useHttps = false, $signKey = "", $shardStrategy = ShardStrategy::CRC, $includeLibraryParam = true) {
if (!is_array($domains)) {
$this->domains = array($domains);
} else {
Expand All @@ -25,6 +26,7 @@ public function __construct($domains, $useHttps = false, $signKey = "", $shardSt
$this->useHttps = $useHttps;
$this->signKey = $signKey;
$this->shardStratgy = $shardStrategy;
$this->includeLibraryParam = $includeLibraryParam;
}

public function setShardStrategy($start) {
Expand Down Expand Up @@ -52,6 +54,10 @@ public function createURL($path, $params=array()) {
$domain = $this->domains[0];
}

if ($this->includeLibraryParam) {
$params['ixlib'] = "php-" . $this->currentVersion;
}

$uh = new UrlHelper($domain, $path, $scheme, $this->signKey, $params);

return $uh->getURL();
Expand Down
23 changes: 16 additions & 7 deletions tests/Imgix/Tests/UrlBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testUrlBuilderCycleShard() {

$domains = array("jackangers.imgix.net", "jackangers2.imgix.net", "jackangers3.imgix.net");

$ub = new URLBuilder($domains);
$ub = new URLBuilder($domains, false, "", ShardStrategy::CRC, false);
$ub->setShardStrategy(ShardStrategy::CYCLE);

for ($i = 0; $i < 100; $i++) {
Expand All @@ -34,7 +34,7 @@ public function testUrlBuilderCycleShard() {
}

public function testExamplePlain() {
$builder = new UrlBuilder("demos.imgix.net");
$builder = new UrlBuilder("demos.imgix.net", false, "", ShardStrategy::CRC, false);

$params = array("w" => 100, "h" => 100);
$url = $builder->createURL("bridge.png", $params);
Expand All @@ -43,7 +43,7 @@ public function testExamplePlain() {
}

public function testExamplePlainHttps() {
$builder = new UrlBuilder("demos.imgix.net");
$builder = new UrlBuilder("demos.imgix.net", false, "", ShardStrategy::CRC, false);

$builder->setUseHttps(true);
$params = array("w" => 100, "h" => 100);
Expand All @@ -53,7 +53,7 @@ public function testExamplePlainHttps() {
}

public function testExamplePlainSecure() {
$builder = new UrlBuilder("demos.imgix.net");
$builder = new UrlBuilder("demos.imgix.net", false, "", ShardStrategy::CRC, false);
$builder->setSignKey("test1234");
$params = array("w" => 100, "h" => 100);
$url = $builder->createURL("bridge.png", $params);
Expand All @@ -62,27 +62,36 @@ public function testExamplePlainSecure() {
}

public function testWithFullyQualifiedUrl() {
$builder = new UrlBuilder("demos.imgix.net", true);
$builder = new UrlBuilder("demos.imgix.net", true, "", ShardStrategy::CRC, false);
$builder->setSignKey("test1234");
$url = $builder->createUrl("http://media.giphy.com/media/jCMq0p94fgBIk/giphy.gif");

$this->assertEquals("https://demos.imgix.net/http%3A%2F%2Fmedia.giphy.com%2Fmedia%2FjCMq0p94fgBIk%2Fgiphy.gif?&s=ffc3359566fe1dc6445ad17d17b98951", $url);
}

public function testWithFullyQualifiedUrlWithSpaces() {
$builder = new UrlBuilder("demos.imgix.net", true);
$builder = new UrlBuilder("demos.imgix.net", true, "", ShardStrategy::CRC, false);
$builder->setSignKey("test1234");
$url = $builder->createUrl("https://my-demo-site.com/files/133467012/avatar icon.png");

$this->assertEquals("https://demos.imgix.net/https%3A%2F%2Fmy-demo-site.com%2Ffiles%2F133467012%2Favatar+icon.png?&s=f6a4e1504af365564014564f1d7e13de", $url);
}

public function testWithFullyQualifiedUrlWithParams() {
$builder = new UrlBuilder("demos.imgix.net", true);
$builder = new UrlBuilder("demos.imgix.net", true, "", ShardStrategy::CRC, false);
$builder->setSignKey("test1234");
$url = $builder->createUrl("https://my-demo-site.com/files/133467012/avatar icon.png?some=chill&params=1");

$this->assertEquals("https://demos.imgix.net/https%3A%2F%2Fmy-demo-site.com%2Ffiles%2F133467012%2Favatar+icon.png%3Fsome%3Dchill%26params%3D1?&s=259b9ca6206721752ad7a3ce50f08dd2", $url);
}

public function testInclusionOfLibraryVersionParam() {
$builder = new UrlBuilder("demos.imgix.net", true);
$url = $builder->createUrl("https://my-demo-site.com/files/133467012/avatar icon.png?some=chill&params=1");
$composerFileJson = json_decode(file_get_contents("./composer.json"), true);
$version = $composerFileJson['version'];

$this->assertEquals("https://demos.imgix.net/https%3A%2F%2Fmy-demo-site.com%2Ffiles%2F133467012%2Favatar+icon.png%3Fsome%3Dchill%26params%3D1?ixlib=php-" . $version, $url);
}
}
?>

0 comments on commit fa45726

Please sign in to comment.