Skip to content

Commit

Permalink
FallbackAdapter::fileExists() and `FallbackAdapter::directoryExists…
Browse files Browse the repository at this point in the history
…()` now try next adapter in cas of FALSE result
  • Loading branch information
ElGigi committed Mar 20, 2024
1 parent 125edf6 commit cced38d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 11 additions & 1 deletion src/FallbackAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/FallbackAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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'));
}
}

0 comments on commit cced38d

Please sign in to comment.