PluginProbe ʕ •ᴥ•ʔ
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization / 1.18.0
NitroPack – Performance, Page Speed & Cache Plugin for Core Web Vitals, CDN & Image Optimization v1.18.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 / classes / Util / CacheStreamWrapper.php
nitropack / classes / Util Last commit date
CacheStreamWrapper.php 2 years ago VoidCacheManager.php 2 years ago
CacheStreamWrapper.php
184 lines
1 <?php
2
3 namespace NitroPack\Util;
4
5 use NitroPack\Util\VoidCacheManager;
6
7 class CacheStreamWrapper {
8 public $context;
9 private $startTime;
10 private $endTime;
11 private $stream;
12 private $path;
13
14 public static $timeTaken = [];
15 private static $cacheManager = NULL;
16
17 private $cacheLoaded = false;
18 private $content = "";
19 private $readPos = 0;
20 private $ctxOpts = [];
21
22 public static function setCacheManager($cacheManager) {
23 self::$cacheManager = $cacheManager;
24 }
25
26 public static function init() {
27 if (!self::$cacheManager) {
28 self::$cacheManager = new VoidCacheManager();
29 }
30
31 self::register();
32 }
33
34 private static function register() {
35 stream_wrapper_unregister('https');
36 stream_wrapper_unregister('http');
37 stream_wrapper_register('https', __CLASS__);
38 stream_wrapper_register('http', __CLASS__);
39 }
40
41 private static function restore() {
42 stream_wrapper_restore('https');
43 stream_wrapper_restore('http');
44 }
45
46 public function stream_open($path, $mode, $options, &$opened_path) {
47 if ($this->context && is_resource($this->context)) {
48 $this->ctxOpts = stream_context_get_options($this->context);
49 }
50
51 $this->path = $path;
52 $this->startTime = microtime(true);
53 if ($this->hasCache()) {
54 $this->content = self::$cacheManager->getCache($this->path);
55 $this->cacheLoaded = true;
56 return true;
57 }
58
59 self::restore();
60 $this->stream = fopen($path, $mode, $options, $this->context);
61 self::register();
62
63 return $this->stream !== false;
64 }
65
66 public function stream_read($count) {
67 if ($this->cacheLoaded) {
68 $chunk = substr($this->content, $this->readPos, $count);
69 $this->readPos += $count;
70 return $chunk;
71 }
72 $chunk = fread($this->stream, $count);
73 $this->content .= $chunk;
74 return $chunk;
75 }
76
77 public function stream_write($data) {
78 if ($this->cacheLoaded) {
79 return strlen($data);
80 }
81 return fwrite($this->stream, $data);
82 }
83
84 public function stream_close() {
85 $this->endTime = microtime(true);
86 self::$timeTaken[$this->path] = $this->endTime - $this->startTime;
87
88 if ($this->cacheLoaded) return;
89
90 fclose($this->stream);
91 $this->saveCache();
92 }
93
94 public function stream_eof() {
95 if ($this->cacheLoaded) {
96 return $this->readPos >= strlen($this->content);
97 }
98 return feof($this->stream);
99 }
100
101 public function stream_stat() {
102 return false;
103 }
104
105 public function stream_seek($offset, $whence = SEEK_SET) {
106 return 0;
107 }
108
109 public function stream_tell() {
110 return 0;
111 }
112
113 public function stream_metadata($path, $option, $value) {
114 return false;
115 }
116
117 public function url_stat($path, $flags) {
118 self::restore();
119 $stat = @stat($path);
120 self::register();
121 return $stat;
122 }
123
124 public function stream_set_option($option, $arg1, $arg2) {
125 return false;
126 }
127
128 public function dir_closedir() {
129 return false;
130 }
131
132 public function dir_opendir($path, $options) {
133 return false;
134 }
135
136 public function dir_readdir() {
137 return false;
138 }
139
140 public function dir_rewinddir() {
141 return false;
142 }
143
144 public function mkdir($path, $mode, $options) {
145 return false;
146 }
147
148 public function rename($path_from, $path_to) {
149 return false;
150 }
151
152 public function rmdir($path, $options) {
153 return false;
154 }
155
156 public function stream_cast($cast_as) {
157 return $this->stream;
158 }
159
160 public function unlink($path) {
161 return false;
162 }
163
164 private function hasCache() {
165 if (!$this->canCache()) return false;
166 return self::$cacheManager->hasCache($this->path);
167 }
168
169 private function saveCache() {
170 if (!$this->canCache()) return;
171 self::$cacheManager->setCache($this->path, $this->content);
172 }
173
174 private function canCache() {
175 if (!empty($this->ctxOpts["http"])) {
176 if (!empty($this->ctxOpts["http"]["method"]) && $this->ctxOpts["http"]["method"] !== "GET") {
177 return false;
178 }
179 }
180
181 return self::$cacheManager->isCacheAllowed($this->path);
182 }
183 }
184