| 1: | <?php |
| 2: | |
| 3: | declare(strict_types = 1); |
| 4: | |
| 5: | namespace CloudCastle\FileSystem; |
| 6: | |
| 7: | use \SplFileInfo; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | class File |
| 18: | { |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | public int $aTime = 0; |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | public ?string $basename; |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | public int $cTime = 0; |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | public ?string $extension; |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | public ?string $filename; |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | public int $group = 0; |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | public int $inode = 0; |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | public ?string $linkTarget = null; |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | public int $mTime; |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | public ?int $owner; |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | public ?string $path; |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | public ?string $pathname; |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | public int $perms = 0; |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | public ?string $realPath; |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | public int $size = 0; |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | public ?string $type = null; |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | public bool $isDir = false; |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | |
| 126: | public bool $executable = false; |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | public bool $isFile = false; |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | public bool $isLink = false; |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | public bool $isReadable = false; |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | public bool $isWritable = false; |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | public static function has(?string $file = null): bool |
| 158: | { |
| 159: | if (is_file($file)) { |
| 160: | return true; |
| 161: | } |
| 162: | return false; |
| 163: | } |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | public static function delete(string $file): bool |
| 171: | { |
| 172: | if (self::has($file) AND unlink($file)) { |
| 173: | return true; |
| 174: | } |
| 175: | return false; |
| 176: | } |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | public static function rewrite(string $file, ?string $content = null): bool |
| 185: | { |
| 186: | if (self::has($file) AND self::delete($file)) { |
| 187: | return self::create($file, $content); |
| 188: | } |
| 189: | return false; |
| 190: | } |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | public static function create(string $file, ?string $content = null): bool |
| 199: | { |
| 200: | if (Dir::create(dirname($file), 0777, true) AND ! self::has($file)) { |
| 201: | file_put_contents($file, $content, LOCK_EX); |
| 202: | return self::has($file); |
| 203: | } |
| 204: | return false; |
| 205: | } |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | public static function add(string $file, string $content): bool |
| 214: | { |
| 215: | if (self::has($file)) { |
| 216: | if (file_put_contents($file, $content, LOCK_EX | FILE_APPEND)) { |
| 217: | return true; |
| 218: | } |
| 219: | } |
| 220: | return false; |
| 221: | } |
| 222: | |
| 223: | |
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 229: | public static function permissions(string $file, int $permissions = 0777): bool |
| 230: | { |
| 231: | if (self::has($file) AND chmod($file, $permissions)) { |
| 232: | return true; |
| 233: | } |
| 234: | return false; |
| 235: | } |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: | |
| 241: | |
| 242: | public static function read(string $file) |
| 243: | { |
| 244: | if (self::has($file)) { |
| 245: | return file_get_contents($file); |
| 246: | } |
| 247: | return false; |
| 248: | } |
| 249: | |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | |
| 255: | public static function info(string $file): self |
| 256: | { |
| 257: | $obj = new SplFileInfo($file); |
| 258: | $data = new self(); |
| 259: | $data->aTime = $obj->getATime(); |
| 260: | $data->basename = $obj->getBasename(); |
| 261: | $data->cTime = $obj->getCTime(); |
| 262: | $data->extension = $obj->getExtension(); |
| 263: | $data->filename = $obj->getFilename(); |
| 264: | $data->group = $obj->getGroup(); |
| 265: | $data->inode = $obj->getInode(); |
| 266: | $data->mTime = $obj->getMTime(); |
| 267: | $data->owner = $obj->getOwner(); |
| 268: | $data->path = $obj->getPath(); |
| 269: | $data->pathname = $obj->getPathname(); |
| 270: | $data->perms = $obj->getPerms(); |
| 271: | $data->realPath = $obj->getRealPath(); |
| 272: | $data->size = $obj->getSize(); |
| 273: | $data->type = $obj->getType(); |
| 274: | $data->isDir = $obj->isDir(); |
| 275: | $data->executable = $obj->isExecutable(); |
| 276: | $data->isFile = $obj->isFile(); |
| 277: | $data->isLink = $obj->isLink(); |
| 278: | $data->isReadable = $obj->isReadable(); |
| 279: | $data->isWritable = $obj->isWritable(); |
| 280: | return $data; |
| 281: | } |
| 282: | |
| 283: | } |
| 284: | |