Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public function directoryExists(string $location): bool
return $this->adapter->directoryExists($this->pathNormalizer->normalizePath($location));
}

public function directoryIsEmpty(string $location): bool
{
foreach ($this->listContents($location) as $item) {
return false;
}

return true;
}

public function has(string $location): bool
{
$path = $this->pathNormalizer->normalizePath($location);
Expand Down
15 changes: 15 additions & 0 deletions src/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ public function checking_if_directories_exist(): void
$this->assertFalse($notExistingDirectory);
}

/**
* @test
*/
public function checking_if_directories_are_empty(): void
{
$this->filesystem->createDirectory('empty-directory');
$this->filesystem->write('filled-directory/file.txt', 'contents');
$this->filesystem->createDirectory('directory-with-subdirectory/nested');

$this->assertTrue($this->filesystem->directoryIsEmpty('empty-directory'));
$this->assertFalse($this->filesystem->directoryIsEmpty('filled-directory'));
$this->assertFalse($this->filesystem->directoryIsEmpty('directory-with-subdirectory'));
$this->assertTrue($this->filesystem->directoryIsEmpty('not-existing-directory'));
}

/**
* @test
*/
Expand Down
12 changes: 12 additions & 0 deletions src/MountManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ public function directoryExists(string $location): bool
}
}

public function directoryIsEmpty(string $location): bool
{
/** @var FilesystemOperator $filesystem */
[$filesystem, $path] = $this->determineFilesystemAndPath($location);

foreach ($filesystem->listContents($path) as $item) {
return false;
}

return true;
}

public function read(string $location): string
{
/** @var FilesystemOperator $filesystem */
Expand Down
13 changes: 13 additions & 0 deletions src/MountManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,19 @@ public function checking_existence_for_an_existing_directory(): void
$this->assertTrue($existence);
}

/**
* @test
*/
public function checking_if_directories_are_empty(): void
{
$this->secondFilesystem->createDirectory('empty');
$this->secondFilesystem->write('nested/location.txt', 'contents');

$this->assertTrue($this->mountManager->directoryIsEmpty('second://empty'));
$this->assertFalse($this->mountManager->directoryIsEmpty('second://nested'));
$this->assertTrue($this->mountManager->directoryIsEmpty('second://not-existing'));
}

/**
* @test
*/
Expand Down