DummyFileSystemStream.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the nelexa/zip package.
  5. * (c) Ne-Lexa <https://github.com/Ne-Lexa/php-zip>
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. namespace PhpZip\Tests\Internal;
  10. /**
  11. * Try to load using dummy stream.
  12. *
  13. * @see https://www.php.net/streamwrapper
  14. */
  15. class DummyFileSystemStream
  16. {
  17. /**
  18. * Stream context, assigned by PHP when the wrapper is registered.
  19. * Declaring it prevents a dynamic property deprecation on PHP 8.2+.
  20. *
  21. * @var resource|null
  22. */
  23. public $context = null;
  24. /** @var resource */
  25. private $fp;
  26. /**
  27. * Opens file or URL.
  28. *
  29. * This method is called immediately after the wrapper is
  30. * initialized (f.e. by {@see fopen()} and {@see file_get_contents()}).
  31. *
  32. * @param string $path specifies the URL that was passed to
  33. * the original function
  34. * @param string $mode the mode used to open the file, as detailed
  35. * for {@see fopen()}
  36. * @param int $options Holds additional flags set by the streams
  37. * API. It can hold one or more of the
  38. * following values OR'd together.
  39. * @param string|null $opened_path if the path is opened successfully, and
  40. * STREAM_USE_PATH is set in options,
  41. * opened_path should be set to the
  42. * full path of the file/resource that
  43. * was actually opened
  44. *
  45. * @see https://www.php.net/streamwrapper.stream-open
  46. *
  47. * @noinspection PhpUsageOfSilenceOperatorInspection
  48. */
  49. public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool
  50. {
  51. $parsedUrl = parse_url($path);
  52. $uri = substr($parsedUrl['path'], 1);
  53. $this->fp = @fopen($uri, $mode);
  54. return $this->fp !== false;
  55. }
  56. /**
  57. * Read from stream.
  58. *
  59. * This method is called in response to {@see fread()} and {@see fgets()}.
  60. *
  61. * Note: Remember to update the read/write position of the stream
  62. * (by the number of bytes that were successfully read).
  63. *
  64. * @param int $count how many bytes of data from the current
  65. * position should be returned
  66. *
  67. * @return false|string If there are less than count bytes available,
  68. * return as many as are available. If no more data
  69. * is available, return either FALSE or
  70. * an empty string.
  71. *
  72. * @see https://www.php.net/streamwrapper.stream-read
  73. */
  74. public function stream_read(int $count)
  75. {
  76. return fread($this->fp, $count);
  77. }
  78. /**
  79. * Seeks to specific location in a stream.
  80. *
  81. * This method is called in response to {@see fseek()}.
  82. * The read/write position of the stream should be updated according
  83. * to the offset and whence.
  84. *
  85. * @param int $offset the stream offset to seek to
  86. * @param int $whence Possible values:
  87. * {@see \SEEK_SET} - Set position equal to offset bytes.
  88. * {@see \SEEK_CUR} - Set position to current location plus offset.
  89. * {@see \SEEK_END} - Set position to end-of-file plus offset.
  90. *
  91. * @return bool return TRUE if the position was updated, FALSE otherwise
  92. *
  93. * @see https://www.php.net/streamwrapper.stream-seek
  94. */
  95. public function stream_seek(int $offset, int $whence = \SEEK_SET): bool
  96. {
  97. return fseek($this->fp, $offset, $whence) === 0;
  98. }
  99. /**
  100. * Retrieve the current position of a stream.
  101. *
  102. * This method is called in response to {@see fseek()} to determine
  103. * the current position.
  104. *
  105. * @return int should return the current position of the stream
  106. *
  107. * @see https://www.php.net/streamwrapper.stream-tell
  108. */
  109. public function stream_tell(): int
  110. {
  111. $pos = ftell($this->fp);
  112. if ($pos === false) {
  113. throw new \RuntimeException('Cannot get stream position.');
  114. }
  115. return $pos;
  116. }
  117. /**
  118. * Tests for end-of-file on a file pointer.
  119. *
  120. * This method is called in response to {@see feof()}.
  121. *
  122. * @return bool should return TRUE if the read/write position is at
  123. * the end of the stream and if no more data is available
  124. * to be read, or FALSE otherwise
  125. *
  126. * @see https://www.php.net/streamwrapper.stream-eof
  127. */
  128. public function stream_eof(): bool
  129. {
  130. return feof($this->fp);
  131. }
  132. /**
  133. * Retrieve information about a file resource.
  134. *
  135. * This method is called in response to {@see fstat()}.
  136. *
  137. * @see https://www.php.net/streamwrapper.stream-stat
  138. * @see https://www.php.net/stat
  139. * @see https://www.php.net/fstat
  140. */
  141. public function stream_stat(): array
  142. {
  143. return fstat($this->fp);
  144. }
  145. /**
  146. * Flushes the output.
  147. *
  148. * This method is called in response to {@see fflush()} and when the
  149. * stream is being closed while any unflushed data has been written to
  150. * it before.
  151. * If you have cached data in your stream but not yet stored it into
  152. * the underlying storage, you should do so now.
  153. *
  154. * @return bool should return TRUE if the cached data was successfully
  155. * stored (or if there was no data to store), or FALSE
  156. * if the data could not be stored
  157. *
  158. * @see https://www.php.net/streamwrapper.stream-flush
  159. */
  160. public function stream_flush(): bool
  161. {
  162. return fflush($this->fp);
  163. }
  164. /**
  165. * Truncate stream.
  166. *
  167. * Will respond to truncation, e.g., through {@see ftruncate()}.
  168. *
  169. * @param int $new_size the new size
  170. *
  171. * @return bool returns TRUE on success or FALSE on failure
  172. *
  173. * @see https://www.php.net/streamwrapper.stream-truncate
  174. */
  175. public function stream_truncate(int $new_size): bool
  176. {
  177. return ftruncate($this->fp, $new_size);
  178. }
  179. /**
  180. * Write to stream.
  181. *
  182. * This method is called in response to {@see fwrite().}
  183. *
  184. * Note: Remember to update the current position of the stream by
  185. * number of bytes that were successfully written.
  186. *
  187. * @param string $data should be stored into the underlying stream
  188. *
  189. * @return int should return the number of bytes that were successfully stored, or 0 if none could be stored
  190. *
  191. * @see https://www.php.net/streamwrapper.stream-write
  192. */
  193. public function stream_write(string $data): int
  194. {
  195. $bytes = fwrite($this->fp, $data);
  196. return $bytes === false ? 0 : $bytes;
  197. }
  198. /**
  199. * Close a resource.
  200. *
  201. * This method is called in response to {@see fclose()}.
  202. * All resources that were locked, or allocated, by the wrapper should be released.
  203. *
  204. * @see https://www.php.net/streamwrapper.stream-close
  205. */
  206. public function stream_close(): void
  207. {
  208. fclose($this->fp);
  209. }
  210. }