Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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