StreamInterface.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace GuzzleHttp\Stream;
  3. /**
  4. * Describes a stream instance.
  5. */
  6. interface StreamInterface
  7. {
  8. /**
  9. * Attempts to seek to the beginning of the stream and reads all data into
  10. * a string until the end of the stream is reached.
  11. *
  12. * Warning: This could attempt to load a large amount of data into memory.
  13. *
  14. * @return string
  15. */
  16. public function __toString();
  17. /**
  18. * Closes the stream and any underlying resources.
  19. */
  20. public function close();
  21. /**
  22. * Separates any underlying resources from the stream.
  23. *
  24. * After the underlying resource has been detached, the stream object is in
  25. * an unusable state. If you wish to use a Stream object as a PHP stream
  26. * but keep the Stream object in a consistent state, use
  27. * {@see GuzzleHttp\Stream\GuzzleStreamWrapper::getResource}.
  28. *
  29. * @return resource|null Returns the underlying PHP stream resource or null
  30. * if the Stream object did not utilize an underlying
  31. * stream resource.
  32. */
  33. public function detach();
  34. /**
  35. * Replaces the underlying stream resource with the provided stream.
  36. *
  37. * Use this method to replace the underlying stream with another; as an
  38. * example, in server-side code, if you decide to return a file, you
  39. * would replace the original content-oriented stream with the file
  40. * stream.
  41. *
  42. * Any internal state such as caching of cursor position should be reset
  43. * when attach() is called, as the stream has changed.
  44. *
  45. * @param resource $stream
  46. *
  47. * @return void
  48. */
  49. public function attach($stream);
  50. /**
  51. * Get the size of the stream if known
  52. *
  53. * @return int|null Returns the size in bytes if known, or null if unknown
  54. */
  55. public function getSize();
  56. /**
  57. * Returns the current position of the file read/write pointer
  58. *
  59. * @return int|bool Returns the position of the file pointer or false on error
  60. */
  61. public function tell();
  62. /**
  63. * Returns true if the stream is at the end of the stream.
  64. *
  65. * @return bool
  66. */
  67. public function eof();
  68. /**
  69. * Returns whether or not the stream is seekable
  70. *
  71. * @return bool
  72. */
  73. public function isSeekable();
  74. /**
  75. * Seek to a position in the stream
  76. *
  77. * @param int $offset Stream offset
  78. * @param int $whence Specifies how the cursor position will be calculated
  79. * based on the seek offset. Valid values are identical
  80. * to the built-in PHP $whence values for `fseek()`.
  81. * SEEK_SET: Set position equal to offset bytes
  82. * SEEK_CUR: Set position to current location plus offset
  83. * SEEK_END: Set position to end-of-stream plus offset
  84. *
  85. * @return bool Returns true on success or false on failure
  86. * @link http://www.php.net/manual/en/function.fseek.php
  87. */
  88. public function seek($offset, $whence = SEEK_SET);
  89. /**
  90. * Returns whether or not the stream is writable
  91. *
  92. * @return bool
  93. */
  94. public function isWritable();
  95. /**
  96. * Write data to the stream
  97. *
  98. * @param string $string The string that is to be written.
  99. *
  100. * @return int|bool Returns the number of bytes written to the stream on
  101. * success returns false on failure (e.g., broken pipe,
  102. * writer needs to slow down, buffer is full, etc.)
  103. */
  104. public function write($string);
  105. /**
  106. * Returns whether or not the stream is readable
  107. *
  108. * @return bool
  109. */
  110. public function isReadable();
  111. /**
  112. * Read data from the stream
  113. *
  114. * @param int $length Read up to $length bytes from the object and return
  115. * them. Fewer than $length bytes may be returned if
  116. * underlying stream call returns fewer bytes.
  117. *
  118. * @return string Returns the data read from the stream.
  119. */
  120. public function read($length);
  121. /**
  122. * Returns the remaining contents of the stream as a string.
  123. *
  124. * Note: this could potentially load a large amount of data into memory.
  125. *
  126. * @return string
  127. */
  128. public function getContents();
  129. /**
  130. * Get stream metadata as an associative array or retrieve a specific key.
  131. *
  132. * The keys returned are identical to the keys returned from PHP's
  133. * stream_get_meta_data() function.
  134. *
  135. * @param string $key Specific metadata to retrieve.
  136. *
  137. * @return array|mixed|null Returns an associative array if no key is
  138. * no key is provided. Returns a specific key
  139. * value if a key is provided and the value is
  140. * found, or null if the key is not found.
  141. * @see http://php.net/manual/en/function.stream-get-meta-data.php
  142. */
  143. public function getMetadata($key = null);
  144. }