Skip to content

Commit

Permalink
Make in-memory listings more useful.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Aug 14, 2020
1 parent 5f601f6 commit 1b4dddd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/InMemory/InMemoryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function setVisibility(string $visibility): void
$this->visibility = $visibility;
}

public function visibility(): string
public function visibility(): ?string
{
return $this->visibility;
}
Expand Down
5 changes: 3 additions & 2 deletions src/InMemory/InMemoryFilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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);
Expand Down Expand Up @@ -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());
}
}
}
Expand Down
20 changes: 15 additions & 5 deletions src/InMemory/InMemoryFilesystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()]);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/InMemory/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"require": {
"php": "^7.2",
"ext-fileinfo": "*",
"league/flysystem": "^2.0.0-beta.1"
},
"license": "MIT",
Expand Down

0 comments on commit 1b4dddd

Please sign in to comment.