Skip to content

Commit

Permalink
Add new "group cache in folders" option.
Browse files Browse the repository at this point in the history
  • Loading branch information
reinink committed Dec 24, 2015
1 parent c45cef0 commit ec5c79a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Server.php
Expand Up @@ -36,6 +36,12 @@ class Server
*/
protected $cachePathPrefix;

/**
* Whether to group cache in folders.
* @var bool
*/
protected $groupCacheInFolders = true;

/**
* Image manipulation API.
* @var ApiInterface
Expand Down Expand Up @@ -204,6 +210,24 @@ public function getCachePathPrefix()
return $this->cachePathPrefix;
}

/**
* Set the group cache in folders setting.
* @param bool $groupCacheInFolders Whether to group cache in folders.
*/
public function setGroupCacheInFolders($groupCacheInFolders)
{
$this->groupCacheInFolders = $groupCacheInFolders;
}

/**
* Get the group cache in folders setting.
* @return bool Whether to group cache in folders.
*/
public function getGroupCacheInFolders()
{
return $this->groupCacheInFolders;
}

/**
* Get cache path.
* @param string $path Image path.
Expand All @@ -224,7 +248,7 @@ public function getCachePath($path, array $params = [])

$md5 = md5($sourcePath.'?'.http_build_query($params));

$path = $sourcePath.'/'.$md5;
$path = $this->groupCacheInFolders ? $sourcePath.'/'.$md5 : $md5;

if ($this->cachePathPrefix) {
$path = $this->cachePathPrefix.'/'.$path;
Expand Down
14 changes: 14 additions & 0 deletions src/ServerFactory.php
Expand Up @@ -55,6 +55,7 @@ public function getServer()

$server->setSourcePathPrefix($this->getSourcePathPrefix());
$server->setCachePathPrefix($this->getCachePathPrefix());
$server->setGroupCacheInFolders($this->getGroupCacheInFolders());
$server->setDefaults($this->getDefaults());
$server->setPresets($this->getPresets());
$server->setBaseUrl($this->getBaseUrl());
Expand Down Expand Up @@ -123,6 +124,19 @@ public function getCachePathPrefix()
}
}

/**
* Get the group cache in folders setting.
* @return bool Whether to group cache in folders.
*/
public function getGroupCacheInFolders()
{
if (isset($this->config['group_cache_in_folders'])) {
return $this->config['group_cache_in_folders'];
}

return true;
}

/**
* Get watermarks file system.
* @return FilesystemInterface|null Watermarks file system.
Expand Down
13 changes: 13 additions & 0 deletions tests/ServerFactoryTest.php
Expand Up @@ -92,6 +92,19 @@ public function testGetCachePathPrefix()
$this->assertSame('cache', $server->getCachePathPrefix());
}

public function testGetGroupCacheInFolders()
{
$server = new ServerFactory();

$this->assertTrue($server->getGroupCacheInFolders());

$server = new ServerFactory([
'group_cache_in_folders' => false,
]);

$this->assertFalse($server->getGroupCacheInFolders());
}

public function testGetWatermarks()
{
$server = new ServerFactory([
Expand Down
22 changes: 22 additions & 0 deletions tests/ServerTest.php
Expand Up @@ -135,6 +135,18 @@ public function testGetCachePathPrefix()
$this->assertEquals('', $this->server->getCachePathPrefix());
}

public function testSetGroupCacheInFolders()
{
$this->server->setGroupCacheInFolders(false);

$this->assertFalse($this->server->getGroupCacheInFolders());
}

public function testGetGroupCacheInFolders()
{
$this->assertTrue($this->server->getGroupCacheInFolders());
}

public function testGetCachePath()
{
$this->assertEquals(
Expand All @@ -143,6 +155,16 @@ public function testGetCachePath()
);
}

public function testGetCachePathWithNoFolderGrouping()
{
$this->server->setGroupCacheInFolders(false);

$this->assertEquals(
'e863e008b6f09807c3b0aa3805bc9c63',
$this->server->getCachePath('image.jpg', ['w' => '100'])
);
}

public function testGetCachePathWithPrefix()
{
$this->server->setCachePathPrefix('img/');
Expand Down

0 comments on commit ec5c79a

Please sign in to comment.