DummyFileSystemStream.php 6.6 KB

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