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" diff --git a/src/Adapter/Filesystem/FilesystemCachePool.php b/src/Adapter/Filesystem/FilesystemCachePool.php index 065519d1..d86711cb 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 string $folder + * @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,13 @@ 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 +164,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 +188,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; + } } /** @@ -194,7 +209,13 @@ protected function removeListItem($name, $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; + } } /** @@ -205,9 +226,11 @@ 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" },