From ba3e4feaa172540740e8c489ceb23e4153e400c0 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Thu, 10 Jun 2021 14:40:32 +0200 Subject: [PATCH 1/8] Allow mount identifier, for MountManager of FlySystem library, into prefixes --- src/Server.php | 15 +++++++++++++-- tests/ServerTest.php | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/Server.php b/src/Server.php index a32a9ca4..b3a6534d 100644 --- a/src/Server.php +++ b/src/Server.php @@ -111,6 +111,17 @@ public function __construct(FilesystemOperator $source, FilesystemOperator $cach $this->tempDir = sys_get_temp_dir(); } + private function trimPrefix(string $prefix): string + { + if ('//' == substr($prefix, -2)) { + if (':' == substr(rtrim($prefix, '/'), -1)) { + return rtrim($prefix, '/') . '/'; + } + } + + return trim($prefix, '/'); + } + /** * Set source file system. * @@ -142,7 +153,7 @@ public function getSource() */ public function setSourcePathPrefix($sourcePathPrefix) { - $this->sourcePathPrefix = trim($sourcePathPrefix, '/'); + $this->sourcePathPrefix = $this->trimPrefix($sourcePathPrefix); } /** @@ -254,7 +265,7 @@ public function getCache() */ public function setCachePathPrefix($cachePathPrefix) { - $this->cachePathPrefix = trim($cachePathPrefix, '/'); + $this->cachePathPrefix = $this->trimPrefix($cachePathPrefix); } /** diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 3048ba6e..6c16d453 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -61,6 +61,15 @@ public function testSetSourcePathPrefix() $this->assertEquals('img', $this->server->getSourcePathPrefix()); } + public function testSetSourcePathPrefixWithMonthIdentifier() + { + $this->server->setSourcePathPrefix('img://'); + $this->assertEquals('img:/', $this->server->getSourcePathPrefix()); + + $this->server->setSourcePathPrefix('img:///'); + $this->assertEquals('img:/', $this->server->getSourcePathPrefix()); + } + public function testGetSourcePathPrefix() { $this->assertEquals('', $this->server->getSourcePathPrefix()); @@ -85,6 +94,22 @@ public function testGetSourcePathWithPrefix() { $this->server->setSourcePathPrefix('img/'); $this->assertEquals('img/image.jpg', $this->server->getSourcePath('image.jpg')); + + $this->server->setSourcePathPrefix('img://'); + $this->assertEquals('img://image.jpg', $this->server->getSourcePath('image.jpg')); + $this->assertEquals('img://path/image.jpg', $this->server->getSourcePath('/path/image.jpg')); + } + + public function testGetSourcePathWithBaseUrlAndPrefix() + { + $this->server->setBaseUrl('base/'); + + $this->server->setSourcePathPrefix('img/'); + $this->assertEquals('img/image.jpg', $this->server->getSourcePath('/base/image.jpg')); + + $this->server->setSourcePathPrefix('img://'); + $this->assertEquals('img://image.jpg', $this->server->getSourcePath('base/image.jpg')); + $this->assertEquals('img://path/image.jpg', $this->server->getSourcePath('/base/path/image.jpg')); } public function testGetSourcePathWithMissingPath() @@ -137,6 +162,15 @@ public function testSetCachePathPrefix() $this->assertEquals('img', $this->server->getCachePathPrefix()); } + public function testSetCachePathPrefixWithMonthIdentifier() + { + $this->server->setCachePathPrefix('img://'); + $this->assertEquals('img:/', $this->server->getCachePathPrefix()); + + $this->server->setCachePathPrefix('img:///'); + $this->assertEquals('img:/', $this->server->getCachePathPrefix()); + } + public function testGetCachePathPrefix() { $this->assertEquals('', $this->server->getCachePathPrefix()); From 5ff3000f131f880ac4ca9adbcb822be131be8d5f Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Tue, 30 Nov 2021 17:56:50 +0100 Subject: [PATCH 2/8] Allow mount identifier into path without set prefix --- src/Server.php | 18 +++++++++++++++--- tests/ServerTest.php | 11 +++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Server.php b/src/Server.php index b3a6534d..db47a977 100644 --- a/src/Server.php +++ b/src/Server.php @@ -122,6 +122,15 @@ private function trimPrefix(string $prefix): string return trim($prefix, '/'); } + private function purgePath(string $path): string + { + if (strpos($path, '://') === false) { + return $path; + } + + return explode('://', $path, 2)[1] ?? ''; + } + /** * Set source file system. * @@ -153,7 +162,7 @@ public function getSource() */ public function setSourcePathPrefix($sourcePathPrefix) { - $this->sourcePathPrefix = $this->trimPrefix($sourcePathPrefix); + $this->sourcePathPrefix = $this->trimPrefix($sourcePathPrefix ?? ''); } /** @@ -265,7 +274,7 @@ public function getCache() */ public function setCachePathPrefix($cachePathPrefix) { - $this->cachePathPrefix = $this->trimPrefix($cachePathPrefix); + $this->cachePathPrefix = $this->trimPrefix($cachePathPrefix ?? ''); } /** @@ -372,10 +381,13 @@ public function getCachePath($path, array $params = []) $md5 = md5($sourcePath.'?'.http_build_query($params)); + if (strpos($sourcePath, '://') !== false) { + $sourcePath = explode('://', $sourcePath, 2)[1] ?? ''; + } $cachedPath = $this->groupCacheInFolders ? $sourcePath.'/'.$md5 : $md5; if ($this->cachePathPrefix) { - $cachedPath = $this->cachePathPrefix.'/'.$cachedPath; + $cachedPath = $this->cachePathPrefix.'/'.$this->purgePath($cachedPath); } if ($this->cacheWithFileExtensions) { diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 6c16d453..28956eff 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -296,6 +296,17 @@ public function testGetCachePathWithExtensionAndPjpgFmFromPreset() $this->assertEquals('image.jpg/ce5cb75f4a37dec0a0a49854e94123eb.jpg', $this->server->getCachePath('image.jpg', ['p' => 'pjpg'])); } + public function testGetCachePathWithMount() + { + $this->assertEquals('image.jpg/76226a1044d9a55855dbb51f98eacc67', $this->server->getCachePath('file://image.jpg', [])); + } + + public function testGetCachePathWithMountAndCachePrefix() + { + $this->server->setCachePathPrefix('cache://'); + $this->assertEquals('cache://image.jpg/76226a1044d9a55855dbb51f98eacc67', $this->server->getCachePath('file://image.jpg', [])); + } + public function testCacheFileExists() { $this->server->setCache(Mockery::mock('League\Flysystem\FilesystemOperator', function ($mock) { From 7babfbb33db2d39d22213fa225ac96386123e1e1 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Tue, 30 Nov 2021 18:04:40 +0100 Subject: [PATCH 3/8] COde style --- src/Server.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Server.php b/src/Server.php index db47a977..bdb42cd6 100644 --- a/src/Server.php +++ b/src/Server.php @@ -115,7 +115,7 @@ private function trimPrefix(string $prefix): string { if ('//' == substr($prefix, -2)) { if (':' == substr(rtrim($prefix, '/'), -1)) { - return rtrim($prefix, '/') . '/'; + return rtrim($prefix, '/').'/'; } } @@ -124,7 +124,7 @@ private function trimPrefix(string $prefix): string private function purgePath(string $path): string { - if (strpos($path, '://') === false) { + if (false === strpos($path, '://')) { return $path; } @@ -381,7 +381,7 @@ public function getCachePath($path, array $params = []) $md5 = md5($sourcePath.'?'.http_build_query($params)); - if (strpos($sourcePath, '://') !== false) { + if (false !== strpos($sourcePath, '://')) { $sourcePath = explode('://', $sourcePath, 2)[1] ?? ''; } $cachedPath = $this->groupCacheInFolders ? $sourcePath.'/'.$md5 : $md5; From 86ad092e2aa215450818858781d6fa12b4ec5960 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Mon, 29 Aug 2022 13:09:37 +0200 Subject: [PATCH 4/8] Improve methods names & PhpDoc --- src/Server.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/Server.php b/src/Server.php index bdb42cd6..06814eb1 100644 --- a/src/Server.php +++ b/src/Server.php @@ -111,7 +111,14 @@ public function __construct(FilesystemOperator $source, FilesystemOperator $cach $this->tempDir = sys_get_temp_dir(); } - private function trimPrefix(string $prefix): string + /** + * Trim path separator from prefix to prevent multiple combined separator. + * + * @param string $prefix + * + * @return string + */ + private function trimPrefixPathSeparator(string $prefix): string { if ('//' == substr($prefix, -2)) { if (':' == substr(rtrim($prefix, '/'), -1)) { @@ -122,7 +129,14 @@ private function trimPrefix(string $prefix): string return trim($prefix, '/'); } - private function purgePath(string $path): string + /** + * Remove filesystem identifier if present. + * + * @param string $path + * + * @return string + */ + private function removeFilesystemIdentifier(string $path): string { if (false === strpos($path, '://')) { return $path; @@ -162,7 +176,7 @@ public function getSource() */ public function setSourcePathPrefix($sourcePathPrefix) { - $this->sourcePathPrefix = $this->trimPrefix($sourcePathPrefix ?? ''); + $this->sourcePathPrefix = $this->trimPrefixPathSeparator($sourcePathPrefix ?? ''); } /** @@ -274,7 +288,7 @@ public function getCache() */ public function setCachePathPrefix($cachePathPrefix) { - $this->cachePathPrefix = $this->trimPrefix($cachePathPrefix ?? ''); + $this->cachePathPrefix = $this->trimPrefixPathSeparator($cachePathPrefix ?? ''); } /** @@ -387,7 +401,7 @@ public function getCachePath($path, array $params = []) $cachedPath = $this->groupCacheInFolders ? $sourcePath.'/'.$md5 : $md5; if ($this->cachePathPrefix) { - $cachedPath = $this->cachePathPrefix.'/'.$this->purgePath($cachedPath); + $cachedPath = $this->cachePathPrefix.'/'.$this->removeFilesystemIdentifier($cachedPath); } if ($this->cacheWithFileExtensions) { From 4460bf7a2e85c1cb8ffc06f757f86a8a8961fff8 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Thu, 19 Feb 2026 10:35:17 +0100 Subject: [PATCH 5/8] Fix tests --- tests/ServerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 20cdcc79..01027ed7 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -352,13 +352,13 @@ public function testGetCachePathWithExtensionAndPjpgFmFromPreset(): void public function testGetCachePathWithMount(): void { - $this->assertEquals('image.jpg/76226a1044d9a55855dbb51f98eacc67', $this->server->getCachePath('file://image.jpg', [])); + $this->assertEquals('image.jpg/7116fbe362f62057', $this->server->getCachePath('file://image.jpg', [])); } public function testGetCachePathWithMountAndCachePrefix(): void { $this->server->setCachePathPrefix('cache://'); - $this->assertEquals('cache://image.jpg/76226a1044d9a55855dbb51f98eacc67', $this->server->getCachePath('file://image.jpg', [])); + $this->assertEquals('cache://image.jpg/7116fbe362f62057', $this->server->getCachePath('file://image.jpg', [])); } public function testCacheFileExists(): void From 4347028ac0cd3c6c98fe170f88547a62e15b76b3 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Thu, 19 Feb 2026 10:44:37 +0100 Subject: [PATCH 6/8] Modernize pr code --- src/Server.php | 10 ++++------ tests/ServerTest.php | 6 ------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Server.php b/src/Server.php index 787eecfe..2ec4c70b 100644 --- a/src/Server.php +++ b/src/Server.php @@ -102,10 +102,8 @@ public function __construct(FilesystemOperator $source, FilesystemOperator $cach */ private function trimPrefixPathSeparator(string $prefix): string { - if ('//' == substr($prefix, -2)) { - if (':' == substr(rtrim($prefix, '/'), -1)) { - return rtrim($prefix, '/').'/'; - } + if (str_ends_with($prefix, '://')) { + return rtrim($prefix, '/').'/'; } return trim($prefix, '/'); @@ -120,7 +118,7 @@ private function trimPrefixPathSeparator(string $prefix): string */ private function removeFilesystemIdentifier(string $path): string { - if (false === strpos($path, '://')) { + if (!str_contains($path, '://')) { return $path; } @@ -154,7 +152,7 @@ public function getSource(): FilesystemOperator */ public function setSourcePathPrefix(string $sourcePathPrefix): void { - $this->sourcePathPrefix = $this->trimPrefixPathSeparator($sourcePathPrefix ?? ''); + $this->sourcePathPrefix = $this->trimPrefixPathSeparator($sourcePathPrefix); } /** diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 01027ed7..b7986255 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -63,9 +63,6 @@ public function testSetSourcePathPrefixWithMonthIdentifier(): void { $this->server->setSourcePathPrefix('img://'); $this->assertEquals('img:/', $this->server->getSourcePathPrefix()); - - $this->server->setSourcePathPrefix('img:///'); - $this->assertEquals('img:/', $this->server->getSourcePathPrefix()); } public function testGetSourcePathPrefix(): void @@ -164,9 +161,6 @@ public function testSetCachePathPrefixWithMonthIdentifier(): void { $this->server->setCachePathPrefix('img://'); $this->assertEquals('img:/', $this->server->getCachePathPrefix()); - - $this->server->setCachePathPrefix('img:///'); - $this->assertEquals('img:/', $this->server->getCachePathPrefix()); } public function testGetCachePathPrefix(): void From c3e87e4a1d3bc68fe8df4045dded25be67dddae1 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Thu, 19 Feb 2026 10:50:43 +0100 Subject: [PATCH 7/8] Fix code style and phpstan report --- src/Server.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/Server.php b/src/Server.php index 2ec4c70b..0f75497d 100644 --- a/src/Server.php +++ b/src/Server.php @@ -95,10 +95,6 @@ public function __construct(FilesystemOperator $source, FilesystemOperator $cach /** * Trim path separator from prefix to prevent multiple combined separator. - * - * @param string $prefix - * - * @return string */ private function trimPrefixPathSeparator(string $prefix): string { @@ -111,10 +107,6 @@ private function trimPrefixPathSeparator(string $prefix): string /** * Remove filesystem identifier if present. - * - * @param string $path - * - * @return string */ private function removeFilesystemIdentifier(string $path): string { @@ -260,7 +252,7 @@ public function getCache(): FilesystemOperator */ public function setCachePathPrefix(string $cachePathPrefix): void { - $this->cachePathPrefix = $this->trimPrefixPathSeparator($cachePathPrefix ?? ''); + $this->cachePathPrefix = $this->trimPrefixPathSeparator($cachePathPrefix); } /** From 47e3af96bafd8ae40ad70136cfe0f6840779dc91 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Thu, 19 Feb 2026 12:45:32 +0100 Subject: [PATCH 8/8] Micro optimization --- src/Server.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Server.php b/src/Server.php index 0f75497d..d22a358e 100644 --- a/src/Server.php +++ b/src/Server.php @@ -110,11 +110,11 @@ private function trimPrefixPathSeparator(string $prefix): string */ private function removeFilesystemIdentifier(string $path): string { - if (!str_contains($path, '://')) { + if (false === ($identifierPos = strpos($path, '://'))) { return $path; } - return explode('://', $path, 2)[1] ?? ''; + return substr($path, $identifierPos + 3); } /** @@ -381,8 +381,8 @@ public function getCachePath(string $path, array $params = []): string $cachedPath = hash('xxh3', $sourcePath.'?'.http_build_query($params)); - if (false !== strpos($sourcePath, '://')) { - $sourcePath = explode('://', $sourcePath, 2)[1] ?? ''; + if (false !== ($identifierPos = strpos($sourcePath, '://'))) { + $sourcePath = substr($sourcePath, $identifierPos + 3); } if ($this->groupCacheInFolders) {