diff --git a/FilesystemCachePool.php b/FilesystemCachePool.php index 065519d..6ac8c58 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 @@ -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,11 +150,11 @@ 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([])); } @@ -164,6 +164,7 @@ protected function getList($name) /** * {@inheritdoc} + * @throws \League\Flysystem\FilesystemException */ protected function removeList($name) { @@ -173,19 +174,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 +202,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 +215,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..0ea780f 100644 --- a/composer.json +++ b/composer.json @@ -25,17 +25,17 @@ "require": { "php": ">=7.4", "cache/adapter-common": "^1.0", - "league/flysystem": "^1.0", - "psr/cache": "^1.0 || ^2.0", - "psr/simple-cache": "^1.0" + "league/flysystem": "^3.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,