4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Process;
14 use Symfony\Component\Process\Exception\RuntimeException;
17 * Provides a way to continuously write to the input of a Process until the InputStream is closed.
19 * @author Nicolas Grekas <p@tchwork.com>
21 class InputStream implements \IteratorAggregate
23 /** @var callable|null */
24 private $onEmpty = null;
25 private $input = array();
29 * Sets a callback that is called when the write buffer becomes empty.
31 public function onEmpty(callable $onEmpty = null)
33 $this->onEmpty = $onEmpty;
37 * Appends an input to the write buffer.
39 * @param resource|string|int|float|bool|\Traversable|null The input to append as scalar,
40 * stream resource or \Traversable
42 public function write($input)
44 if (null === $input) {
47 if ($this->isClosed()) {
48 throw new RuntimeException(sprintf('%s is closed', static::class));
50 $this->input[] = ProcessUtils::validateInput(__METHOD__, $input);
54 * Closes the write buffer.
56 public function close()
62 * Tells whether the write buffer is closed or not.
64 public function isClosed()
69 public function getIterator()
73 while ($this->open || $this->input) {
78 $current = array_shift($this->input);
80 if ($current instanceof \Iterator) {
81 foreach ($current as $cur) {
87 if (!$this->input && $this->open && null !== $onEmpty = $this->onEmpty) {
88 $this->write($onEmpty($this));