Skip to content

Commit 062bf19

Browse files
committed
feat(Path): Add file system inspection methods
- Added exists() to check directory existence - Added getFiles() to retrieve File objects in directory - Enhanced getBaseNames() to use getFiles() - These methods support efficient file system operations
1 parent c5d2bb0 commit 062bf19

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

  • src/imperazim/components/filesystem

src/imperazim/components/filesystem/Path.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,53 @@ public function getFolder(): string {
4949
return $this->sourceFolder;
5050
}
5151

52+
/**
53+
* Checks if the directory exists.
54+
* @return bool
55+
*/
56+
public function exists(): bool {
57+
return is_dir($this->sourceFolder);
58+
}
59+
60+
/**
61+
* Gets all files in the directory as File objects.
62+
* @return File[]
63+
*/
64+
public function getFiles(): array {
65+
if (!$this->exists()) {
66+
return [];
67+
}
68+
69+
$files = [];
70+
$iterator = new DirectoryIterator($this->sourceFolder);
71+
72+
foreach ($iterator as $file) {
73+
if ($file->isDot() || $file->isDir()) continue;
74+
75+
$fileName = $file->getBasename('.' . $file->getExtension());
76+
$extension = $file->getExtension();
77+
$fileType = File::getTypeByExtension($extension);
78+
79+
$files[] = new File(
80+
directoryOrConfig: $this->sourceFolder,
81+
fileName: $fileName,
82+
fileType: $fileType
83+
);
84+
}
85+
86+
return $files;
87+
}
88+
89+
/**
90+
* Gets all file base names (without extension) in the directory.
91+
* @return string[]
92+
*/
93+
public function getBaseNames(): array {
94+
return array_map(
95+
fn(File $file) => $file->getFileName(),
96+
$this->getFiles()
97+
);
98+
}
5299

53100
/**
54101
* Adds a new file to the directory using the File class.

0 commit comments

Comments
 (0)