diff --git a/src/Storage/Device.php b/src/Storage/Device.php index 23027d94..fdc73483 100644 --- a/src/Storage/Device.php +++ b/src/Storage/Device.php @@ -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. * diff --git a/src/Storage/Device/Local.php b/src/Storage/Device/Local.php index 6d5c0f13..6ef23b63 100644 --- a/src/Storage/Device/Local.php +++ b/src/Storage/Device/Local.php @@ -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; } /** @@ -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; diff --git a/src/Storage/Device/S3.php b/src/Storage/Device/S3.php index 2c771719..625056b5 100644 --- a/src/Storage/Device/S3.php +++ b/src/Storage/Device/S3.php @@ -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 * diff --git a/tests/Storage/Device/LocalTest.php b/tests/Storage/Device/LocalTest.php index 9db0510c..a2156b6a 100644 --- a/tests/Storage/Device/LocalTest.php +++ b/tests/Storage/Device/LocalTest.php @@ -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)); + } }