PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.4.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.4.0
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 6 years ago Integrations 6 years ago StorageDriver 5 years ago Url 6 years ago data 6 years ago Api.php 6 years ago Crypto.php 6 years ago Device.php 6 years ago DeviceType.php 6 years ago EmptyConfigException.php 6 years ago Filesystem.php 5 years ago IntegrationUrl.php 6 years ago NitroPack.php 5 years ago Pagecache.php 5 years ago PurgeType.php 6 years ago StorageException.php 6 years ago Website.php 6 years ago
Filesystem.php
148 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 private static function explodeByHeaders($content) {
122 $headers = [];
123 $pos = strpos($content, "\r\n\r\n");
124 if ($pos !== false) {
125 $headerStr = substr($content, 0, $pos);
126 $content = substr($content, $pos+4);
127 if ($headerStr) {
128 $lines = explode("\r\n", $headerStr);
129 foreach ($lines as $line) {
130 $parts = explode(":", $line);
131 $name = strtolower(array_shift($parts));
132 $value = trim(implode(":", $parts));
133 if (!empty($headers[$name])) {
134 if (!is_array($headers[$name])) {
135 $headers[$name] = array($headers[$name]);
136 }
137 $headers[$name][] = $value;
138 } else {
139 $headers[$name] = $value;
140 }
141 }
142 }
143 }
144
145 return array($headers, $content);
146 }
147 }
148