From 03ea095938085658ab49b4f7e34765c442ba7491 Mon Sep 17 00:00:00 2001 From: Gijsdev <38570931+gijsdev@users.noreply.github.com> Date: Wed, 13 Oct 2021 23:04:20 +0200 Subject: [PATCH 1/3] Update filesystem adapter to Flysystem v2 --- .../Filesystem/FilesystemCachePool.php | 68 ++++++++++++------- .../Filesystem/Tests/CreatePoolTrait.php | 7 +- .../Tests/FilesystemCachePoolTest.php | 6 +- src/Adapter/Filesystem/composer.json | 2 +- 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/src/Adapter/Filesystem/FilesystemCachePool.php b/src/Adapter/Filesystem/FilesystemCachePool.php index 065519d1..6af2e9c2 100644 --- a/src/Adapter/Filesystem/FilesystemCachePool.php +++ b/src/Adapter/Filesystem/FilesystemCachePool.php @@ -14,9 +14,12 @@ use Cache\Adapter\Common\AbstractCachePool; use Cache\Adapter\Common\Exception\InvalidArgumentException; use Cache\Adapter\Common\PhpCacheItem; -use League\Flysystem\FileExistsException; -use League\Flysystem\FileNotFoundException; -use League\Flysystem\FilesystemInterface; +use League\Flysystem\FilesystemOperator; +use League\Flysystem\UnableToCreateDirectory; +use League\Flysystem\UnableToDeleteDirectory; +use League\Flysystem\UnableToDeleteFile; +use League\Flysystem\UnableToReadFile; +use League\Flysystem\UnableToWriteFile; /** * @author Tobias Nyholm @@ -24,7 +27,7 @@ class FilesystemCachePool extends AbstractCachePool { /** - * @type FilesystemInterface + * @type FilesystemOperator */ private $filesystem; @@ -36,15 +39,15 @@ class FilesystemCachePool extends AbstractCachePool private $folder; /** - * @param FilesystemInterface $filesystem + * @param FilesystemOperator $filesystem * @param string $folder */ - public function __construct(FilesystemInterface $filesystem, $folder = 'cache') + public function __construct(FilesystemOperator $filesystem, $folder = 'cache') { $this->folder = $folder; $this->filesystem = $filesystem; - $this->filesystem->createDir($this->folder); + $this->filesystem->createDirectory($this->folder); } /** @@ -68,7 +71,7 @@ protected function fetchObjectFromCache($key) if ($data === false) { return $empty; } - } catch (FileNotFoundException $e) { + } catch (UnableToReadFile $e) { return $empty; } @@ -91,8 +94,17 @@ protected function fetchObjectFromCache($key) */ protected function clearAllObjectsFromCache() { - $this->filesystem->deleteDir($this->folder); - $this->filesystem->createDir($this->folder); + try { + $this->filesystem->deleteDirectory($this->folder); + } catch(UnableToDeleteDirectory $e) { + return false; + } + + try { + $this->filesystem->createDirectory($this->folder); + } catch(UnableToCreateDirectory $e) { + return false; + } return true; } @@ -119,16 +131,11 @@ protected function storeItemInCache(PhpCacheItem $item, $ttl) ); $file = $this->getFilePath($item->getKey()); - if ($this->filesystem->has($file)) { - // Update file if it exists - return $this->filesystem->update($file, $data); - } - try { - return $this->filesystem->write($file, $data); - } catch (FileExistsException $e) { - // To handle issues when/if race conditions occurs, we try to update here. - return $this->filesystem->update($file, $data); + $this->filesystem->write($file, $data); + return true; + } catch (UnableToWriteFile $e) { + return false; } } @@ -155,7 +162,7 @@ protected function getList($name) { $file = $this->getFilePath($name); - if (!$this->filesystem->has($file)) { + if (!$this->filesystem->fileExists($file)) { $this->filesystem->write($file, serialize([])); } @@ -179,7 +186,13 @@ protected function appendListItem($name, $key) $list = $this->getList($name); $list[] = $key; - return $this->filesystem->update($this->getFilePath($name), serialize($list)); + try { + $this->filesystem->write($this->getFilePath($name), serialize($list)); + return true; + } catch (UnableToWriteFile $e) { + return false; + } + } /** @@ -193,8 +206,12 @@ protected function removeListItem($name, $key) unset($list[$i]); } } - - return $this->filesystem->update($this->getFilePath($name), serialize($list)); + try { + $this->filesystem->write($this->getFilePath($name), serialize($list)); + return true; + } catch (UnableToWriteFile $e) { + return false; + } } /** @@ -205,9 +222,10 @@ protected function removeListItem($name, $key) private function forceClear($key) { try { - return $this->filesystem->delete($this->getFilePath($key)); - } catch (FileNotFoundException $e) { + $this->filesystem->delete($this->getFilePath($key)); return true; + } catch (UnableToDeleteFile $e) { + return false; } } } diff --git a/src/Adapter/Filesystem/Tests/CreatePoolTrait.php b/src/Adapter/Filesystem/Tests/CreatePoolTrait.php index 246e921a..2044be56 100644 --- a/src/Adapter/Filesystem/Tests/CreatePoolTrait.php +++ b/src/Adapter/Filesystem/Tests/CreatePoolTrait.php @@ -12,13 +12,14 @@ namespace Cache\Adapter\Filesystem\Tests; use Cache\Adapter\Filesystem\FilesystemCachePool; -use League\Flysystem\Adapter\Local; use League\Flysystem\Filesystem; +use League\Flysystem\FilesystemOperator; +use League\Flysystem\Local\LocalFilesystemAdapter; trait CreatePoolTrait { /** - * @type Filesystem + * @type FilesystemOperator */ private $filesystem; @@ -35,7 +36,7 @@ public function createSimpleCache() private function getFilesystem() { if ($this->filesystem === null) { - $this->filesystem = new Filesystem(new Local(__DIR__.'/cache'.rand(1, 100000))); + $this->filesystem = new Filesystem(new LocalFilesystemAdapter(__DIR__.'/cache'.rand(1, 100000))); } return $this->filesystem; diff --git a/src/Adapter/Filesystem/Tests/FilesystemCachePoolTest.php b/src/Adapter/Filesystem/Tests/FilesystemCachePoolTest.php index 4cadacf0..ca43a033 100644 --- a/src/Adapter/Filesystem/Tests/FilesystemCachePoolTest.php +++ b/src/Adapter/Filesystem/Tests/FilesystemCachePoolTest.php @@ -38,13 +38,13 @@ public function testCleanupOnExpire() $item->set('data'); $item->expiresAt(new \DateTime('now')); $pool->save($item); - $this->assertTrue($this->getFilesystem()->has('cache/test_ttl_null')); + $this->assertTrue($this->getFilesystem()->fileExists('cache/test_ttl_null')); sleep(1); $item = $pool->getItem('test_ttl_null'); $this->assertFalse($item->isHit()); - $this->assertFalse($this->getFilesystem()->has('cache/test_ttl_null')); + $this->assertFalse($this->getFilesystem()->fileExists('cache/test_ttl_null')); } public function testChangeFolder() @@ -53,7 +53,7 @@ public function testChangeFolder() $pool->setFolder('foobar'); $pool->save($pool->getItem('test_path')); - $this->assertTrue($this->getFilesystem()->has('foobar/test_path')); + $this->assertTrue($this->getFilesystem()->fileExists('foobar/test_path')); } public function testCorruptedCacheFileHandledNicely() diff --git a/src/Adapter/Filesystem/composer.json b/src/Adapter/Filesystem/composer.json index d2f794d8..b4f587d2 100644 --- a/src/Adapter/Filesystem/composer.json +++ b/src/Adapter/Filesystem/composer.json @@ -25,7 +25,7 @@ "require": { "php": ">=7.4", "cache/adapter-common": "^1.0", - "league/flysystem": "^1.0", + "league/flysystem": "^2.0 || ^3.0", "psr/cache": "^1.0 || ^2.0", "psr/simple-cache": "^1.0" }, From cedcf806d06617abcd6c5514c76166d100b8d683 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Thu, 24 Feb 2022 12:42:22 +0100 Subject: [PATCH 2/3] CS Fix --- src/Adapter/Filesystem/FilesystemCachePool.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Adapter/Filesystem/FilesystemCachePool.php b/src/Adapter/Filesystem/FilesystemCachePool.php index 6af2e9c2..d86711cb 100644 --- a/src/Adapter/Filesystem/FilesystemCachePool.php +++ b/src/Adapter/Filesystem/FilesystemCachePool.php @@ -40,7 +40,7 @@ class FilesystemCachePool extends AbstractCachePool /** * @param FilesystemOperator $filesystem - * @param string $folder + * @param string $folder */ public function __construct(FilesystemOperator $filesystem, $folder = 'cache') { @@ -96,13 +96,13 @@ protected function clearAllObjectsFromCache() { try { $this->filesystem->deleteDirectory($this->folder); - } catch(UnableToDeleteDirectory $e) { + } catch (UnableToDeleteDirectory $e) { return false; } try { $this->filesystem->createDirectory($this->folder); - } catch(UnableToCreateDirectory $e) { + } catch (UnableToCreateDirectory $e) { return false; } @@ -131,8 +131,10 @@ protected function storeItemInCache(PhpCacheItem $item, $ttl) ); $file = $this->getFilePath($item->getKey()); + try { $this->filesystem->write($file, $data); + return true; } catch (UnableToWriteFile $e) { return false; @@ -188,11 +190,11 @@ protected function appendListItem($name, $key) try { $this->filesystem->write($this->getFilePath($name), serialize($list)); + return true; } catch (UnableToWriteFile $e) { return false; } - } /** @@ -206,8 +208,10 @@ protected function removeListItem($name, $key) unset($list[$i]); } } + try { $this->filesystem->write($this->getFilePath($name), serialize($list)); + return true; } catch (UnableToWriteFile $e) { return false; @@ -223,6 +227,7 @@ private function forceClear($key) { try { $this->filesystem->delete($this->getFilePath($key)); + return true; } catch (UnableToDeleteFile $e) { return false; From 1f2324bff4082537c2ad8bc241dbf5e9177936e0 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Thu, 24 Feb 2022 12:44:44 +0100 Subject: [PATCH 3/3] Update global composer.json as well --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 858c316f..a86fef5d 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "require": { "php": ">=7.4", "doctrine/cache": "^1.3", - "league/flysystem": "^1.0", + "league/flysystem": "^2.0 || ^3.0", "psr/cache": "^1.0 || ^2.0", "psr/log": "^1.0 || ^2.0 || ^3.0", "psr/simple-cache": "^1.0"