ThroughStream.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace React\Stream;
  3. use Evenement\EventEmitter;
  4. use InvalidArgumentException;
  5. /**
  6. * The `ThroughStream` implements the
  7. * [`DuplexStreamInterface`](#duplexstreaminterface) and will simply pass any data
  8. * you write to it through to its readable end.
  9. *
  10. * ```php
  11. * $through = new ThroughStream();
  12. * $through->on('data', $this->expectCallableOnceWith('hello'));
  13. *
  14. * $through->write('hello');
  15. * ```
  16. *
  17. * Similarly, the [`end()` method](#end) will end the stream and emit an
  18. * [`end` event](#end-event) and then [`close()`](#close-1) the stream.
  19. * The [`close()` method](#close-1) will close the stream and emit a
  20. * [`close` event](#close-event).
  21. * Accordingly, this is can also be used in a [`pipe()`](#pipe) context like this:
  22. *
  23. * ```php
  24. * $through = new ThroughStream();
  25. * $source->pipe($through)->pipe($dest);
  26. * ```
  27. *
  28. * Optionally, its constructor accepts any callable function which will then be
  29. * used to *filter* any data written to it. This function receives a single data
  30. * argument as passed to the writable side and must return the data as it will be
  31. * passed to its readable end:
  32. *
  33. * ```php
  34. * $through = new ThroughStream('strtoupper');
  35. * $source->pipe($through)->pipe($dest);
  36. * ```
  37. *
  38. * Note that this class makes no assumptions about any data types. This can be
  39. * used to convert data, for example for transforming any structured data into
  40. * a newline-delimited JSON (NDJSON) stream like this:
  41. *
  42. * ```php
  43. * $through = new ThroughStream(function ($data) {
  44. * return json_encode($data) . PHP_EOL;
  45. * });
  46. * $through->on('data', $this->expectCallableOnceWith("[2, true]\n"));
  47. *
  48. * $through->write(array(2, true));
  49. * ```
  50. *
  51. * The callback function is allowed to throw an `Exception`. In this case,
  52. * the stream will emit an `error` event and then [`close()`](#close-1) the stream.
  53. *
  54. * ```php
  55. * $through = new ThroughStream(function ($data) {
  56. * if (!is_string($data)) {
  57. * throw new \UnexpectedValueException('Only strings allowed');
  58. * }
  59. * return $data;
  60. * });
  61. * $through->on('error', $this->expectCallableOnce()));
  62. * $through->on('close', $this->expectCallableOnce()));
  63. * $through->on('data', $this->expectCallableNever()));
  64. *
  65. * $through->write(2);
  66. * ```
  67. *
  68. * @see WritableStreamInterface::write()
  69. * @see WritableStreamInterface::end()
  70. * @see DuplexStreamInterface::close()
  71. * @see WritableStreamInterface::pipe()
  72. */
  73. final class ThroughStream extends EventEmitter implements DuplexStreamInterface
  74. {
  75. private $readable = true;
  76. private $writable = true;
  77. private $closed = false;
  78. private $paused = false;
  79. private $drain = false;
  80. private $callback;
  81. public function __construct($callback = null)
  82. {
  83. if ($callback !== null && !\is_callable($callback)) {
  84. throw new InvalidArgumentException('Invalid transformation callback given');
  85. }
  86. $this->callback = $callback;
  87. }
  88. public function pause()
  89. {
  90. $this->paused = true;
  91. }
  92. public function resume()
  93. {
  94. if ($this->drain) {
  95. $this->drain = false;
  96. $this->emit('drain');
  97. }
  98. $this->paused = false;
  99. }
  100. public function pipe(WritableStreamInterface $dest, array $options = array())
  101. {
  102. return Util::pipe($this, $dest, $options);
  103. }
  104. public function isReadable()
  105. {
  106. return $this->readable;
  107. }
  108. public function isWritable()
  109. {
  110. return $this->writable;
  111. }
  112. public function write($data)
  113. {
  114. if (!$this->writable) {
  115. return false;
  116. }
  117. if ($this->callback !== null) {
  118. try {
  119. $data = \call_user_func($this->callback, $data);
  120. } catch (\Exception $e) {
  121. $this->emit('error', array($e));
  122. $this->close();
  123. return false;
  124. }
  125. }
  126. $this->emit('data', array($data));
  127. if ($this->paused) {
  128. $this->drain = true;
  129. return false;
  130. }
  131. return true;
  132. }
  133. public function end($data = null)
  134. {
  135. if (!$this->writable) {
  136. return;
  137. }
  138. if (null !== $data) {
  139. $this->write($data);
  140. // return if write() already caused the stream to close
  141. if (!$this->writable) {
  142. return;
  143. }
  144. }
  145. $this->readable = false;
  146. $this->writable = false;
  147. $this->paused = true;
  148. $this->drain = false;
  149. $this->emit('end');
  150. $this->close();
  151. }
  152. public function close()
  153. {
  154. if ($this->closed) {
  155. return;
  156. }
  157. $this->readable = false;
  158. $this->writable = false;
  159. $this->closed = true;
  160. $this->paused = true;
  161. $this->drain = false;
  162. $this->callback = null;
  163. $this->emit('close');
  164. $this->removeAllListeners();
  165. }
  166. }