annotate vendor/symfony/process/InputStream.php @ 0:4c8ae668cc8c

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