From 02285b661eaeee3fd5a4bbfd28f7858ea4941e80 Mon Sep 17 00:00:00 2001
From: Bence Urszin <Bence.Urszin@kuratlegroup.com>
Date: Thu, 23 Jun 2022 14:25:33 +0200
Subject: [PATCH 1/3] Laravel 9 support

---
 FilesystemCachePool.php | 99 +++++++++++++++++++++++------------------
 composer.json           |  2 +-
 2 files changed, 57 insertions(+), 44 deletions(-)

diff --git a/FilesystemCachePool.php b/FilesystemCachePool.php
index 065519d..2bf39b3 100644
--- a/FilesystemCachePool.php
+++ b/FilesystemCachePool.php
@@ -14,9 +14,9 @@
 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\Filesystem;
+use League\Flysystem\FilesystemException;
+use League\Flysystem\UnableToDeleteFile;
 
 /**
  * @author Tobias Nyholm <tobias.nyholm@gmail.com>
@@ -24,51 +24,54 @@
 class FilesystemCachePool extends AbstractCachePool
 {
     /**
-     * @type FilesystemInterface
+     * @type Filesystem
      */
-    private $filesystem;
+    private Filesystem $filesystem;
 
     /**
      * The folder should not begin nor end with a slash. Example: path/to/cache.
      *
      * @type string
      */
-    private $folder;
+    private string $folder;
 
     /**
-     * @param FilesystemInterface $filesystem
-     * @param string              $folder
+     * @param Filesystem $filesystem
+     * @param string $folder
+     *
+     * @throws \League\Flysystem\FilesystemException
      */
-    public function __construct(FilesystemInterface $filesystem, $folder = 'cache')
+    public function __construct(Filesystem $filesystem, string $folder = 'cache')
     {
         $this->folder = $folder;
 
         $this->filesystem = $filesystem;
-        $this->filesystem->createDir($this->folder);
+        $this->filesystem->createDirectory($this->folder, []);
     }
 
     /**
      * @param string $folder
      */
-    public function setFolder($folder)
+    public function setFolder(string $folder)
     {
         $this->folder = $folder;
     }
 
     /**
      * {@inheritdoc}
+     * @throws \League\Flysystem\FilesystemException
      */
