StreamInterface.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Psr\Http\Message;
  3. /**
  4. * Describes a data stream.
  5. *
  6. * Typically, an instance will wrap a PHP stream; this interface provides
  7. * a wrapper around the most common operations, including serialization of
  8. * the entire stream to a string.
  9. */
  10. interface StreamInterface
  11. {
  12. /**
  13. * Reads all data from the stream into a string, from the beginning to end.
  14. *
  15. * This method MUST attempt to seek to the beginning of the stream before
  16. * reading data and read the stream until the end is reached.
  17. *
  18. * Warning: This could attempt to load a large amount of data into memory.
  19. *
  20. * This method MUST NOT raise an exception in order to conform with PHP's
  21. * string casting operations.
  22. *
  23. * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring
  24. * @return string
  25. */
  26. public function __toString();
  27. /**
  28. * Closes the stream and any underlying resources.
  29. *
  30. * @return void
  31. */
  32. public function close();
  33. /**
  34. * Separates any underlying resources from the stream.
  35. *
  36. * After the stream has been detached, the stream is in an unusable state.
  37. *
  38. * @return resource|null Underlying PHP stream, if any
  39. */
  40. public function detach();
  41. /**
  42. * Get the size of the stream if known.
  43. *
  44. * @return int|null Returns the size in bytes if known, or null if unknown.
  45. */
  46. public function getSize();
  47. /**
  48. * Returns the current position of the file read/write pointer
  49. *
  50. * @return int Position of the file pointer
  51. * @throws \RuntimeException on error.
  52. */
  53. public function tell();
  54. /**
  55. * Returns true if the stream is at the end of the stream.
  56. *
  57. * @return bool
  58. */
  59. public function eof();
  60. /**
  61. * Returns whether or not the stream is seekable.
  62. *
  63. * @return bool
  64. */
  65. public function isSeekable();
  66. /**
  67. * Seek to a position in the stream.
  68. *
  69. * @link http://www.php.net/manual/en/function.fseek.php
  70. * @param int $offset Stream offset
  71. * @param int $whence Specifies how the cursor position will be calculated
  72. * based on the seek offset. Valid values are identical to the built-in
  73. * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to
  74. * offset bytes SEEK_CUR: Set position to current location plus offset
  75. * SEEK_END: Set position to end-of-stream plus offset.
  76. * @throws \RuntimeException on failure.
  77. */
  78. public function seek($offset, $whence = SEEK_SET);
  79. /**
  80. * Seek to the beginning of the stream.
  81. *
  82. * If the stream is not seekable, this method will raise an exception;
  83. * otherwise, it will perform a seek(0).
  84. *
  85. * @see seek()
  86. * @link http://www.php.net/manual/en/function.fseek.php
  87. * @throws \RuntimeException on failure.
  88. */
  89. public function rewind();
  90. /**
  91. * Returns whether or not the stream is writable.
  92. *
  93. * @return bool
  94. */
  95. public function isWritable();
  96. /**
  97. * Write data to the stream.
  98. *
  99. * @param string $string The string that is to be written.
  100. * @return int Returns the number of bytes written to the stream.
  101. * @throws \RuntimeException on failure.
  102. */
  103. public function write($string);
  104. /**
  105. * Returns whether or not the stream is readable.
  106. *
  107. * @return bool
  108. */
  109. public function isReadable();
  110. /**
  111. * Read data from the stream.
  112. *
  113. * @param int $length Read up to $length bytes from the object and return
  114. * them. Fewer than $length bytes may be returned if underlying stream
  115. * call returns fewer bytes.
  116. * @return string Returns the data read from the stream, or an empty string
  117. * if no bytes are available.
  118. * @throws \RuntimeException if an error occurs.
  119. */
  120. public function read($length);
  121. /**
  122. * Returns the remaining contents in a string
  123. *
  124. * @return string
  125. * @throws \RuntimeException if unable to read or an error occurs while
  126. * reading.
  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. * @link http://php.net/manual/en/function.stream-get-meta-data.php
  136. * @param string $key Specific metadata to retrieve.
  137. * @return array|mixed|null Returns an associative array if no key is
  138. * provided. Returns a specific key value if a key is provided and the
  139. * value is found, or null if the key is not found.
  140. */
  141. public function getMetadata($key = null);
  142. }