[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Part of the Joomla Framework Filesystem Package 4 * 5 * @copyright Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved. 6 * @license GNU General Public License version 2 or later; see LICENSE 7 */ 8 9 namespace Joomla\Filesystem\Stream; 10 11 use Joomla\Filesystem\Support\StringController; 12 13 /** 14 * String Stream Wrapper 15 * 16 * This class allows you to use a PHP string in the same way that you would normally use a regular stream wrapper 17 * 18 * @since 1.3.0 19 */ 20 class StringWrapper 21 { 22 /** 23 * The current string 24 * 25 * @var string 26 * @since 1.3.0 27 */ 28 protected $currentString; 29 30 /** 31 * The path 32 * 33 * @var string 34 * @since 1.3.0 35 */ 36 protected $path; 37 38 /** 39 * The mode 40 * 41 * @var string 42 * @since 1.3.0 43 */ 44 protected $mode; 45 46 /** 47 * Enter description here ... 48 * 49 * @var string 50 * @since 1.3.0 51 */ 52 protected $options; 53 54 /** 55 * Enter description here ... 56 * 57 * @var string 58 * @since 1.3.0 59 */ 60 protected $openedPath; 61 62 /** 63 * Current position 64 * 65 * @var integer 66 * @since 1.3.0 67 */ 68 protected $pos; 69 70 /** 71 * Length of the string 72 * 73 * @var string 74 * @since 1.3.0 75 */ 76 protected $len; 77 78 /** 79 * Statistics for a file 80 * 81 * @var array 82 * @since 1.3.0 83 * @link https://www.php.net/manual/en/function.stat.php 84 */ 85 protected $stat; 86 87 /** 88 * Method to open a file or URL. 89 * 90 * @param string $path The stream path. 91 * @param string $mode Not used. 92 * @param integer $options Not used. 93 * @param string $openedPath Not used. 94 * 95 * @return boolean 96 * 97 * @since 1.3.0 98 */ 99 public function stream_open($path, $mode, $options, &$openedPath) 100 { 101 $refPath = StringController::getRef(str_replace('string://', '', $path)); 102 103 $this->currentString = &$refPath; 104 105 if ($this->currentString) 106 { 107 $this->len = \strlen($this->currentString); 108 $this->pos = 0; 109 $this->stat = $this->url_stat($path, 0); 110 111 return true; 112 } 113 114 return false; 115 } 116 117 /** 118 * Method to retrieve information from a file resource 119 * 120 * @return array 121 * 122 * @link https://www.php.net/manual/en/streamwrapper.stream-stat.php 123 * @since 1.3.0 124 */ 125 public function stream_stat() 126 { 127 return $this->stat; 128 } 129 130 /** 131 * Method to retrieve information about a file. 132 * 133 * @param string $path File path or URL to stat 134 * @param integer $flags Additional flags set by the streams API 135 * 136 * @return array 137 * 138 * @link https://www.php.net/manual/en/streamwrapper.url-stat.php 139 * @since 1.3.0 140 */ 141 public function url_stat($path, $flags = 0) 142 { 143 $now = time(); 144 $refPath = StringController::getRef(str_replace('string://', '', $path)); 145 $string = &$refPath; 146 $stat = [ 147 'dev' => 0, 148 'ino' => 0, 149 'mode' => 0, 150 'nlink' => 1, 151 'uid' => 0, 152 'gid' => 0, 153 'rdev' => 0, 154 'size' => \strlen($string), 155 'atime' => $now, 156 'mtime' => $now, 157 'ctime' => $now, 158 'blksize' => '512', 159 'blocks' => ceil(\strlen($string) / 512), 160 ]; 161 162 return $stat; 163 } 164 165 /** 166 * Method to read a given number of bytes starting at the current position 167 * and moving to the end of the string defined by the current position plus the 168 * given number. 169 * 170 * @param integer $count Bytes of data from the current position should be returned. 171 * 172 * @return string 173 * 174 * @link https://www.php.net/manual/en/streamwrapper.stream-read.php 175 * @since 1.3.0 176 */ 177 public function stream_read($count) 178 { 179 $result = substr($this->currentString, $this->pos, $count); 180 $this->pos += $count; 181 182 return $result; 183 } 184 185 /** 186 * Stream write, always returning false. 187 * 188 * @param string $data The data to write. 189 * 190 * @return boolean 191 * 192 * @since 1.3.0 193 * @note Updating the string is not supported. 194 */ 195 public function stream_write($data) 196 { 197 // We don't support updating the string. 198 return false; 199 } 200 201 /** 202 * Method to get the current position 203 * 204 * @return integer The position 205 * 206 * @since 1.3.0 207 */ 208 public function stream_tell() 209 { 210 return $this->pos; 211 } 212 213 /** 214 * End of field check 215 * 216 * @return boolean True if at end of field. 217 * 218 * @since 1.3.0 219 */ 220 public function stream_eof() 221 { 222 if ($this->pos >= $this->len) 223 { 224 return true; 225 } 226 227 return false; 228 } 229 230 /** 231 * Stream offset 232 * 233 * @param integer $offset The starting offset. 234 * @param integer $whence SEEK_SET, SEEK_CUR, SEEK_END 235 * 236 * @return boolean True on success. 237 * 238 * @since 1.3.0 239 */ 240 public function stream_seek($offset, $whence) 241 { 242 // $whence: SEEK_SET, SEEK_CUR, SEEK_END 243 if ($offset > $this->len) 244 { 245 // We can't seek beyond our len. 246 return false; 247 } 248 249 switch ($whence) 250 { 251 case \SEEK_SET: 252 $this->pos = $offset; 253 254 break; 255 256 case \SEEK_CUR: 257 if (($this->pos + $offset) > $this->len) 258 { 259 return false; 260 } 261 262 $this->pos += $offset; 263 264 break; 265 266 case \SEEK_END: 267 $this->pos = $this->len - $offset; 268 269 break; 270 } 271 272 return true; 273 } 274 275 /** 276 * Stream flush, always returns true. 277 * 278 * @return boolean 279 * 280 * @since 1.3.0 281 * @note Data storage is not supported 282 */ 283 public function stream_flush() 284 { 285 // We don't store data. 286 return true; 287 } 288 } 289 290 if (!stream_wrapper_register('string', '\\Joomla\\Filesystem\\Stream\\StringWrapper')) 291 { 292 die('\\Joomla\\Filesystem\\Stream\\StringWrapper Wrapper Registration Failed'); 293 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |