From cced38d896480611ae76fb5fa82f61c91a218899 Mon Sep 17 00:00:00 2001 From: Ronan Giron Date: Wed, 20 Mar 2024 15:16:33 +0100 Subject: [PATCH] `FallbackAdapter::fileExists()` and `FallbackAdapter::directoryExists()` now try next adapter in cas of FALSE result --- CHANGELOG.md | 6 ++++++ src/FallbackAdapter.php | 12 +++++++++++- tests/FallbackAdapterTest.php | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e00aaaa..90708ff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file. This projec to [Semantic Versioning] (http://semver.org/). For change log format, use [Keep a Changelog] (http://keepachangelog.com/). +## [1.0.1] - 2024-03-20 + +### Fixed + +- `FallbackAdapter::fileExists()` and `FallbackAdapter::directoryExists()` now try next adapter in cas of FALSE result + ## [1.0.0] - 2024-03-14 Initial development diff --git a/src/FallbackAdapter.php b/src/FallbackAdapter.php index 331c7ef..34fb41b 100644 --- a/src/FallbackAdapter.php +++ b/src/FallbackAdapter.php @@ -35,10 +35,20 @@ public function __construct(FilesystemAdapter $adapter, FilesystemAdapter ...$fa protected function callAdapter(string $method, array $args, ?Closure $callback = null): mixed { $adapterException = null; + $nbAdapters = count($this->adapters); + $count = 0; foreach ($this->adapters as $adapter) { + $count++; + try { - return $adapter->{$method}(...$args); + $result = $adapter->{$method}(...$args); + + if (false === $result && $count < $nbAdapters) { + continue; + } + + return $result; } catch (Throwable $exception) { $adapterException = $exception; } diff --git a/tests/FallbackAdapterTest.php b/tests/FallbackAdapterTest.php index 5fb7a49..a6e6009 100644 --- a/tests/FallbackAdapterTest.php +++ b/tests/FallbackAdapterTest.php @@ -12,6 +12,7 @@ use ElGigi\FlysystemUsefulAdapters\FallbackAdapter; use League\Flysystem\AdapterTestUtilities\FilesystemAdapterTestCase; +use League\Flysystem\Config; use League\Flysystem\FilesystemAdapter; use League\Flysystem\InMemory\InMemoryFilesystemAdapter; use PHPUnit\Framework\TestCase; @@ -26,4 +27,18 @@ protected static function createFilesystemAdapter(): FilesystemAdapter new InMemoryFilesystemAdapter(), ); } + + public function testFileExists() + { + $fallback = new FallbackAdapter( + $adapter1 = new InMemoryFilesystemAdapter(), + $adapter2 = new InMemoryFilesystemAdapter(), + ); + + $adapter1->write('foo/baz', 'test', new Config()); + $adapter2->write('foo/bar', 'test', new Config()); + + $this->assertTrue($fallback->fileExists('foo/baz')); + $this->assertTrue($fallback->fileExists('foo/bar')); + } }