From 2f454c45b37fde644cd3f2338fcbf07dd65a3fea Mon Sep 17 00:00:00 2001 From: Michele Lazzeri <19763535+mlazze@users.noreply.github.com> Date: Tue, 25 Apr 2023 09:05:31 +0200 Subject: [PATCH 1/3] Custom path callable (#282) Added cache path callable --- docs/1.0/config/setup.md | 1 + src/Server.php | 32 +++++++++++++++++++++++++ src/ServerFactory.php | 11 +++++++++ tests/ServerTest.php | 51 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 95 insertions(+) diff --git a/docs/1.0/config/setup.md b/docs/1.0/config/setup.md index cae35a75..95e3bc8e 100644 --- a/docs/1.0/config/setup.md +++ b/docs/1.0/config/setup.md @@ -21,6 +21,7 @@ $server = League\Glide\ServerFactory::create([ 'cache_path_prefix' => // Cache filesystem path prefix 'temp_dir' => // Temporary directory where cache EXIF data should be stored // (defaults to sys_get_temp_dir()) + 'cache_path_callable' => // Custom cache path callable 'group_cache_in_folders' => // Whether to group cached images in folders 'watermarks' => // Watermarks filesystem 'watermarks_path_prefix' => // Watermarks filesystem path prefix diff --git a/src/Server.php b/src/Server.php index ed0cbb6b..63293785 100644 --- a/src/Server.php +++ b/src/Server.php @@ -95,6 +95,12 @@ class Server * @var array */ protected $presets = []; + /** + * Custom cache path callable. + * + * @var \Closure|null + */ + protected $cachePathCallable; /** * Create Server instance. @@ -339,6 +345,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. * @@ -349,6 +375,12 @@ public function getCacheWithFileExtensions() */ public function getCachePath($path, array $params = []) { + $customCallable = $this->getCachePathCallable(); + if (null !== $customCallable) { + $boundCallable = \Closure::bind($customCallable, $this, self::class); + + return $boundCallable($path, $params); + } $sourcePath = $this->getSourcePath($path); if ($this->sourcePathPrefix) { diff --git a/src/ServerFactory.php b/src/ServerFactory.php index 778e7da0..3dbf987c 100644 --- a/src/ServerFactory.php +++ b/src/ServerFactory.php @@ -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()); @@ -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. * diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 3048ba6e..bd58a63a 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -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); From b0d5edb7223328fbd8b26b2cca2679115ea3ae75 Mon Sep 17 00:00:00 2001 From: ADmad Date: Tue, 25 Apr 2023 12:39:02 +0530 Subject: [PATCH 2/3] Update docs. --- docs/1.0/config/setup.md | 3 +-- docs/2.0/config/setup.md | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/1.0/config/setup.md b/docs/1.0/config/setup.md index 95e3bc8e..cb4ae71c 100644 --- a/docs/1.0/config/setup.md +++ b/docs/1.0/config/setup.md @@ -19,9 +19,8 @@ $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()) - 'cache_path_callable' => // Custom cache path callable 'group_cache_in_folders' => // Whether to group cached images in folders 'watermarks' => // Watermarks filesystem 'watermarks_path_prefix' => // Watermarks filesystem path prefix diff --git a/docs/2.0/config/setup.md b/docs/2.0/config/setup.md index 35399ea9..f52e2129 100644 --- a/docs/2.0/config/setup.md +++ b/docs/2.0/config/setup.md @@ -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) From 9ed0d5ee202683d54180b3410ffaf60b6045e998 Mon Sep 17 00:00:00 2001 From: ADmad Date: Tue, 25 Apr 2023 12:49:32 +0530 Subject: [PATCH 3/3] Code formatting changes --- src/Server.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Server.php b/src/Server.php index 63293785..fb77046e 100644 --- a/src/Server.php +++ b/src/Server.php @@ -2,6 +2,7 @@ namespace League\Glide; +use Closure; use InvalidArgumentException; use League\Flysystem\FilesystemException as FilesystemV2Exception; use League\Flysystem\FilesystemOperator; @@ -95,6 +96,7 @@ class Server * @var array */ protected $presets = []; + /** * Custom cache path callable. * @@ -350,7 +352,7 @@ public function getCacheWithFileExtensions() * * @param \Closure|null $cachePathCallable The custom cache path callable. It receives the same arguments as @see getCachePath */ - public function setCachePathCallable(?\Closure $cachePathCallable) + public function setCachePathCallable(?Closure $cachePathCallable) { $this->cachePathCallable = $cachePathCallable; } @@ -377,10 +379,11 @@ public function getCachePath($path, array $params = []) { $customCallable = $this->getCachePathCallable(); if (null !== $customCallable) { - $boundCallable = \Closure::bind($customCallable, $this, self::class); + $boundCallable = Closure::bind($customCallable, $this, static::class); return $boundCallable($path, $params); } + $sourcePath = $this->getSourcePath($path); if ($this->sourcePathPrefix) {