Stream.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Stream\Wrapper\IWrapper;
  36. /**
  37. * Interface \Hoa\Stream\Wrapper\IWrapper\Stream.
  38. *
  39. * Interface for “stream stream wrapper” class.
  40. *
  41. * @copyright Copyright © 2007-2017 Hoa community
  42. * @license New BSD License
  43. */
  44. interface Stream
  45. {
  46. /**
  47. * Retrieve the underlaying resource.
  48. *
  49. * @param int $castAs Can be STREAM_CAST_FOR_SELECT when
  50. * stream_select() is calling stream_cast() or
  51. * STREAM_CAST_AS_STREAM when stream_cast() is
  52. * called for other uses.
  53. * @return resource
  54. */
  55. public function stream_cast($castAs);
  56. /**
  57. * Close a resource.
  58. * This method is called in response to fclose().
  59. * All resources that were locked, or allocated, by the wrapper should be
  60. * released.
  61. *
  62. * @return void
  63. */
  64. public function stream_close();
  65. /**
  66. * Tests for end-of-file on a file pointer.
  67. * This method is called in response to feof().
  68. *
  69. * @return bool
  70. */
  71. public function stream_eof();
  72. /**
  73. * Flush the output.
  74. * This method is called in response to fflush().
  75. * If we have cached data in our stream but not yet stored it into the
  76. * underlying storage, we should do so now.
  77. *
  78. * @return bool
  79. */
  80. public function stream_flush();
  81. /**
  82. * Advisory file locking.
  83. * This method is called in response to flock(), when file_put_contents()
  84. * (when flags contains LOCK_EX), stream_set_blocking() and when closing the
  85. * stream (LOCK_UN).
  86. *
  87. * @param int $operation Operation is one the following:
  88. * * LOCK_SH to acquire a shared lock (reader) ;
  89. * * LOCK_EX to acquire an exclusive lock (writer) ;
  90. * * LOCK_UN to release a lock (shared or exclusive) ;
  91. * * LOCK_NB if we don't want flock() to
  92. * block while locking (not supported on
  93. * Windows).
  94. * @return bool
  95. */
  96. public function stream_lock($operation);
  97. /**
  98. * Change stream options.
  99. * This method is called to set metadata on the stream. It is called when
  100. * one of the following functions is called one a stream URL: touch, chmod,
  101. * chown or chgrp.
  102. *
  103. * @param string $path The file path or URL to set metadata.
  104. * @param int $option One of the following:
  105. * * STREAM_META_TOUCH,
  106. * * STREAM_META_OWNER_NAME,
  107. * * STREAM_META_OWNER,
  108. * * STREAM_META_GROUP_NAME,
  109. * * STREAM_META_GROUP,
  110. * * STREAM_META_ACCESS.
  111. * @param mixed $value An array or a scalar depending of the option.
  112. * @return bool
  113. */
  114. public function stream_metadata($path, $option, $value);
  115. /**
  116. * Open file or URL.
  117. * This method is called immediately after the wrapper is initialized (f.e.
  118. * by fopen() and file_get_contents()).
  119. *
  120. * @param string $path Specifies the URL that was passed to the
  121. * original function.
  122. * @param string $mode The mode used to open the file, as
  123. * detailed for fopen().
  124. * @param int $options Holds additional flags set by the
  125. * streams API. It can hold one or more of
  126. * the following values OR'd together:
  127. * * STREAM_USE_PATH, if path is relative,
  128. * search for the resource using the
  129. * include_path;
  130. * * STREAM_REPORT_ERRORS, if this is
  131. * set, you are responsible for raising
  132. * errors using trigger_error during
  133. * opening the stream. If this is not
  134. * set, you should not raise any errors.
  135. * @param string &$openedPath If the $path is opened successfully, and
  136. * STREAM_USE_PATH is set in $options,
  137. * $openedPath should be set to the full
  138. * path of the file/resource that was
  139. * actually opened.
  140. * @return bool
  141. */
  142. public function stream_open($path, $mode, $options, &$openedPath);
  143. /**
  144. * Read from stream.
  145. * This method is called in response to fread() and fgets().
  146. *
  147. * @param int $count How many bytes of data from the current
  148. * position should be returned.
  149. * @return string
  150. */
  151. public function stream_read($count);
  152. /**
  153. * Seek to specific location in a stream.
  154. * This method is called in response to fseek().
  155. * The read/write position of the stream should be updated according to the
  156. * $offset and $whence.
  157. *
  158. * @param int $offset The stream offset to seek to.
  159. * @param int $whence Possible values:
  160. * * SEEK_SET to set position equal to $offset
  161. * bytes ;
  162. * * SEEK_CUR to set position to current
  163. * location plus $offsete ;
  164. * * SEEK_END to set position to end-of-file
  165. * plus $offset.
  166. * @return bool
  167. */
  168. public function stream_seek($offset, $whence = SEEK_SET);
  169. /**
  170. * Change stream options.
  171. * This method is called to set options on the stream.
  172. *
  173. * @param int $option One of:
  174. * * STREAM_OPTION_BLOCKING, the method was
  175. * called in response to
  176. * stream_set_blocking() ;
  177. * * STREAM_OPTION_READ_TIMEOUT, the method
  178. * was called in response to
  179. * stream_set_timeout() ;
  180. * * STREAM_OPTION_WRITE_BUFFER, the method
  181. * was called in response to
  182. * stream_set_write_buffer().
  183. * @param int $arg1 If $option is:
  184. * * STREAM_OPTION_BLOCKING: requested blocking
  185. * mode (1 meaning block, 0 not blocking) ;
  186. * * STREAM_OPTION_READ_TIMEOUT: the timeout
  187. * in seconds ;
  188. * * STREAM_OPTION_WRITE_BUFFER: buffer mode
  189. * (STREAM_BUFFER_NONE or
  190. * STREAM_BUFFER_FULL).
  191. * @param int $arg2 If $option is:
  192. * * STREAM_OPTION_BLOCKING: this option is
  193. * not set ;
  194. * * STREAM_OPTION_READ_TIMEOUT: the timeout
  195. * in microseconds ;
  196. * * STREAM_OPTION_WRITE_BUFFER: the requested
  197. * buffer size.
  198. * @return bool
  199. */
  200. public function stream_set_option($option, $arg1, $arg2);
  201. /**
  202. * Retrieve information about a file resource.
  203. * This method is called in response to fstat().
  204. *
  205. * @return array
  206. */
  207. public function stream_stat();
  208. /**
  209. * Retrieve the current position of a stream.
  210. * This method is called in response to ftell().
  211. *
  212. * @return int
  213. */
  214. public function stream_tell();
  215. /**
  216. * Truncate a stream to a given length.
  217. *
  218. * @param int $size Size.
  219. * @return bool
  220. */
  221. public function stream_truncate($size);
  222. /**
  223. * Write to stream.
  224. * This method is called in response to fwrite().
  225. *
  226. * @param string $data Should be stored into the underlying stream.
  227. * @return int
  228. */
  229. public function stream_write($data);
  230. }