PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.5.2
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.5.2
1.19.8 1.19.7 1.19.6 1.19.5 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.12.0 1.13.0 1.14.0 1.15.0 1.15.1 1.15.2 1.15.3 1.16.0 1.16.1 1.16.2 1.16.3 1.16.4 1.16.5 1.16.6 1.16.7 1.16.8 1.17.0 1.17.6 1.17.7 1.17.8 1.17.9 1.18.0 1.18.1 1.18.2 1.18.3 1.18.4 1.18.5 1.18.6 1.18.7 1.18.8 1.18.9 1.19.0 1.19.1 1.19.2 1.19.3 1.19.4 1.3.19 1.3.20 1.4.0 1.4.1 1.5.0 1.5.1 1.5.10 1.5.11 1.5.12 1.5.13 1.5.14 1.5.15 1.5.16 1.5.17 1.5.18 1.5.19 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.7.0 1.7.1 1.8.0 1.8.1 1.8.3 1.9.0 1.9.1 1.9.2
nitropack / nitropack-sdk / NitroPack / SDK / Filesystem.php
nitropack / nitropack-sdk / NitroPack / SDK Last commit date
Api 5 years ago Integrations 6 years ago StorageDriver 5 years ago Url 6 years ago data 6 years ago Api.php 5 years ago Backlog.php 5 years ago Crypto.php 6 years ago Device.php 6 years ago DeviceType.php 6 years ago EmptyConfigException.php 6 years ago FileHandle.php 5 years ago Filesystem.php 5 years ago HealthStatus.php 5 years ago IntegrationUrl.php 6 years ago NitroPack.php 5 years ago NoConfigException.php 5 years ago Pagecache.php 5 years ago PurgeType.php 6 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
192 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) {
67 return Filesystem::getStorageDriver()->touch($filePath);
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 ($headers === NULL) {
102 return Filesystem::getStorageDriver()->setContent($file, $content);
103 } else if (is_array($headers)) {
104 $headerStr = implode("\r\n", $headers) . "\r\n\r\n";
105 return Filesystem::getStorageDriver()->setContent($file, $headerStr . $content);
106 } else if (is_string($headers)) {
107 return Filesystem::getStorageDriver()->setContent($file, $headers . $content);
108 } else {
109 return Filesystem::getStorageDriver()->setContent($file, $content);
110 }
111 }
112
113 public static function rename($oldName, $newName) {
114 if (self::fileExists($oldName)) {
115 return Filesystem::getStorageDriver()->rename($oldName, $newName);
116 }
117
118 return false;
119 }
120
121 public static function fopen($file, $mode) {
122 return Filesystem::getStorageDriver()->fopen($file, $mode);
123 }
124
125 public static function fclose($fh) {
126 return Filesystem::getStorageDriver()->fclose($fh);
127 }
128
129 public static function fflush($fh) {
130 return Filesystem::getStorageDriver()->fflush($fh);
131 }
132
133 public static function fseek($fh, $offset, $whence = SEEK_SET) {
134 return Filesystem::getStorageDriver()->fseek($fh, $offset, $whence);
135 }
136
137 public static function ftell($fh) {
138 return Filesystem::getStorageDriver()->ftell($fh);
139 }
140
141 public static function fwrite($fh, $string, $length = NULL) {
142 return Filesystem::getStorageDriver()->fwrite($fh, $string, $length);
143 }
144
145 public static function fread($fh, $length) {
146 return Filesystem::getStorageDriver()->fread($fh, $length);
147 }
148
149 public static function fgetc($fh) {
150 return Filesystem::getStorageDriver()->fgetc($fh);
151 }
152
153 public static function fgets($fh, $length = NULL) {
154 return Filesystem::getStorageDriver()->fgets($fh, $length);
155 }
156
157 public static function flock($fh, $operation, $wouldblock = NULL) {
158 return Filesystem::getStorageDriver()->flock($fh, $operation, $wouldblock);
159 }
160
161 public static function feof($fh) {
162 return Filesystem::getStorageDriver()->feof($fh);
163 }
164
165 private static function explodeByHeaders($content) {
166 $headers = [];
167 $pos = strpos($content, "\r\n\r\n");
168 if ($pos !== false) {
169 $headerStr = substr($content, 0, $pos);
170 $content = substr($content, $pos+4);
171 if ($headerStr) {
172 $lines = explode("\r\n", $headerStr);
173 foreach ($lines as $line) {
174 $parts = explode(":", $line);
175 $name = strtolower(array_shift($parts));
176 $value = trim(implode(":", $parts));
177 if (!empty($headers[$name])) {
178 if (!is_array($headers[$name])) {
179 $headers[$name] = array($headers[$name]);
180 }
181 $headers[$name][] = $value;
182 } else {
183 $headers[$name] = $value;
184 }
185 }
186 }
187 }
188
189 return array($headers, $content);
190 }
191 }
192