Skip to content

Commit

Permalink
add ensureExists method for Directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Aug 6, 2021
1 parent b35b000 commit 784dce3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function clean(): bool
/**
* Create a directory.
*
* @param int $mode The mode is 0777 by default, which means the widest possible access.
* @param int $mode The mode is 0755 by default, which means the widest possible access.
* @param bool $recursive Allows the creation of nested directories specified in the path.
*
* @return bool Returns TRUE on success or FALSE on failure.
Expand Down Expand Up @@ -146,6 +146,23 @@ public function copy(string $destination, ?int $flags = null): bool
return true;
}

/**
* Ensure a directory exists.
*
* @param int $mode The mode is 0755 by default, which means the widest possible access.
* @param bool $recursive Allows the creation of nested directories specified in the path.
*
* @return bool Returns TRUE on success or FALSE on failure.
*/
public function ensureExists($mode = 0755, $recursive = false)
{
if ($this->exists() === false) {
return $this->create($mode, $recursive);
}

return true;
}

/**
* Checks the existence of directory and returns false if any of them is missing.
*
Expand Down
7 changes: 6 additions & 1 deletion tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@
$this->assertTrue($filesystem->isStream('file://1.txt'));
});


test('test isAbsolute method', function (): void {
$filesystem = new Filesystem();

Expand Down Expand Up @@ -228,6 +227,12 @@
$this->assertTrue($filesystem->directory($this->tempDir . '/2/3/4/')->create(0755, true));
});

test('test directory ensureExists() method', function (): void {
$filesystem = new Filesystem();
$this->assertTrue($filesystem->directory($this->tempDir . '/1')->ensureExists());
$this->assertTrue($filesystem->directory($this->tempDir . '/1/2/3/4/')->ensureExists(0755, true));
$this->assertTrue($filesystem->directory($this->tempDir . '/2/3/4/')->ensureExists(0755, true));
});

test('test directory move() method', function (): void {
@mkdir($this->tempDir . '/1');
Expand Down

0 comments on commit 784dce3

Please sign in to comment.