added knp-snappy package for Jerry's UB04 work
[openemr.git] / vendor / symfony / process / InputStream.php
blob831b10932599d4663e0dd157f6a5a3b261fdad8a
1 <?php
3 /*
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;
16 /**
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 private $onEmpty = null;
24 private $input = array();
25 private $open = true;
27 /**
28 * Sets a callback that is called when the write buffer becomes empty.
30 public function onEmpty(callable $onEmpty = null)
32 $this->onEmpty = $onEmpty;
35 /**
36 * Appends an input to the write buffer.
38 * @param resource|scalar|\Traversable|null The input to append as stream resource, scalar or \Traversable
40 public function write($input)
42 if (null === $input) {
43 return;
45 if ($this->isClosed()) {
46 throw new RuntimeException(sprintf('%s is closed', static::class));
48 $this->input[] = ProcessUtils::validateInput(__METHOD__, $input);
51 /**
52 * Closes the write buffer.
54 public function close()
56 $this->open = false;
59 /**
60 * Tells whether the write buffer is closed or not.
62 public function isClosed()
64 return !$this->open;
67 public function getIterator()
69 $this->open = true;
71 while ($this->open || $this->input) {
72 if (!$this->input) {
73 yield '';
74 continue;
76 $current = array_shift($this->input);
78 if ($current instanceof \Iterator) {
79 foreach ($current as $cur) {
80 yield $cur;
82 } else {
83 yield $current;
85 if (!$this->input && $this->open && null !== $onEmpty = $this->onEmpty) {
86 $this->write($onEmpty($this));