ReadableStreamInterface.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. namespace React\Stream;
  3. use Evenement\EventEmitterInterface;
  4. /**
  5. * The `ReadableStreamInterface` is responsible for providing an interface for
  6. * read-only streams and the readable side of duplex streams.
  7. *
  8. * Besides defining a few methods, this interface also implements the
  9. * `EventEmitterInterface` which allows you to react to certain events:
  10. *
  11. * data event:
  12. * The `data` event will be emitted whenever some data was read/received
  13. * from this source stream.
  14. * The event receives a single mixed argument for incoming data.
  15. *
  16. * ```php
  17. * $stream->on('data', function ($data) {
  18. * echo $data;
  19. * });
  20. * ```
  21. *
  22. * This event MAY be emitted any number of times, which may be zero times if
  23. * this stream does not send any data at all.
  24. * It SHOULD not be emitted after an `end` or `close` event.
  25. *
  26. * The given `$data` argument may be of mixed type, but it's usually
  27. * recommended it SHOULD be a `string` value or MAY use a type that allows
  28. * representation as a `string` for maximum compatibility.
  29. *
  30. * Many common streams (such as a TCP/IP connection or a file-based stream)
  31. * will emit the raw (binary) payload data that is received over the wire as
  32. * chunks of `string` values.
  33. *
  34. * Due to the stream-based nature of this, the sender may send any number
  35. * of chunks with varying sizes. There are no guarantees that these chunks
  36. * will be received with the exact same framing the sender intended to send.
  37. * In other words, many lower-level protocols (such as TCP/IP) transfer the
  38. * data in chunks that may be anywhere between single-byte values to several
  39. * dozens of kilobytes. You may want to apply a higher-level protocol to
  40. * these low-level data chunks in order to achieve proper message framing.
  41. *
  42. * end event:
  43. * The `end` event will be emitted once the source stream has successfully
  44. * reached the end of the stream (EOF).
  45. *
  46. * ```php
  47. * $stream->on('end', function () {
  48. * echo 'END';
  49. * });
  50. * ```
  51. *
  52. * This event SHOULD be emitted once or never at all, depending on whether
  53. * a successful end was detected.
  54. * It SHOULD NOT be emitted after a previous `end` or `close` event.
  55. * It MUST NOT be emitted if the stream closes due to a non-successful
  56. * end, such as after a previous `error` event.
  57. *
  58. * After the stream is ended, it MUST switch to non-readable mode,
  59. * see also `isReadable()`.
  60. *
  61. * This event will only be emitted if the *end* was reached successfully,
  62. * not if the stream was interrupted by an unrecoverable error or explicitly
  63. * closed. Not all streams know this concept of a "successful end".
  64. * Many use-cases involve detecting when the stream closes (terminates)
  65. * instead, in this case you should use the `close` event.
  66. * After the stream emits an `end` event, it SHOULD usually be followed by a
  67. * `close` event.
  68. *
  69. * Many common streams (such as a TCP/IP connection or a file-based stream)
  70. * will emit this event if either the remote side closes the connection or
  71. * a file handle was successfully read until reaching its end (EOF).
  72. *
  73. * Note that this event should not be confused with the `end()` method.
  74. * This event defines a successful end *reading* from a source stream, while
  75. * the `end()` method defines *writing* a successful end to a destination
  76. * stream.
  77. *
  78. * error event:
  79. * The `error` event will be emitted once a fatal error occurs, usually while
  80. * trying to read from this stream.
  81. * The event receives a single `Exception` argument for the error instance.
  82. *
  83. * ```php
  84. * $stream->on('error', function (Exception $e) {
  85. * echo 'Error: ' . $e->getMessage() . PHP_EOL;
  86. * });
  87. * ```
  88. *
  89. * This event SHOULD be emitted once the stream detects a fatal error, such
  90. * as a fatal transmission error or after an unexpected `data` or premature
  91. * `end` event.
  92. * It SHOULD NOT be emitted after a previous `error`, `end` or `close` event.
  93. * It MUST NOT be emitted if this is not a fatal error condition, such as
  94. * a temporary network issue that did not cause any data to be lost.
  95. *
  96. * After the stream errors, it MUST close the stream and SHOULD thus be
  97. * followed by a `close` event and then switch to non-readable mode, see
  98. * also `close()` and `isReadable()`.
  99. *
  100. * Many common streams (such as a TCP/IP connection or a file-based stream)
  101. * only deal with data transmission and do not make assumption about data
  102. * boundaries (such as unexpected `data` or premature `end` events).
  103. * In other words, many lower-level protocols (such as TCP/IP) may choose
  104. * to only emit this for a fatal transmission error once and will then
  105. * close (terminate) the stream in response.
  106. *
  107. * If this stream is a `DuplexStreamInterface`, you should also notice
  108. * how the writable side of the stream also implements an `error` event.
  109. * In other words, an error may occur while either reading or writing the
  110. * stream which should result in the same error processing.
  111. *
  112. * close event:
  113. * The `close` event will be emitted once the stream closes (terminates).
  114. *
  115. * ```php
  116. * $stream->on('close', function () {
  117. * echo 'CLOSED';
  118. * });
  119. * ```
  120. *
  121. * This event SHOULD be emitted once or never at all, depending on whether
  122. * the stream ever terminates.
  123. * It SHOULD NOT be emitted after a previous `close` event.
  124. *
  125. * After the stream is closed, it MUST switch to non-readable mode,
  126. * see also `isReadable()`.
  127. *
  128. * Unlike the `end` event, this event SHOULD be emitted whenever the stream
  129. * closes, irrespective of whether this happens implicitly due to an
  130. * unrecoverable error or explicitly when either side closes the stream.
  131. * If you only want to detect a *successful* end, you should use the `end`
  132. * event instead.
  133. *
  134. * Many common streams (such as a TCP/IP connection or a file-based stream)
  135. * will likely choose to emit this event after reading a *successful* `end`
  136. * event or after a fatal transmission `error` event.
  137. *
  138. * If this stream is a `DuplexStreamInterface`, you should also notice
  139. * how the writable side of the stream also implements a `close` event.
  140. * In other words, after receiving this event, the stream MUST switch into
  141. * non-writable AND non-readable mode, see also `isWritable()`.
  142. * Note that this event should not be confused with the `end` event.
  143. *
  144. * The event callback functions MUST be a valid `callable` that obeys strict
  145. * parameter definitions and MUST accept event parameters exactly as documented.
  146. * The event callback functions MUST NOT throw an `Exception`.
  147. * The return value of the event callback functions will be ignored and has no
  148. * effect, so for performance reasons you're recommended to not return any
  149. * excessive data structures.
  150. *
  151. * Every implementation of this interface MUST follow these event semantics in
  152. * order to be considered a well-behaving stream.
  153. *
  154. * > Note that higher-level implementations of this interface may choose to
  155. * define additional events with dedicated semantics not defined as part of
  156. * this low-level stream specification. Conformance with these event semantics
  157. * is out of scope for this interface, so you may also have to refer to the
  158. * documentation of such a higher-level implementation.
  159. *
  160. * @see EventEmitterInterface
  161. */
  162. interface ReadableStreamInterface extends EventEmitterInterface
  163. {
  164. /**
  165. * Checks whether this stream is in a readable state (not closed already).
  166. *
  167. * This method can be used to check if the stream still accepts incoming
  168. * data events or if it is ended or closed already.
  169. * Once the stream is non-readable, no further `data` or `end` events SHOULD
  170. * be emitted.
  171. *
  172. * ```php
  173. * assert($stream->isReadable() === false);
  174. *
  175. * $stream->on('data', assertNeverCalled());
  176. * $stream->on('end', assertNeverCalled());
  177. * ```
  178. *
  179. * A successfully opened stream always MUST start in readable mode.
  180. *
  181. * Once the stream ends or closes, it MUST switch to non-readable mode.
  182. * This can happen any time, explicitly through `close()` or
  183. * implicitly due to a remote close or an unrecoverable transmission error.
  184. * Once a stream has switched to non-readable mode, it MUST NOT transition
  185. * back to readable mode.
  186. *
  187. * If this stream is a `DuplexStreamInterface`, you should also notice
  188. * how the writable side of the stream also implements an `isWritable()`
  189. * method. Unless this is a half-open duplex stream, they SHOULD usually
  190. * have the same return value.
  191. *
  192. * @return bool
  193. */
  194. public function isReadable();
  195. /**
  196. * Pauses reading incoming data events.
  197. *
  198. * Removes the data source file descriptor from the event loop. This
  199. * allows you to throttle incoming data.
  200. *
  201. * Unless otherwise noted, a successfully opened stream SHOULD NOT start
  202. * in paused state.
  203. *
  204. * Once the stream is paused, no futher `data` or `end` events SHOULD
  205. * be emitted.
  206. *
  207. * ```php
  208. * $stream->pause();
  209. *
  210. * $stream->on('data', assertShouldNeverCalled());
  211. * $stream->on('end', assertShouldNeverCalled());
  212. * ```
  213. *
  214. * This method is advisory-only, though generally not recommended, the
  215. * stream MAY continue emitting `data` events.
  216. *
  217. * You can continue processing events by calling `resume()` again.
  218. *
  219. * Note that both methods can be called any number of times, in particular
  220. * calling `pause()` more than once SHOULD NOT have any effect.
  221. *
  222. * @see self::resume()
  223. * @return void
  224. */
  225. public function pause();
  226. /**
  227. * Resumes reading incoming data events.
  228. *
  229. * Re-attach the data source after a previous `pause()`.
  230. *
  231. * ```php
  232. * $stream->pause();
  233. *
  234. * Loop::addTimer(1.0, function () use ($stream) {
  235. * $stream->resume();
  236. * });
  237. * ```
  238. *
  239. * Note that both methods can be called any number of times, in particular
  240. * calling `resume()` without a prior `pause()` SHOULD NOT have any effect.
  241. *
  242. * @see self::pause()
  243. * @return void
  244. */
  245. public function resume();
  246. /**
  247. * Pipes all the data from this readable source into the given writable destination.
  248. *
  249. * Automatically sends all incoming data to the destination.
  250. * Automatically throttles the source based on what the destination can handle.
  251. *
  252. * ```php
  253. * $source->pipe($dest);
  254. * ```
  255. *
  256. * Similarly, you can also pipe an instance implementing `DuplexStreamInterface`
  257. * into itself in order to write back all the data that is received.
  258. * This may be a useful feature for a TCP/IP echo service:
  259. *
  260. * ```php
  261. * $connection->pipe($connection);
  262. * ```
  263. *
  264. * This method returns the destination stream as-is, which can be used to
  265. * set up chains of piped streams:
  266. *
  267. * ```php
  268. * $source->pipe($decodeGzip)->pipe($filterBadWords)->pipe($dest);
  269. * ```
  270. *
  271. * By default, this will call `end()` on the destination stream once the
  272. * source stream emits an `end` event. This can be disabled like this:
  273. *
  274. * ```php
  275. * $source->pipe($dest, array('end' => false));
  276. * ```
  277. *
  278. * Note that this only applies to the `end` event.
  279. * If an `error` or explicit `close` event happens on the source stream,
  280. * you'll have to manually close the destination stream:
  281. *
  282. * ```php
  283. * $source->pipe($dest);
  284. * $source->on('close', function () use ($dest) {
  285. * $dest->end('BYE!');
  286. * });
  287. * ```
  288. *
  289. * If the source stream is not readable (closed state), then this is a NO-OP.
  290. *
  291. * ```php
  292. * $source->close();
  293. * $source->pipe($dest); // NO-OP
  294. * ```
  295. *
  296. * If the destinantion stream is not writable (closed state), then this will simply
  297. * throttle (pause) the source stream:
  298. *
  299. * ```php
  300. * $dest->close();
  301. * $source->pipe($dest); // calls $source->pause()
  302. * ```
  303. *
  304. * Similarly, if the destination stream is closed while the pipe is still
  305. * active, it will also throttle (pause) the source stream:
  306. *
  307. * ```php
  308. * $source->pipe($dest);
  309. * $dest->close(); // calls $source->pause()
  310. * ```
  311. *
  312. * Once the pipe is set up successfully, the destination stream MUST emit
  313. * a `pipe` event with this source stream an event argument.
  314. *
  315. * @param WritableStreamInterface $dest
  316. * @param array $options
  317. * @return WritableStreamInterface $dest stream as-is
  318. */
  319. public function pipe(WritableStreamInterface $dest, array $options = array());
  320. /**
  321. * Closes the stream (forcefully).
  322. *
  323. * This method can be used to (forcefully) close the stream.
  324. *
  325. * ```php
  326. * $stream->close();
  327. * ```
  328. *
  329. * Once the stream is closed, it SHOULD emit a `close` event.
  330. * Note that this event SHOULD NOT be emitted more than once, in particular
  331. * if this method is called multiple times.
  332. *
  333. * After calling this method, the stream MUST switch into a non-readable
  334. * mode, see also `isReadable()`.
  335. * This means that no further `data` or `end` events SHOULD be emitted.
  336. *
  337. * ```php
  338. * $stream->close();
  339. * assert($stream->isReadable() === false);
  340. *
  341. * $stream->on('data', assertNeverCalled());
  342. * $stream->on('end', assertNeverCalled());
  343. * ```
  344. *
  345. * If this stream is a `DuplexStreamInterface`, you should also notice
  346. * how the writable side of the stream also implements a `close()` method.
  347. * In other words, after calling this method, the stream MUST switch into
  348. * non-writable AND non-readable mode, see also `isWritable()`.
  349. * Note that this method should not be confused with the `end()` method.
  350. *
  351. * @return void
  352. * @see WritableStreamInterface::close()
  353. */
  354. public function close();
  355. }