Disk.php
4 years ago
DiskFileHandle.php
5 years ago
Redis.php
4 years ago
RedisFileHandle.php
5 years ago
Disk.php
181 lines
| 1 | <?php |
| 2 | namespace NitroPack\SDK\StorageDriver; |
| 3 | |
| 4 | use \NitroPack\SDK\FileHandle; |
| 5 | |
| 6 | class Disk { |
| 7 | public function getOsPath($parts) { |
| 8 | return implode(DIRECTORY_SEPARATOR, $parts); |
| 9 | } |
| 10 | |
| 11 | public function deleteFile($path) { |
| 12 | return @unlink($path); |
| 13 | } |
| 14 | |
| 15 | public function createDir($dir) { |
| 16 | if (!is_dir($dir) && !mkdir($dir, 0755, true)) { |
| 17 | return false; |
| 18 | } |
| 19 | |
| 20 | return true; |
| 21 | } |
| 22 | |
| 23 | public function deleteDir($dir) { |
| 24 | if (!is_dir($dir)) return true; |
| 25 | return $this->trunkDir($dir) && rmdir($dir); |
| 26 | } |
| 27 | |
| 28 | public function trunkDir($dir) { |
| 29 | if (!is_dir($dir)) return true; |
| 30 | $dh = opendir($dir); |
| 31 | if ($dh === false) return false; |
| 32 | |
| 33 | while (false !== ($entry = readdir($dh))) { |
| 34 | if ($entry == "." || $entry == "..") continue; |
| 35 | $path = $this->getOsPath(array($dir, $entry)); |
| 36 | if (is_dir($path)) { |
| 37 | if (!$this->deleteDir($path)) { |
| 38 | closedir($dh); |
| 39 | return false; |
| 40 | } |
| 41 | } else { |
| 42 | if (!unlink($path)) { |
| 43 | closedir($dh); |
| 44 | return false; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | closedir($dh); |
| 49 | |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | public function isDirEmpty($dir) { |
| 54 | if (!is_dir($dir)) return false; |
| 55 | $dh = opendir($dir); |
| 56 | if ($dh === false) return false; |
| 57 | |
| 58 | $isEmpty = true; |
| 59 | while (false !== ($entry = readdir($dh))) { |
| 60 | if ($entry == "." || $entry == "..") continue; |
| 61 | $isEmpty = false; // first entry which is not "." or ".." means the dir is not empty |
| 62 | break; |
| 63 | } |
| 64 | closedir($dh); |
| 65 | |
| 66 | return $isEmpty; |
| 67 | } |
| 68 | |
| 69 | public function dirForeach($dir, $callback) { |
| 70 | if (!is_dir($dir)) return false; |
| 71 | $dh = opendir($dir); |
| 72 | if ($dh === false) return false; |
| 73 | |
| 74 | while (false !== ($entry = readdir($dh))) { |
| 75 | if ($entry == "." || $entry == "..") continue; |
| 76 | call_user_func($callback, $this->getOsPath(array($dir, $entry))); |
| 77 | } |
| 78 | closedir($dh); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | public function mtime($filePath) { |
| 83 | return @filemtime($filePath); |
| 84 | } |
| 85 | |
| 86 | public function touch($filePath, $time = NULL) { |
| 87 | if ($time && is_numeric($time)) { |
| 88 | return @touch($filePath, (int)$time); |
| 89 | } else { |
| 90 | return @touch($filePath); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public function exists($filePath) { |
| 95 | return file_exists($filePath); |
| 96 | } |
| 97 | |
| 98 | public function getContent($filePath) { |
| 99 | return file_get_contents($filePath); |
| 100 | } |
| 101 | |
| 102 | public function setContent($file, $content) { |
| 103 | return @file_put_contents($file, $content); |
| 104 | } |
| 105 | |
| 106 | public function rename($oldName, $newName) { |
| 107 | return @rename($oldName, $newName); |
| 108 | } |
| 109 | |
| 110 | public function fopen($file, $mode) { |
| 111 | $fh = @fopen($file, $mode); |
| 112 | if ($fh) { |
| 113 | return new DiskFileHandle($fh); |
| 114 | } else { |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | public function fclose($fh) { |
| 120 | if (!($fh instanceof FileHandle)) return false; |
| 121 | return @fclose($fh->getHandle()); |
| 122 | } |
| 123 | |
| 124 | public function fflush($fh) { |
| 125 | if (!($fh instanceof FileHandle)) return false; |
| 126 | return @fflush($fh->getHandle()); |
| 127 | } |
| 128 | |
| 129 | public function fseek($fh, $offset, $whence = SEEK_SET) { |
| 130 | if (!($fh instanceof FileHandle)) return false; |
| 131 | return @fseek($fh->getHandle(), $offset, $whence); |
| 132 | } |
| 133 | |
| 134 | public function ftell($fh) { |
| 135 | if (!($fh instanceof FileHandle)) return false; |
| 136 | return @ftell($fh->getHandle()); |
| 137 | } |
| 138 | |
| 139 | public function fwrite($fh, $string, $length = NULL) { |
| 140 | if (!($fh instanceof FileHandle)) return false; |
| 141 | if ($length !== NULL) { |
| 142 | return @fwrite($fh->getHandle(), $string, $length); |
| 143 | } else { |
| 144 | return @fwrite($fh->getHandle(), $string); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | public function fread($fh, $length) { |
| 149 | if (!($fh instanceof FileHandle)) return false; |
| 150 | return @fread($fh->getHandle(), $length); |
| 151 | } |
| 152 | |
| 153 | public function fgetc($fh) { |
| 154 | if (!($fh instanceof FileHandle)) return false; |
| 155 | return @fgetc($fh->getHandle()); |
| 156 | } |
| 157 | |
| 158 | public function fgets($fh, $length = NULL) { |
| 159 | if (!($fh instanceof FileHandle)) return false; |
| 160 | if ($length !== NULL) { |
| 161 | return @fgets($fh->getHandle(), $length); |
| 162 | } else { |
| 163 | return @fgets($fh->getHandle()); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | public function flock($fh, $operation, $wouldblock = NULL) { |
| 168 | if (!($fh instanceof FileHandle)) return false; |
| 169 | if ($wouldblock !== NULL) { |
| 170 | return @flock($fh->getHandle(), $operation, $wouldblock); |
| 171 | } else { |
| 172 | return @flock($fh->getHandle(), $operation); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | public function feof($fh) { |
| 177 | if (!($fh instanceof FileHandle)) return false; |
| 178 | return @feof($fh->getHandle()); |
| 179 | } |
| 180 | } |
| 181 |