Skip to content

Commit

Permalink
[9.x] Adds isDirectoryEmpty() and isDirectoryNotEmpty() to Filesy…
Browse files Browse the repository at this point in the history
…stem (#42559)

* Adds `hasFiles` and `hasNoFiles` to Filesystem

* Adds methods to facade.

* Adds some additional test methods.

* Better method name.

* Fixes returning `true` if the path is a file.

* Fixes empty directories without sub-directories.

* Refactor as there is already an invalid dir exception.

* formatting

* fix test

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
DarkGhostHunter and taylorotwell authored May 31, 2022
1 parent 6e87a42 commit 77ceefe
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/Illuminate/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,18 @@ public function isDirectory($directory)
return is_dir($directory);
}

/**
* Determine if the given path is a directory that does not contain any other files or directories.
*
* @param string $directory
* @param bool $ignoreDotFiles
* @return bool
*/
public function isEmptyDirectory($directory, $ignoreDotFiles = false)
{
return ! Finder::create()->ignoreDotFiles($ignoreDotFiles)->in($directory)->depth(0)->hasResults();
}

/**
* Determine if the given path is readable.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @method static bool isFile(string $file)
* @method static bool isReadable(string $path)
* @method static bool isWritable(string $path)
* @method static bool isEmptyDirectory(string $directory, bool $ignoreDotFiles = false)
* @method static bool makeDirectory(string $path, int $mode = 0755, bool $recursive = false, bool $force = false)
* @method static bool missing(string $path)
* @method static bool move(string $path, string $target)
Expand Down
23 changes: 23 additions & 0 deletions tests/Filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,29 @@ public function testIsReadable()
$this->assertFalse($files->isReadable(self::$tempDir.'/doesnotexist.txt'));
}

public function testIsDirEmpty()
{
mkdir(self::$tempDir.'/foo-dir');
file_put_contents(self::$tempDir.'/foo-dir/.hidden', 'foo');
mkdir(self::$tempDir.'/bar-dir');
file_put_contents(self::$tempDir.'/bar-dir/foo.txt', 'foo');
mkdir(self::$tempDir.'/baz-dir');
mkdir(self::$tempDir.'/baz-dir/.hidden');
mkdir(self::$tempDir.'/quz-dir');
mkdir(self::$tempDir.'/quz-dir/not-hidden');

$files = new Filesystem;

$this->assertTrue($files->isEmptyDirectory(self::$tempDir.'/foo-dir', true));
$this->assertFalse($files->isEmptyDirectory(self::$tempDir.'/foo-dir'));
$this->assertFalse($files->isEmptyDirectory(self::$tempDir.'/bar-dir', true));
$this->assertFalse($files->isEmptyDirectory(self::$tempDir.'/bar-dir'));
$this->assertTrue($files->isEmptyDirectory(self::$tempDir.'/baz-dir', true));
$this->assertFalse($files->isEmptyDirectory(self::$tempDir.'/baz-dir'));
$this->assertFalse($files->isEmptyDirectory(self::$tempDir.'/quz-dir', true));
$this->assertFalse($files->isEmptyDirectory(self::$tempDir.'/quz-dir'));
}

public function testGlobFindsFiles()
{
file_put_contents(self::$tempDir.'/foo.txt', 'foo');
Expand Down

0 comments on commit 77ceefe

Please sign in to comment.