Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Adds isDirectoryEmpty() and isDirectoryNotEmpty() to Filesystem #42559

Merged
merged 9 commits into from
May 31, 2022
26 changes: 26 additions & 0 deletions src/Illuminate/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,32 @@ public function isDirectory($directory)
return is_dir($directory);
}

/**
* Determine if the given path is a directory without files or directories.
*
* @param string $directory
* @param bool $hidden
* @return bool
*/
public function isDirectoryEmpty($directory, $hidden = false)
{
return $this->isDirectory($directory)
&& ! Finder::create()->files()->ignoreDotFiles(! $hidden)->in($directory)->depth(0)->hasResults();
}

/**
* Determine if the given directory as any file or directory.
*
* @param string $directory
* @param bool $hidden
* @return bool
*/
public function isDirectoryNotEmpty($directory, $hidden = false)
{
return $this->isDirectory($directory)
DarkGhostHunter marked this conversation as resolved.
Show resolved Hide resolved
&& Finder::create()->ignoreDotFiles(! $hidden)->in($directory)->depth(0)->hasResults();
}

/**
* Determine if the given path is readable.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Support/Facades/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* @method static bool isFile(string $file)
* @method static bool isReadable(string $path)
* @method static bool isWritable(string $path)
* @method static bool isDirectoryEmpty(string $directory, bool $hidden = false)
* @method static bool isDirectoryNotEmpty(string $directory, bool $hidden = 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
36 changes: 36 additions & 0 deletions tests/Filesystem/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,42 @@ 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->isDirectoryEmpty(self::$tempDir.'/foo-dir'));
$this->assertFalse($files->isDirectoryEmpty(self::$tempDir.'/foo-dir', true));
$this->assertFalse($files->isDirectoryEmpty(self::$tempDir.'/bar-dir'));
$this->assertFalse($files->isDirectoryEmpty(self::$tempDir.'/bar-dir', true));
$this->assertFalse($files->isDirectoryEmpty(self::$tempDir.'/bar-dir/foo.txt'));
$this->assertFalse($files->isDirectoryEmpty(self::$tempDir.'/bar-dir/foo.txt', true));
$this->assertTrue($files->isDirectoryEmpty(self::$tempDir.'/baz-dir'));
$this->assertFalse($files->isDirectoryEmpty(self::$tempDir.'/baz-dir', true));
$this->assertFalse($files->isDirectoryEmpty(self::$tempDir.'/quz-dir'));
$this->assertFalse($files->isDirectoryEmpty(self::$tempDir.'/quz-dir', true));

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

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