-    protected function fetchObjectFromCache($key)
+    protected function fetchObjectFromCache($key): array
     {
         $empty = [false, null, [], null];
-        $file  = $this->getFilePath($key);
+        $file = $this->getFilePath($key);
 
         try {
             $data = @unserialize($this->filesystem->read($file));
             if ($data === false) {
                 return $empty;
             }
-        } catch (FileNotFoundException $e) {
+        } catch (FilesystemException $e) {
             return $empty;
         }
 
@@ -88,11 +91,12 @@ protected function fetchObjectFromCache($key)
 
     /**
      * {@inheritdoc}
+     * @throws \League\Flysystem\FilesystemException
      */
-    protected function clearAllObjectsFromCache()
+    protected function clearAllObjectsFromCache(): bool
     {
-        $this->filesystem->deleteDir($this->folder);
-        $this->filesystem->createDir($this->folder);
+        $this->filesystem->deleteDirectory($this->folder);
+        $this->filesystem->createDirectory($this->folder);
 
         return true;
     }
@@ -100,7 +104,7 @@ protected function clearAllObjectsFromCache()
     /**
      * {@inheritdoc}
      */
-    protected function clearOneObjectFromCache($key)
+    protected function clearOneObjectFromCache($key): bool
     {
         return $this->forceClear($key);
     }
@@ -108,7 +112,7 @@ protected function clearOneObjectFromCache($key)
     /**
      * {@inheritdoc}
      */
-    protected function storeItemInCache(PhpCacheItem $item, $ttl)
+    protected function storeItemInCache(PhpCacheItem $item, $ttl): bool
     {
         $data = serialize(
             [
@@ -119,29 +123,25 @@ 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 (FilesystemException $e) {
+            return false;
         }
     }
 
     /**
      * @param string $key
      *
+     * @return string
      * @throws InvalidArgumentException
      *
-     * @return string
      */
-    private function getFilePath($key)
+    private function getFilePath(string $key): string
     {
-        if (!preg_match('|^[a-zA-Z0-9_\.! ]+$|', $key)) {
+        if (! preg_match('|^[a-zA-Z0-9_\.! ]+$|', $key)) {
             throw new InvalidArgumentException(sprintf('Invalid key "%s". Valid filenames must match [a-zA-Z0-9_\.! ].', $key));
         }
 
@@ -150,20 +150,19 @@ private function getFilePath($key)
 
     /**
      * {@inheritdoc}
+     * @throws \League\Flysystem\FilesystemException
      */
     protected function getList($name)
     {
         $file = $this->getFilePath($name);
-
-        if (!$this->filesystem->has($file)) {
-            $this->filesystem->write($file, serialize([]));
-        }
+        $this->filesystem->write($file, serialize([]));
 
         return unserialize($this->filesystem->read($file));
     }
 
     /**
      * {@inheritdoc}
+     * @throws \League\Flysystem\FilesystemException
      */
     protected function removeList($name)
     {
@@ -173,19 +172,26 @@ protected function removeList($name)
 
     /**
      * {@inheritdoc}
+     * @throws \League\Flysystem\FilesystemException
      */
-    protected function appendListItem($name, $key)
+    protected function appendListItem($name, $key): bool
     {
-        $list   = $this->getList($name);
+        $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 (FilesystemException $e) {
+            return false;
+        }
     }
 
     /**
      * {@inheritdoc}
+     * @throws \League\Flysystem\FilesystemException
      */
-    protected function removeListItem($name, $key)
+    protected function removeListItem($name, $key): bool
     {
         $list = $this->getList($name);
         foreach ($list as $i => $item) {
@@ -194,7 +200,12 @@ 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 (FilesystemException $e) {
+            return false;
+        }
     }
 
     /**
@@ -202,12 +213,14 @@ protected function removeListItem($name, $key)
      *
      * @return bool
      */
-    private function forceClear($key)
+    private function forceClear($key): bool
     {
         try {
-            return $this->filesystem->delete($this->getFilePath($key));
-        } catch (FileNotFoundException $e) {
+            $this->filesystem->delete($this->getFilePath($key));
+
             return true;
+        } catch (FilesystemException $e) {
+            return false;
         }
     }
 }
diff --git a/composer.json b/composer.json
index d2f794d..b6bdad3 100644
--- a/composer.json
+++ b/composer.json
@@ -25,7 +25,7 @@
     "require": {
         "php": ">=7.4",
         "cache/adapter-common": "^1.0",
-        "league/flysystem": "^1.0",
+        "league/flysystem": "^3.0",
         "psr/cache": "^1.0 || ^2.0",
         "psr/simple-cache": "^1.0"
     },

From 567a1b2558725168fdcb8d9a00ca4e73f4b63eae Mon Sep 17 00:00:00 2001
From: Alexander Filippov <swayalex@gmail.com>
Date: Wed, 20 Jul 2022 19:21:28 +0300
Subject: [PATCH 2/3] Fixed error in getList method

---
 FilesystemCachePool.php | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/FilesystemCachePool.php b/FilesystemCachePool.php
index 2bf39b3..6ac8c58 100644
--- a/FilesystemCachePool.php
+++ b/FilesystemCachePool.php
@@ -155,7 +155,9 @@ private function getFilePath(string $key): string
     protected function getList($name)
     {
         $file = $this->getFilePath($name);
-        $this->filesystem->write($file, serialize([]));
+        if (!$this->filesystem->has($file)) {
+            $this->filesystem->write($file, serialize([]));
+        }
 
         return unserialize($this->filesystem->read($file));
     }

From a5c16fe291489cd6c34c7604036e28a2a761d14a Mon Sep 17 00:00:00 2001
From: Alexander Filippov <swayalex@gmail.com>
Date: Fri, 22 Jul 2022 23:18:32 +0300
Subject: [PATCH 3/3] updated possible versions of psr/cache and
 psr/simple-cache packages

---
 composer.json | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/composer.json b/composer.json
index b6bdad3..0ea780f 100644
--- a/composer.json
+++ b/composer.json
@@ -26,16 +26,16 @@
         "php": ">=7.4",
         "cache/adapter-common": "^1.0",
         "league/flysystem": "^3.0",
-        "psr/cache": "^1.0 || ^2.0",
-        "psr/simple-cache": "^1.0"
+        "psr/cache": "^1.0 || ^2.0 || ^3.0",
+        "psr/simple-cache": "^1.0 || ^2.0 || ^3.0"
     },
     "require-dev": {
         "cache/integration-tests": "^0.17",
         "phpunit/phpunit": "^7.5.20 || ^9.5.10"
     },
     "provide": {
-        "psr/cache-implementation": "^1.0",
-        "psr/simple-cache-implementation": "^1.0"
+        "psr/cache-implementation": "^1.0 || ^2.0 || ^3.0",
+        "psr/simple-cache-implementation": "^1.0 || ^2.0 || ^3.0"
     },
     "minimum-stability": "dev",
     "prefer-stable": true,