Disk.php
104 lines
| 1 | <?php |
| 2 | namespace NitroPack\SDK\StorageDriver; |
| 3 | |
| 4 | class Disk { |
| 5 | public function getOsPath($parts) { |
| 6 | return implode(DIRECTORY_SEPARATOR, $parts); |
| 7 | } |
| 8 | |
| 9 | public function deleteFile($path) { |
| 10 | return @unlink($path); |
| 11 | } |
| 12 | |
| 13 | public function createDir($dir) { |
| 14 | if (!is_dir($dir) && !mkdir($dir, 0755, true)) { |
| 15 | return false; |
| 16 | } |
| 17 | |
| 18 | return true; |
| 19 | } |
| 20 | |
| 21 | public function deleteDir($dir) { |
| 22 | if (!is_dir($dir)) return true; |
| 23 | return $this->trunkDir($dir) && rmdir($dir); |
| 24 | } |
| 25 | |
| 26 | public function trunkDir($dir) { |
| 27 | if (!is_dir($dir)) return true; |
| 28 | $dh = opendir($dir); |
| 29 | if ($dh === false) return false; |
| 30 | |
| 31 | while (false !== ($entry = readdir($dh))) { |
| 32 | if ($entry == "." || $entry == "..") continue; |
| 33 | $path = $this->getOsPath(array($dir, $entry)); |
| 34 | if (is_dir($path)) { |
| 35 | if (!$this->deleteDir($path)) { |
| 36 | closedir($dh); |
| 37 | return false; |
| 38 | } |
| 39 | } else { |
| 40 | if (!unlink($path)) { |
| 41 | closedir($dh); |
| 42 | return false; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | closedir($dh); |
| 47 | |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | public function isDirEmpty($dir) { |
| 52 | if (!is_dir($dir)) return false; |
| 53 | $dh = opendir($dir); |
| 54 | if ($dh === false) return false; |
| 55 | |
| 56 | $isEmpty = true; |
| 57 | while (false !== ($entry = readdir($dh))) { |
| 58 | if ($entry == "." || $entry == "..") continue; |
| 59 | $isEmpty = false; // first entry which is not "." or ".." means the dir is not empty |
| 60 | break; |
| 61 | } |
| 62 | closedir($dh); |
| 63 | |
| 64 | return $isEmpty; |
| 65 | } |
| 66 | |
| 67 | public function dirForeach($dir, $callback) { |
| 68 | if (!is_dir($dir)) return false; |
| 69 | $dh = opendir($dir); |
| 70 | if ($dh === false) return false; |
| 71 | |
| 72 | while (false !== ($entry = readdir($dh))) { |
| 73 | if ($entry == "." || $entry == "..") continue; |
| 74 | call_user_func($callback, $this->getOsPath(array($dir, $entry))); |
| 75 | } |
| 76 | closedir($dh); |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | public function mtime($filePath) { |
| 81 | return @filemtime($filePath); |
| 82 | } |
| 83 | |
| 84 | public function touch($filePath) { |
| 85 | return @touch($filePath); |
| 86 | } |
| 87 | |
| 88 | public function exists($filePath) { |
| 89 | return file_exists($filePath); |
| 90 | } |
| 91 | |
| 92 | public function getContent($filePath) { |
| 93 | return file_get_contents($filePath); |
| 94 | } |
| 95 | |
| 96 | public function setContent($file, $content) { |
| 97 | return @file_put_contents($file, $content); |
| 98 | } |
| 99 | |
| 100 | public function rename($oldName, $newName) { |
| 101 | return @rename($oldName, $newName); |
| 102 | } |
| 103 | } |
| 104 |