Api
2 years ago
Integrations
2 years ago
StorageDriver
4 years ago
Url
2 years ago
Utils
2 years ago
data
6 years ago
Api.php
2 years ago
Backlog.php
4 years ago
BacklogReplayTimeoutException.php
4 years ago
ChallengeProcessingException.php
4 years ago
ChallengeVerificationException.php
4 years ago
ConfigFetcherException.php
4 years ago
Crypto.php
3 years ago
Device.php
6 years ago
DeviceType.php
6 years ago
ElementRevision.php
3 years ago
EmptyConfigException.php
6 years ago
ExcludeEntry.php
2 years ago
ExcludeOperationType.php
2 years ago
FileHandle.php
5 years ago
Filesystem.php
4 years ago
HealthStatus.php
5 years ago
IntegrationUrl.php
2 years ago
NitroPack.php
2 years ago
NoConfigException.php
5 years ago
Pagecache.php
2 years ago
PurgeType.php
3 years ago
ServiceDownException.php
5 years ago
StorageException.php
6 years ago
VariationCookieException.php
5 years ago
WebhookException.php
5 years ago
Website.php
6 years ago
Filesystem.php
190 lines
| 1 | <?php |
| 2 | namespace NitroPack\SDK; |
| 3 | |
| 4 | class Filesystem { |
| 5 | public static $storageDriver = NULL; |
| 6 | |
| 7 | public static function setStorageDriver($driver) { |
| 8 | Filesystem::$storageDriver = $driver; |
| 9 | } |
| 10 | |
| 11 | public static function getStorageDriver() { |
| 12 | if (Filesystem::$storageDriver === NULL) { |
| 13 | Filesystem::$storageDriver = new StorageDriver\Disk(); |
| 14 | } |
| 15 | |
| 16 | return Filesystem::$storageDriver; |
| 17 | } |
| 18 | |
| 19 | public static function getOsPath($parts) { |
| 20 | return Filesystem::getStorageDriver()->getOsPath($parts); |
| 21 | } |
| 22 | |
| 23 | public static function deleteFile($path) { |
| 24 | if (self::fileExists($path)) { |
| 25 | return Filesystem::getStorageDriver()->deleteFile($path); |
| 26 | } |
| 27 | |
| 28 | return true; |
| 29 | } |
| 30 | |
| 31 | public static function createDir($dir) { |
| 32 | return Filesystem::getStorageDriver()->createDir($dir); |
| 33 | } |
| 34 | |
| 35 | public static function deleteDir($dir) { |
| 36 | return Filesystem::getStorageDriver()->deleteDir($dir); |
| 37 | } |
| 38 | |
| 39 | public static function trunkDir($dir) { |
| 40 | return Filesystem::getStorageDriver()->trunkDir($dir); |
| 41 | } |
| 42 | |
| 43 | public static function isDirEmpty($dir) { |
| 44 | return Filesystem::getStorageDriver()->isDirEmpty($dir); |
| 45 | } |
| 46 | |
| 47 | public static function createCacheDir($dir) { |
| 48 | if (!self::createDir($dir . "/mobile") |
| 49 | || !self::createDir($dir . "/tablet") |
| 50 | || !self::createDir($dir . "/desktop") |
| 51 | ) { |
| 52 | return false; |
| 53 | } |
| 54 | |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | public static function dirForeach($dir, $callback) { |
| 59 | return Filesystem::getStorageDriver()->dirForeach($dir, $callback); |
| 60 | } |
| 61 | |
| 62 | public static function fileMTime($filePath) { |
| 63 | return self::fileExists($filePath) ? Filesystem::getStorageDriver()->mtime($filePath) : 0; |
| 64 | } |
| 65 | |
| 66 | public static function touch($filePath, $time = NULL) { |
| 67 | return Filesystem::getStorageDriver()->touch($filePath, $time); |
| 68 | } |
| 69 | |
| 70 | public static function fileGetHeaders($filePath) { |
| 71 | return self::fileGetAll($filePath)->headers; |
| 72 | } |
| 73 | |
| 74 | public static function fileGetContents($filePath) { |
| 75 | return self::fileGetAll($filePath)->content; |
| 76 | } |
| 77 | |
| 78 | public static function fileSize($filePath) { |
| 79 | return self::fileGetAll($filePath)->size; |
| 80 | } |
| 81 | |
| 82 | public static function fileExists($filePath) { |
| 83 | return Filesystem::getStorageDriver()->exists($filePath); |
| 84 | } |
| 85 | |
| 86 | public static function fileGetAll($filePath) { |
| 87 | if (!self::fileExists($filePath)) throw new \Exception("File not found: $filePath"); |
| 88 | |
| 89 | $res = new \stdClass(); |
| 90 | $res->headers = []; |
| 91 | $res->content = ""; |
| 92 | $res->size = 0; |
| 93 | |
| 94 | list($res->headers, $res->content) = self::explodeByHeaders(Filesystem::getStorageDriver()->getContent($filePath)); |
| 95 | $res->size = strlen($res->content); |
| 96 | |
| 97 | return $res; |
| 98 | } |
| 99 | |
| 100 | public static function filePutContents($file, $content, $headers = NULL) { |
| 101 | if (is_array($headers)) { |
| 102 | $headerStr = implode("\r\n", $headers) . "\r\n\r\n"; |
| 103 | return Filesystem::getStorageDriver()->setContent($file, $headerStr . $content); |
| 104 | } else if (is_string($headers)) { |
| 105 | return Filesystem::getStorageDriver()->setContent($file, $headers . $content); |
| 106 | } else { |
| 107 | return Filesystem::getStorageDriver()->setContent($file, $content); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | public static function rename($oldName, $newName) { |
| 112 | if (self::fileExists($oldName)) { |
| 113 | return Filesystem::getStorageDriver()->rename($oldName, $newName); |
| 114 | } |
| 115 | |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | public static function fopen($file, $mode) { |
| 120 | return Filesystem::getStorageDriver()->fopen($file, $mode); |
| 121 | } |
| 122 | |
| 123 | public static function fclose($fh) { |
| 124 | return Filesystem::getStorageDriver()->fclose($fh); |
| 125 | } |
| 126 | |
| 127 | public static function fflush($fh) { |
| 128 | return Filesystem::getStorageDriver()->fflush($fh); |
| 129 | } |
| 130 | |
| 131 | public static function fseek($fh, $offset, $whence = SEEK_SET) { |
| 132 | return Filesystem::getStorageDriver()->fseek($fh, $offset, $whence); |
| 133 | } |
| 134 | |
| 135 | public static function ftell($fh) { |
| 136 | return Filesystem::getStorageDriver()->ftell($fh); |
| 137 | } |
| 138 | |
| 139 | public static function fwrite($fh, $string, $length = NULL) { |
| 140 | return Filesystem::getStorageDriver()->fwrite($fh, $string, $length); |
| 141 | } |
| 142 | |
| 143 | public static function fread($fh, $length) { |
| 144 | return Filesystem::getStorageDriver()->fread($fh, $length); |
| 145 | } |
| 146 | |
| 147 | public static function fgetc($fh) { |
| 148 | return Filesystem::getStorageDriver()->fgetc($fh); |
| 149 | } |
| 150 | |
| 151 | public static function fgets($fh, $length = NULL) { |
| 152 | return Filesystem::getStorageDriver()->fgets($fh, $length); |
| 153 | } |
| 154 | |
| 155 | public static function flock($fh, $operation, $wouldblock = NULL) { |
| 156 | return Filesystem::getStorageDriver()->flock($fh, $operation, $wouldblock); |
| 157 | } |
| 158 | |
| 159 | public static function feof($fh) { |
| 160 | return Filesystem::getStorageDriver()->feof($fh); |
| 161 | } |
| 162 | |
| 163 | public static function explodeByHeaders($content) { |
| 164 | $headers = []; |
| 165 | $pos = strpos($content, "\r\n\r\n"); |
| 166 | if ($pos !== false) { |
| 167 | $headerStr = substr($content, 0, $pos); |
| 168 | $content = substr($content, $pos+4); |
| 169 | if ($headerStr) { |
| 170 | $lines = explode("\r\n", $headerStr); |
| 171 | foreach ($lines as $line) { |
| 172 | $parts = explode(":", $line); |
| 173 | $name = strtolower(array_shift($parts)); |
| 174 | $value = trim(implode(":", $parts)); |
| 175 | if (!empty($headers[$name])) { |
| 176 | if (!is_array($headers[$name])) { |
| 177 | $headers[$name] = array($headers[$name]); |
| 178 | } |
| 179 | $headers[$name][] = $value; |
| 180 | } else { |
| 181 | $headers[$name] = $value; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return array($headers, $content); |
| 188 | } |
| 189 | } |
| 190 |