Skip to content

Commit

Permalink
Merge pull request #370 from thephpleague/cache-path-callable
Browse files Browse the repository at this point in the history
Cache path callable
  • Loading branch information
ADmad authored Jul 8, 2023
2 parents 7bf821e + 9ed0d5e commit 2ff92c8
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/1.0/config/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ $server = League\Glide\ServerFactory::create([
'source_path_prefix' => // Source filesystem path prefix
'cache' => // Cache filesystem
'cache_path_prefix' => // Cache filesystem path prefix
'temp_dir' => // Temporary directory where cache EXIF data should be stored
'temp_dir' => // Temporary directory where cache EXIF data should be stored
// (defaults to sys_get_temp_dir())
'group_cache_in_folders' => // Whether to group cached images in folders
'watermarks' => // Watermarks filesystem
Expand Down
1 change: 1 addition & 0 deletions docs/2.0/config/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ $server = League\Glide\ServerFactory::create([
// (defaults to sys_get_temp_dir())
'group_cache_in_folders' => // Whether to group cached images in folders
'cache_with_file_extensions' => // Whether to include file extension in cache filename. Default `false`.
'cache_path_callable' => // Custom cache path callable
'watermarks' => // Watermarks filesystem
'watermarks_path_prefix' => // Watermarks filesystem path prefix
'driver' => // Image driver (gd or imagick)
Expand Down
35 changes: 35 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace League\Glide;

use Closure;
use InvalidArgumentException;
use League\Flysystem\FilesystemException as FilesystemV2Exception;
use League\Flysystem\FilesystemOperator;
Expand Down Expand Up @@ -96,6 +97,13 @@ class Server
*/
protected $presets = [];

/**
* Custom cache path callable.
*
* @var \Closure|null
*/
protected $cachePathCallable;

/**
* Create Server instance.
*
Expand Down Expand Up @@ -339,6 +347,26 @@ public function getCacheWithFileExtensions()
return $this->cacheWithFileExtensions;
}

/**
* Set a custom cachePathCallable.
*
* @param \Closure|null $cachePathCallable The custom cache path callable. It receives the same arguments as @see getCachePath
*/
public function setCachePathCallable(?Closure $cachePathCallable)
{
$this->cachePathCallable = $cachePathCallable;
}

/**
* Gets the custom cachePathCallable.
*
* @return \Closure|null The custom cache path callable. It receives the same arguments as @see getCachePath
*/
public function getCachePathCallable()
{
return $this->cachePathCallable;
}

/**
* Get cache path.
*
Expand All @@ -349,6 +377,13 @@ public function getCacheWithFileExtensions()
*/
public function getCachePath($path, array $params = [])
{
$customCallable = $this->getCachePathCallable();
if (null !== $customCallable) {
$boundCallable = Closure::bind($customCallable, $this, static::class);

return $boundCallable($path, $params);
}

$sourcePath = $this->getSourcePath($path);

if ($this->sourcePathPrefix) {
Expand Down
11 changes: 11 additions & 0 deletions src/ServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function getServer()
$server->setPresets($this->getPresets());
$server->setBaseUrl($this->getBaseUrl() ?: '');
$server->setResponseFactory($this->getResponseFactory());
$server->setCachePathCallable($this->getCachePathCallable());

if ($this->getTempDir()) {
$server->setTempDir($this->getTempDir());
Expand Down Expand Up @@ -149,6 +150,16 @@ public function getTempDir()
}
}

/**
* Get cache path callable.
*
* @return \Closure|null Cache path callable.
*/
public function getCachePathCallable()
{
return $this->config['cache_path_callable'] ?? null;
}

/**
* Get the group cache in folders setting.
*
Expand Down
51 changes: 51 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,57 @@ public function testSetGetTempDir()
$this->assertSame(__DIR__.DIRECTORY_SEPARATOR, $this->server->getTempDir());
}

public function testSetCachePathCallable()
{
$this->server->setCachePathCallable(null);
$this->assertEquals(null, $this->server->getCachePathCallable());
}

public function testGetCachePathCallable()
{
$this->assertEquals(null, $this->server->getCachePathCallable());
}

public function testCachePathCallableIsCalledOnGetCachePath()
{
$expected = 'TEST';
$callable = function () use ($expected) {
return $expected;
};

$this->server->setCachePathCallable($callable);

self::assertEquals($expected, $this->server->getCachePath(''));
}

public function testSetCachePathCallableIsBoundClosure()
{
$server = $this->server;
$phpUnit = $this;
$this->server->setCachePathCallable(function () use ($phpUnit, $server) {
$phpUnit::assertEquals($server, $this);
});

$this->server->getCachePath('');
}

public function testSetCachePathCallableArgumentsAreSameAsGetCachePath()
{
$phpUnit = $this;
$pathArgument = 'TEST';
$optionsArgument = [
'TEST' => 'TEST',
];
$this->server->setCachePathCallable(function () use ($optionsArgument, $pathArgument, $phpUnit) {
$arguments = func_get_args();
$phpUnit::assertCount(2, $arguments);
$phpUnit::assertEquals($arguments[0], $pathArgument);
$phpUnit::assertEquals($arguments[1], $optionsArgument);
});

$this->server->getCachePath($pathArgument, $optionsArgument);
}

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

0 comments on commit 2ff92c8

Please sign in to comment.