|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tamedevelopers\Support\Capsule; |
| 6 | + |
| 7 | +/** |
| 8 | + * Class File |
| 9 | + * |
| 10 | + * Provides file handling utilities similar to Laravel's Filesystem. |
| 11 | + * Supports reading, writing, deleting, copying, moving, and checking files. |
| 12 | + */ |
| 13 | +class File { |
| 14 | + |
| 15 | + /** |
| 16 | + * Get all files in a directory as SplFileInfo objects. |
| 17 | + * |
| 18 | + * @param string $directory |
| 19 | + * @return array<int, \SplFileInfo> |
| 20 | + */ |
| 21 | + public static function files(string $directory): array |
| 22 | + { |
| 23 | + $result = []; |
| 24 | + if (!self::isDirectory($directory)) { |
| 25 | + return $result; |
| 26 | + } |
| 27 | + |
| 28 | + $iterator = new \FilesystemIterator($directory, \FilesystemIterator::SKIP_DOTS); |
| 29 | + foreach ($iterator as $file) { |
| 30 | + if ($file->isFile()) { |
| 31 | + $result[] = $file; |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + return $result; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Create a directory. |
| 40 | + * |
| 41 | + * @param string $path |
| 42 | + * @param int $mode |
| 43 | + * @param bool $recursive |
| 44 | + * @return bool |
| 45 | + */ |
| 46 | + public static function makeDirectory(string $path, int $mode = 0777, bool $recursive = true): bool |
| 47 | + { |
| 48 | + if (self::isDirectory($path)) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + return mkdir($path, $mode, $recursive); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Validate a file name for unsafe characters. |
| 57 | + * |
| 58 | + * @param string $fileName |
| 59 | + * @param bool $disallowUnsafeCharacters |
| 60 | + * @return bool |
| 61 | + */ |
| 62 | + public static function isValidName(string $fileName, bool $disallowUnsafeCharacters = true): bool |
| 63 | + { |
| 64 | + if ($disallowUnsafeCharacters) { |
| 65 | + // Disallow: / \ ? % * : | " < > |
| 66 | + return !preg_match('/[\\\/\?%\*:|"<>]/', $fileName); |
| 67 | + } |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Determine if a file exists at a given path. |
| 73 | + * |
| 74 | + * @param string $path |
| 75 | + * @return bool |
| 76 | + */ |
| 77 | + public static function exists(string $path): bool |
| 78 | + { |
| 79 | + return is_file($path); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Get the contents of a file. |
| 84 | + * |
| 85 | + * @param string $path |
| 86 | + * @return string|false |
| 87 | + */ |
| 88 | + public static function get(string $path): string|false |
| 89 | + { |
| 90 | + return @file_get_contents($path); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Write the contents to a file. |
| 95 | + * |
| 96 | + * @param string $path |
| 97 | + * @param string $contents |
| 98 | + * @param int $flags |
| 99 | + * @return bool|int |
| 100 | + */ |
| 101 | + public static function put(string $path, string $contents, int $flags = 0): bool|int |
| 102 | + { |
| 103 | + return @file_put_contents($path, $contents, $flags); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Delete the file at the given path. |
| 108 | + * |
| 109 | + * @param string $path |
| 110 | + * @return bool |
| 111 | + */ |
| 112 | + public static function delete(string $path): bool |
| 113 | + { |
| 114 | + return self::exists($path) ? unlink($path) : false; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Copy a file to a new location. |
| 119 | + * |
| 120 | + * @param string $from |
| 121 | + * @param string $to |
| 122 | + * @return bool |
| 123 | + */ |
| 124 | + public static function copy(string $from, string $to): bool |
| 125 | + { |
| 126 | + return copy($from, $to); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Move a file to a new location. |
| 131 | + * |
| 132 | + * @param string $from |
| 133 | + * @param string $to |
| 134 | + * @return bool |
| 135 | + */ |
| 136 | + public static function move(string $from, string $to): bool |
| 137 | + { |
| 138 | + return rename($from, $to); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Get the file size. |
| 143 | + * |
| 144 | + * @param string $path |
| 145 | + * @return int|false |
| 146 | + */ |
| 147 | + public static function size(string $path): int|false |
| 148 | + { |
| 149 | + return filesize($path); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Get the file's last modification time. |
| 154 | + * |
| 155 | + * @param string $path |
| 156 | + * @return int|false |
| 157 | + */ |
| 158 | + public static function lastModified(string $path): int|false |
| 159 | + { |
| 160 | + return filemtime($path); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Get the file's extension. |
| 165 | + * |
| 166 | + * @param string $path |
| 167 | + * @return string|null |
| 168 | + */ |
| 169 | + public static function extension(string $path): ?string |
| 170 | + { |
| 171 | + $ext = pathinfo($path, PATHINFO_EXTENSION); |
| 172 | + return $ext !== '' ? $ext : null; |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Get the file's basename. |
| 177 | + * |
| 178 | + * @param string $path |
| 179 | + * @return string |
| 180 | + */ |
| 181 | + public static function name(string $path): string |
| 182 | + { |
| 183 | + return pathinfo($path, PATHINFO_FILENAME); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Get the file's mime type. |
| 188 | + * |
| 189 | + * @param string $path |
| 190 | + * @return string|false |
| 191 | + */ |
| 192 | + public static function mimeType(string $path): string|false |
| 193 | + { |
| 194 | + return mime_content_type($path); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Get the file's type (file, dir, link, etc). |
| 199 | + * |
| 200 | + * @param string $path |
| 201 | + * @return string|false |
| 202 | + */ |
| 203 | + public static function type(string $path): string|false |
| 204 | + { |
| 205 | + return filetype($path); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Get the file's permissions. |
| 210 | + * |
| 211 | + * @param string $path |
| 212 | + * @return int|false |
| 213 | + */ |
| 214 | + public static function permissions(string $path): int|false |
| 215 | + { |
| 216 | + return fileperms($path); |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Check if the file is readable. |
| 221 | + * |
| 222 | + * @param string $path |
| 223 | + * @return bool |
| 224 | + */ |
| 225 | + public static function isReadable(string $path): bool |
| 226 | + { |
| 227 | + return is_readable($path); |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * Check if the file is writable. |
| 232 | + * |
| 233 | + * @param string $path |
| 234 | + * @return bool |
| 235 | + */ |
| 236 | + public static function isWritable(string $path): bool |
| 237 | + { |
| 238 | + return is_writable($path); |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Check if the file is a directory. |
| 243 | + * |
| 244 | + * @param string $path |
| 245 | + * @return bool |
| 246 | + */ |
| 247 | + public static function isDirectory(string $path): bool |
| 248 | + { |
| 249 | + return is_dir($path); |
| 250 | + } |
| 251 | + |
| 252 | + /** |
| 253 | + * Check if the file is a regular file. |
| 254 | + * |
| 255 | + * @param string $path |
| 256 | + * @return bool |
| 257 | + */ |
| 258 | + public static function isFile(string $path): bool |
| 259 | + { |
| 260 | + return is_file($path); |
| 261 | + } |
| 262 | + |
| 263 | + /** |
| 264 | + * Determines if the given string represents a valid file type. |
| 265 | + * |
| 266 | + * @param string $string The string to check for file type validity. |
| 267 | + * @return bool Returns true if the string is a valid file type, false otherwise. |
| 268 | + */ |
| 269 | + public static function isFileType(?string $string = null) |
| 270 | + { |
| 271 | + return pathinfo($string, PATHINFO_EXTENSION) !== ''; |
| 272 | + } |
| 273 | + |
| 274 | +} |
0 commit comments