DummyFileSystemStream.php 7.0 KB

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