Skip to content

Commit 2ff92c8

Browse files
authored
Merge pull request #370 from thephpleague/cache-path-callable
Cache path callable
2 parents 7bf821e + 9ed0d5e commit 2ff92c8

File tree

5 files changed

+99
-1
lines changed

5 files changed

+99
-1
lines changed

docs/1.0/config/setup.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ $server = League\Glide\ServerFactory::create([
1919
'source_path_prefix' => // Source filesystem path prefix
2020
'cache' => // Cache filesystem
2121
'cache_path_prefix' => // Cache filesystem path prefix
22-
'temp_dir' => // Temporary directory where cache EXIF data should be stored
22+
'temp_dir' => // Temporary directory where cache EXIF data should be stored
2323
// (defaults to sys_get_temp_dir())
2424
'group_cache_in_folders' => // Whether to group cached images in folders
2525
'watermarks' => // Watermarks filesystem

docs/2.0/config/setup.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ $server = League\Glide\ServerFactory::create([
2323
// (defaults to sys_get_temp_dir())
2424
'group_cache_in_folders' => // Whether to group cached images in folders
2525
'cache_with_file_extensions' => // Whether to include file extension in cache filename. Default `false`.
26+
'cache_path_callable' => // Custom cache path callable
2627
'watermarks' => // Watermarks filesystem
2728
'watermarks_path_prefix' => // Watermarks filesystem path prefix
2829
'driver' => // Image driver (gd or imagick)

src/Server.php

+35
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace League\Glide;
44

5+
use Closure;
56
use InvalidArgumentException;
67
use League\Flysystem\FilesystemException as FilesystemV2Exception;
78
use League\Flysystem\FilesystemOperator;
@@ -96,6 +97,13 @@ class Server
9697
*/
9798
protected $presets = [];
9899

100+
/**
101+
* Custom cache path callable.
102+
*
103+
* @var \Closure|null
104+
*/
105+
protected $cachePathCallable;
106+
99107
/**
100108
* Create Server instance.
101109
*
@@ -339,6 +347,26 @@ public function getCacheWithFileExtensions()
339347
return $this->cacheWithFileExtensions;
340348
}
341349

350+
/**
351+
* Set a custom cachePathCallable.
352+
*
353+
* @param \Closure|null $cachePathCallable The custom cache path callable. It receives the same arguments as @see getCachePath
354+
*/
355+
public function setCachePathCallable(?Closure $cachePathCallable)
356+
{
357+
$this->cachePathCallable = $cachePathCallable;
358+
}
359+
360+
/**
361+
* Gets the custom cachePathCallable.
362+
*
363+
* @return \Closure|null The custom cache path callable. It receives the same arguments as @see getCachePath
364+
*/
365+
public function getCachePathCallable()
366+
{
367+
return $this->cachePathCallable;
368+
}
369+
342370
/**
343371
* Get cache path.
344372
*
@@ -349,6 +377,13 @@ public function getCacheWithFileExtensions()
349377
*/
350378
public function getCachePath($path, array $params = [])
351379
{
380+
$customCallable = $this->getCachePathCallable();
381+
if (null !== $customCallable) {
382+
$boundCallable = Closure::bind($customCallable, $this, static::class);
383+
384+
return $boundCallable($path, $params);
385+
}
386+
352387
$sourcePath = $this->getSourcePath($path);
353388

354389
if ($this->sourcePathPrefix) {

src/ServerFactory.php

+11
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ public function getServer()
6565
$server->setPresets($this->getPresets());
6666
$server->setBaseUrl($this->getBaseUrl() ?: '');
6767
$server->setResponseFactory($this->getResponseFactory());
68+
$server->setCachePathCallable($this->getCachePathCallable());
6869

6970
if ($this->getTempDir()) {
7071
$server->setTempDir($this->getTempDir());
@@ -149,6 +150,16 @@ public function getTempDir()
149150
}
150151
}
151152

153+
/**
154+
* Get cache path callable.
155+
*
156+
* @return \Closure|null Cache path callable.
157+
*/
158+
public function getCachePathCallable()
159+
{
160+
return $this->config['cache_path_callable'] ?? null;
161+
}
162+
152163
/**
153164
* Get the group cache in folders setting.
154165
*

tests/ServerTest.php

+51
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,57 @@ public function testSetGetTempDir()
154154
$this->assertSame(__DIR__.DIRECTORY_SEPARATOR, $this->server->getTempDir());
155155
}
156156

157+
public function testSetCachePathCallable()
158+
{
159+
$this->server->setCachePathCallable(null);
160+
$this->assertEquals(null, $this->server->getCachePathCallable());
161+
}
162+
163+
public function testGetCachePathCallable()
164+
{
165+
$this->assertEquals(null, $this->server->getCachePathCallable());
166+
}
167+
168+
public function testCachePathCallableIsCalledOnGetCachePath()
169+
{
170+
$expected = 'TEST';
171+
$callable = function () use ($expected) {
172+
return $expected;
173+
};
174+
175+
$this->server->setCachePathCallable($callable);
176+
177+
self::assertEquals($expected, $this->server->getCachePath(''));
178+
}
179+
180+
public function testSetCachePathCallableIsBoundClosure()
181+
{
182+
$server = $this->server;
183+
$phpUnit = $this;
184+
$this->server->setCachePathCallable(function () use ($phpUnit, $server) {
185+
$phpUnit::assertEquals($server, $this);
186+
});
187+
188+
$this->server->getCachePath('');
189+
}
190+
191+
public function testSetCachePathCallableArgumentsAreSameAsGetCachePath()
192+
{
193+
$phpUnit = $this;
194+
$pathArgument = 'TEST';
195+
$optionsArgument = [
196+
'TEST' => 'TEST',
197+
];
198+
$this->server->setCachePathCallable(function () use ($optionsArgument, $pathArgument, $phpUnit) {
199+
$arguments = func_get_args();
200+
$phpUnit::assertCount(2, $arguments);
201+
$phpUnit::assertEquals($arguments[0], $pathArgument);
202+
$phpUnit::assertEquals($arguments[1], $optionsArgument);
203+
});
204+
205+
$this->server->getCachePath($pathArgument, $optionsArgument);
206+
}
207+
157208
public function testSetGroupCacheInFolders()
158209
{
159210
$this->server->setGroupCacheInFolders(false);

0 commit comments

Comments
 (0)