From 1b4dddd91b83388a775ab22b8af7876801645a4c Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Fri, 14 Aug 2020 13:59:40 +0200 Subject: [PATCH] Make in-memory listings more useful. --- src/InMemory/InMemoryFile.php | 2 +- src/InMemory/InMemoryFilesystemAdapter.php | 5 +++-- .../InMemoryFilesystemAdapterTest.php | 20 ++++++++++++++----- src/InMemory/composer.json | 1 + 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/InMemory/InMemoryFile.php b/src/InMemory/InMemoryFile.php index b35b5bf9c..3c9c2095d 100644 --- a/src/InMemory/InMemoryFile.php +++ b/src/InMemory/InMemoryFile.php @@ -71,7 +71,7 @@ public function setVisibility(string $visibility): void $this->visibility = $visibility; } - public function visibility(): string + public function visibility(): ?string { return $this->visibility; } diff --git a/src/InMemory/InMemoryFilesystemAdapter.php b/src/InMemory/InMemoryFilesystemAdapter.php index 1c78d5cf0..bdc76c4d1 100644 --- a/src/InMemory/InMemoryFilesystemAdapter.php +++ b/src/InMemory/InMemoryFilesystemAdapter.php @@ -136,6 +136,7 @@ public function lastModified(string $path): FileAttributes public function fileSize(string $path): FileAttributes { $path = $this->preparePath($path); + if (array_key_exists($path, $this->files) === false) { throw UnableToRetrieveMetadata::fileSize($path, 'file does not exist'); } @@ -149,7 +150,7 @@ public function listContents(string $prefix, bool $deep): iterable $prefixLength = strlen($prefix); $listedDirectories = []; - foreach (array_keys($this->files) as $path) { + foreach ($this->files as $path => $file) { if (substr($path, 0, $prefixLength) === $prefix) { $subPath = substr($path, $prefixLength); $dirname = dirname($subPath); @@ -178,7 +179,7 @@ public function listContents(string $prefix, bool $deep): iterable } if ($deep === true || strpos($subPath, '/') === false) { - yield new FileAttributes(ltrim($path, '/')); + yield new FileAttributes(ltrim($path, '/'), $file->fileSize(), $file->visibility(), $file->lastModified(), $file->mimeType()); } } } diff --git a/src/InMemory/InMemoryFilesystemAdapterTest.php b/src/InMemory/InMemoryFilesystemAdapterTest.php index 6d4e24b3c..847a44fd3 100644 --- a/src/InMemory/InMemoryFilesystemAdapterTest.php +++ b/src/InMemory/InMemoryFilesystemAdapterTest.php @@ -9,6 +9,7 @@ use League\Flysystem\DirectoryAttributes; use League\Flysystem\FileAttributes; use League\Flysystem\FilesystemAdapter; +use League\Flysystem\StorageAttributes; use League\Flysystem\UnableToCopyFile; use League\Flysystem\UnableToMoveFile; use League\Flysystem\UnableToReadFile; @@ -145,13 +146,22 @@ public function listing_all_files(): void $adapter->write('path.txt', 'contents', new Config()); $adapter->write('a/path.txt', 'contents', new Config()); $adapter->write('a/b/path.txt', 'contents', new Config()); + /** @var StorageAttributes[] $listing */ $listing = iterator_to_array($adapter->listContents('/', true)); $this->assertCount(5, $listing); - $this->assertContainsEquals(new FileAttributes('path.txt'), $listing); - $this->assertContainsEquals(new FileAttributes('a/path.txt'), $listing); - $this->assertContainsEquals(new FileAttributes('a/b/path.txt'), $listing); - $this->assertContainsEquals(new DirectoryAttributes('a'), $listing); - $this->assertContainsEquals(new DirectoryAttributes('a/b'), $listing); + + $expected = [ + 'path.txt' => StorageAttributes::TYPE_FILE, + 'a/path.txt' => StorageAttributes::TYPE_FILE, + 'a/b/path.txt' => StorageAttributes::TYPE_FILE, + 'a' => StorageAttributes::TYPE_DIRECTORY, + 'a/b' => StorageAttributes::TYPE_DIRECTORY, + ]; + + foreach ($listing as $item) { + $this->assertArrayHasKey($item->path(), $expected); + $this->assertEquals($item->type(), $expected[$item->path()]); + } } /** diff --git a/src/InMemory/composer.json b/src/InMemory/composer.json index c43b80da8..3cfcd5ffc 100644 --- a/src/InMemory/composer.json +++ b/src/InMemory/composer.json @@ -12,6 +12,7 @@ }, "require": { "php": "^7.2", + "ext-fileinfo": "*", "league/flysystem": "^2.0.0-beta.1" }, "license": "MIT",