InputStream.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Process;
  11. use Symfony\Component\Process\Exception\RuntimeException;
  12. /**
  13. * Provides a way to continuously write to the input of a Process until the InputStream is closed.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. *
  17. * @implements \IteratorAggregate<int, string>
  18. */
  19. class InputStream implements \IteratorAggregate
  20. {
  21. private ?\Closure $onEmpty = null;
  22. private array $input = [];
  23. private bool $open = true;
  24. /**
  25. * Sets a callback that is called when the write buffer becomes empty.
  26. *
  27. * @return void
  28. */
  29. public function onEmpty(?callable $onEmpty = null)
  30. {
  31. $this->onEmpty = null !== $onEmpty ? $onEmpty(...) : null;
  32. }
  33. /**
  34. * Appends an input to the write buffer.
  35. *
  36. * @param resource|string|int|float|bool|\Traversable|null $input The input to append as scalar,
  37. * stream resource or \Traversable
  38. *
  39. * @return void
  40. */
  41. public function write(mixed $input)
  42. {
  43. if (null === $input) {
  44. return;
  45. }
  46. if ($this->isClosed()) {
  47. throw new RuntimeException(sprintf('"%s" is closed.', static::class));
  48. }
  49. $this->input[] = ProcessUtils::validateInput(__METHOD__, $input);
  50. }
  51. /**
  52. * Closes the write buffer.
  53. *
  54. * @return void
  55. */
  56. public function close()
  57. {
  58. $this->open = false;
  59. }
  60. /**
  61. * Tells whether the write buffer is closed or not.
  62. *
  63. * @return bool
  64. */
  65. public function isClosed()
  66. {
  67. return !$this->open;
  68. }
  69. public function getIterator(): \Traversable
  70. {
  71. $this->open = true;
  72. while ($this->open || $this->input) {
  73. if (!$this->input) {
  74. yield '';
  75. continue;
  76. }
  77. $current = array_shift($this->input);
  78. if ($current instanceof \Iterator) {
  79. yield from $current;
  80. } else {
  81. yield $current;
  82. }
  83. if (!$this->input && $this->open && null !== $onEmpty = $this->onEmpty) {
  84. $this->write($onEmpty($this));
  85. }
  86. }
  87. }
  88. }