Lines
68.18%
30 / 44
Methods
50.00%
5 / 10
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 62.50% | 5 / 8 | 0.00% | 0 / 1 | 3.47 | ||
| getFiles | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| search | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| exists | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| delete | 88.88% | 8 / 9 | 0.00% | 0 / 1 | 4.02 | ||
| create | 66.66% | 2 / 3 | 0.00% | 0 / 1 | 2.15 | ||
| copy | 87.50% | 7 / 8 | 0.00% | 0 / 1 | 4.03 | ||
| [Lucent\Filesystem\FileSystemObject] getDirectory | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [Lucent\Filesystem\FileSystemObject] getName | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [Lucent\Filesystem\FileSystemObject] rename | 0.00% | 0 / 8 | 0.00% | 0 / 1 | 12 | ||
| 17 | class Folder extends FileSystemObject | |
| 18 | { | |
| 19 | /** | |
| 20 | * Creates a new Folder instance | |
| 21 | * | |
| 22 | * @param string $path The path to the folder (relative or absolute) | |
| 23 | * @param bool $absolute Whether the provided path is absolute (true) or relative to root (false) | |
| 24 | */ | |
| 25 | public function __construct(string $path, bool $absolute = false){ | |
| 26 | if(!$absolute){ | |
| 27 | $path = FileSystem::resolvePath($path); | |
| 28 | } | |
| 29 | ||
| 30 | // Normalize the path so `..`/`.` segments are resolved before the | |
| 31 | // containment guard runs. | |
| 32 | $path = FileSystem::normalizePath($path); | |
| 33 | ||
| 34 | // Containment guard: never allow a path to escape the configured root. | |
| 35 | if (!FileSystem::isWithinRoot($path)) { | |
| 36 | throw new \RuntimeException( | |
| 37 | "Folder path '{$path}' escapes the configured root path '" . FileSystem::rootPath() . "'" | |
| 38 | ); | |
| 39 | } | |
| 40 | ||
| 41 | $this->path = $path; | |
| 42 | } | |
| 43 | ||
| 44 | /** | |
| 45 | * Gets all files in the folder | |
| 46 | * | |
| 47 | * @param bool $recursive Whether to get files recursively from subdirectories | |
| 48 | * @return array Array of File objects | |
| 49 | */ | |
| 50 | public function getFiles(bool $recursive = false) : array | |
| 51 | { | |
| 52 | $collection = $this->search()->onlyFiles(); | |
| 53 | ||
| 54 | if($recursive){ | |
| 55 | $collection->recursive(); | |
| 56 | } | |
| 57 | ||
| 58 | return $collection->collect(); | |
| 59 | } | |
| 60 | ||
| 61 | /** | |
| 62 | * Creates a search collection for this folder | |
| 63 | * | |
| 64 | * @return FileSystemCollection A new collection for searching within this folder | |
| 65 | */ | |
| 66 | public function search() : FileSystemCollection | |
| 67 | { | |
| 68 | return new FileSystemCollection($this); | |
| 69 | } | |
| 70 | ||
| 71 | /** | |
| 72 | * Checks if the folder exists | |
| 73 | * | |
| 74 | * @return bool True if the folder exists, false otherwise | |
| 75 | */ | |
| 76 | public function exists() : bool{ | |
| 77 | return is_dir($this->path); | |
| 78 | } | |
| 79 | ||
| 80 | /** | |
| 81 | * Deletes the folder and all its contents | |
| 82 | * | |
| 83 | * @return bool True if successfully deleted, false otherwise | |
| 84 | */ | |
| 85 | public function delete() : bool | |
| 86 | { | |
| 87 | if (!$this->exists()) { | |
| 88 | return false; | |
| 89 | } | |
| 90 | ||
| 91 | $it = new RecursiveDirectoryIterator($this->path, RecursiveDirectoryIterator::SKIP_DOTS); | |
| 92 | $files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST); | |
| 93 | ||
| 94 | foreach ($files as $file) { | |
| 95 | if ($file->isDir()) { | |
| 96 | rmdir($file->getRealPath()); | |
| 97 | } else { | |
| 98 | unlink($file->getRealPath()); | |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | return rmdir($this->path); | |
| 103 | } | |
| 104 | ||
| 105 | /** | |
| 106 | * Creates the folder | |
| 107 | * | |
| 108 | * @param mixed $params Permissions for the folder (numeric, e.g., 0777) | |
| 109 | * @return bool True if successfully created, false otherwise | |
| 110 | * @throws \Exception If params is not numeric | |
| 111 | */ | |
| 112 | public function create(mixed $params = 0777) : bool | |
| 113 | { | |
| 114 | if(!is_numeric($params)){ | |
| 115 | throw new \Exception("Params must be numeric permission value ie, 0777"); | |
| 116 | } | |
| 117 | return mkdir($this->path, $params, true); | |
| 118 | } | |
| 119 | ||
| 120 | /** | |
| 121 | * Copies all files from this folder to another folder | |
| 122 | * | |
| 123 | * @param FileSystemObject $object Destination folder | |
| 124 | * @return bool True if successful, false otherwise | |
| 125 | */ | |
| 126 | public function copy(FileSystemObject $object): bool | |
| 127 | { | |
| 128 | $outcome = false; | |
| 129 | ||
| 130 | // Copy all files from the source folder (this) to the destination folder | |
| 131 | foreach ($this->search()->collect() as $fileSystemObject) { | |
| 132 | if ($fileSystemObject instanceof File) { | |
| 133 | $newFile = $fileSystemObject->copy($fileSystemObject->getName(), $object); | |
| 134 | $outcome = $newFile->exists(); | |
| 135 | continue; | |
| 136 | } | |
| 137 | if($fileSystemObject instanceof Folder){ | |
| 138 | // $fileSystemObject->copy($folder->getName(), $folder); | |
| 139 | } | |
| 140 | } | |
| 141 | ||
| 142 | // Return the outcome | |
| 143 | return $outcome; | |
| 144 | } | |
| 145 | } |
Inherited from Lucent\Filesystem\FileSystemObject
| 26 | public function getDirectory(): Folder | |
| 27 | { | |
| 28 | return new Folder(dirname($this->path), true); | |
| 29 | } |
| 36 | public function getName(): string | |
| 37 | { | |
| 38 | return basename($this->path); | |
| 39 | } |
| 70 | public function rename(string $newName): bool | |
| 71 | { | |
| 72 | // Get the directory path of the current object | |
| 73 | $directory = $this->getDirectory()->path; | |
| 74 | ||
| 75 | // Construct the new full path | |
| 76 | $newPath = $directory . DIRECTORY_SEPARATOR . $newName; | |
| 77 | ||
| 78 | // Check if the target already exists | |
| 79 | if (file_exists($newPath)) { | |
| 80 | throw new \RuntimeException("Cannot rename: target '{$newPath}' already exists"); | |
| 81 | } | |
| 82 | ||
| 83 | // Perform the rename operation | |
| 84 | if (rename($this->path, $newPath)) { | |
| 85 | // Update the path property to reflect the new path | |
| 86 | $this->path = $newPath; | |
| 87 | return true; | |
| 88 | } | |
| 89 | ||
| 90 | return false; | |
| 91 | } |