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

Feat: public getFiles in local device #83

Merged
merged 8 commits into from
Aug 21, 2023
Merged
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
8 changes: 8 additions & 0 deletions src/Storage/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,14 @@ abstract public function getPartitionFreeSpace(): float;
*/
abstract public function getPartitionTotalSpace(): float;

/**
* Get all files and directories inside a directory.
*
* @param string $dir Directory to scan
* @return string[]
*/
abstract public function getFiles(string $dir): array;

/**
* Get the absolute path by resolving strings like ../, .., //, /\ and so on.
*
Expand Down
24 changes: 14 additions & 10 deletions src/Storage/Device/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,23 @@ public function deletePath(string $path): bool
{
$path = realpath($this->getRoot().DIRECTORY_SEPARATOR.$path);

if (\is_dir($path)) {
$files = $this->getFiles($path);
if (! file_exists($path) || ! is_dir($path)) {
return false;
}

foreach ($files as $file) {
$files = $this->getFiles($path);

foreach ($files as $file) {
if (is_dir($file)) {
$this->deletePath(\ltrim($file, $this->getRoot().DIRECTORY_SEPARATOR));
} else {
$this->delete($file, true);
}

\rmdir($path);

return true;
}

return false;
\rmdir($path);

return true;
}

/**
Expand Down Expand Up @@ -498,12 +502,12 @@ public function getPartitionTotalSpace(): float
}

/**
* Get all files inside a directory.
* Get all files and directories inside a directory.
*
* @param string $dir Directory to scan
* @return string[]
*/
private function getFiles(string $dir): array
public function getFiles(string $dir): array
{
if (! (\str_ends_with($dir, DIRECTORY_SEPARATOR))) {
$dir .= DIRECTORY_SEPARATOR;
Expand Down
11 changes: 11 additions & 0 deletions src/Storage/Device/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,17 @@ public function getPartitionTotalSpace(): float
return -1;
}

/**
* Get all files and directories inside a directory.
*
* @param string $dir Directory to scan
* @return string[]
*/
public function getFiles(string $dir): array
{
throw new Exception('Not implemented.');
}

/**
* Get file info
*
Expand Down
33 changes: 33 additions & 0 deletions tests/Storage/Device/LocalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,37 @@ public function testDeletePath()
$this->assertEquals(false, $this->object->exists($path2));
$this->assertEquals(false, $this->object->exists($path3));
}

public function testGetFiles()
{
$dir = DIRECTORY_SEPARATOR.'get-files-test';

$this->assertTrue($this->object->createDirectory($dir));

$files = $this->object->getFiles($dir);
$this->assertEquals(0, \count($files));

$this->object->write($dir.DIRECTORY_SEPARATOR.'new-file.txt', 'Hello World');
$this->object->write($dir.DIRECTORY_SEPARATOR.'new-file-two.txt', 'Hello World');

$files = $this->object->getFiles($dir);
$this->assertEquals(2, \count($files));
}

public function testNestedDeletePath()
{
$dir = $this->object->getPath('nested-delete-path-test');
$dir2 = $dir.DIRECTORY_SEPARATOR.'dir2';
$dir3 = $dir2.DIRECTORY_SEPARATOR.'dir3';

$this->assertTrue($this->object->createDirectory($dir));
$this->object->write($dir.DIRECTORY_SEPARATOR.'new-file.txt', 'Hello World');
$this->assertTrue($this->object->createDirectory($dir2));
$this->object->write($dir2.DIRECTORY_SEPARATOR.'new-file-2.txt', 'Hello World');
$this->assertTrue($this->object->createDirectory($dir3));
$this->object->write($dir3.DIRECTORY_SEPARATOR.'new-file-3.txt', 'Hello World');

$this->assertTrue($this->object->deletePath('nested-delete-path-test'));
$this->assertFalse($this->object->exists($dir));
}
}