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@17
|
23 /** @var callable|null */
|
Chris@0
|
24 private $onEmpty = null;
|
Chris@17
|
25 private $input = [];
|
Chris@0
|
26 private $open = true;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * Sets a callback that is called when the write buffer becomes empty.
|
Chris@0
|
30 */
|
Chris@0
|
31 public function onEmpty(callable $onEmpty = null)
|
Chris@0
|
32 {
|
Chris@0
|
33 $this->onEmpty = $onEmpty;
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Appends an input to the write buffer.
|
Chris@0
|
38 *
|
Chris@17
|
39 * @param resource|string|int|float|bool|\Traversable|null $input The input to append as scalar,
|
Chris@17
|
40 * stream resource or \Traversable
|
Chris@0
|
41 */
|
Chris@0
|
42 public function write($input)
|
Chris@0
|
43 {
|
Chris@0
|
44 if (null === $input) {
|
Chris@0
|
45 return;
|
Chris@0
|
46 }
|
Chris@0
|
47 if ($this->isClosed()) {
|
Chris@0
|
48 throw new RuntimeException(sprintf('%s is closed', static::class));
|
Chris@0
|
49 }
|
Chris@0
|
50 $this->input[] = ProcessUtils::validateInput(__METHOD__, $input);
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * Closes the write buffer.
|
Chris@0
|
55 */
|
Chris@0
|
56 public function close()
|
Chris@0
|
57 {
|
Chris@0
|
58 $this->open = false;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * Tells whether the write buffer is closed or not.
|
Chris@0
|
63 */
|
Chris@0
|
64 public function isClosed()
|
Chris@0
|
65 {
|
Chris@0
|
66 return !$this->open;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 public function getIterator()
|
Chris@0
|
70 {
|
Chris@0
|
71 $this->open = true;
|
Chris@0
|
72
|
Chris@0
|
73 while ($this->open || $this->input) {
|
Chris@0
|
74 if (!$this->input) {
|
Chris@0
|
75 yield '';
|
Chris@0
|
76 continue;
|
Chris@0
|
77 }
|
Chris@0
|
78 $current = array_shift($this->input);
|
Chris@0
|
79
|
Chris@0
|
80 if ($current instanceof \Iterator) {
|
Chris@0
|
81 foreach ($current as $cur) {
|
Chris@0
|
82 yield $cur;
|
Chris@0
|
83 }
|
Chris@0
|
84 } else {
|
Chris@0
|
85 yield $current;
|
Chris@0
|
86 }
|
Chris@0
|
87 if (!$this->input && $this->open && null !== $onEmpty = $this->onEmpty) {
|
Chris@0
|
88 $this->write($onEmpty($this));
|
Chris@0
|
89 }
|
Chris@0
|
90 }
|
Chris@0
|
91 }
|
Chris@0
|
92 }
|