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 |