AbstractPipes.php
1 year ago
PipesInterface.php
1 year ago
UnixPipes.php
1 year ago
WindowsPipes.php
1 year ago
AbstractPipes.php
161 lines
| 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 | namespace Symfony\Component\Process\Pipes; |
| 12 | |
| 13 | use Symfony\Component\Process\Exception\InvalidArgumentException; |
| 14 | /** |
| 15 | * @author Romain Neutron <imprec@gmail.com> |
| 16 | * |
| 17 | * @internal |
| 18 | */ |
| 19 | abstract class AbstractPipes implements \Symfony\Component\Process\Pipes\PipesInterface |
| 20 | { |
| 21 | public $pipes = []; |
| 22 | private $inputBuffer = ''; |
| 23 | private $input; |
| 24 | private $blocked = \true; |
| 25 | private $lastError; |
| 26 | /** |
| 27 | * @param resource|string|int|float|bool|\Iterator|null $input |
| 28 | */ |
| 29 | public function __construct($input) |
| 30 | { |
| 31 | if (\is_resource($input) || $input instanceof \Iterator) { |
| 32 | $this->input = $input; |
| 33 | } elseif (\is_string($input)) { |
| 34 | $this->inputBuffer = $input; |
| 35 | } else { |
| 36 | $this->inputBuffer = (string) $input; |
| 37 | } |
| 38 | } |
| 39 | /** |
| 40 | * {@inheritdoc} |
| 41 | */ |
| 42 | public function close() |
| 43 | { |
| 44 | foreach ($this->pipes as $pipe) { |
| 45 | if (\is_resource($pipe)) { |
| 46 | fclose($pipe); |
| 47 | } |
| 48 | } |
| 49 | $this->pipes = []; |
| 50 | } |
| 51 | /** |
| 52 | * Returns true if a system call has been interrupted. |
| 53 | */ |
| 54 | protected function hasSystemCallBeenInterrupted() : bool |
| 55 | { |
| 56 | $lastError = $this->lastError; |
| 57 | $this->lastError = null; |
| 58 | // stream_select returns false when the `select` system call is interrupted by an incoming signal |
| 59 | return null !== $lastError && \false !== stripos($lastError, 'interrupted system call'); |
| 60 | } |
| 61 | /** |
| 62 | * Unblocks streams. |
| 63 | */ |
| 64 | protected function unblock() |
| 65 | { |
| 66 | if (!$this->blocked) { |
| 67 | return; |
| 68 | } |
| 69 | foreach ($this->pipes as $pipe) { |
| 70 | stream_set_blocking($pipe, 0); |
| 71 | } |
| 72 | if (\is_resource($this->input)) { |
| 73 | stream_set_blocking($this->input, 0); |
| 74 | } |
| 75 | $this->blocked = \false; |
| 76 | } |
| 77 | /** |
| 78 | * Writes input to stdin. |
| 79 | * |
| 80 | * @throws InvalidArgumentException When an input iterator yields a non supported value |
| 81 | */ |
| 82 | protected function write() : ?array |
| 83 | { |
| 84 | if (!isset($this->pipes[0])) { |
| 85 | return null; |
| 86 | } |
| 87 | $input = $this->input; |
| 88 | if ($input instanceof \Iterator) { |
| 89 | if (!$input->valid()) { |
| 90 | $input = null; |
| 91 | } elseif (\is_resource($input = $input->current())) { |
| 92 | stream_set_blocking($input, 0); |
| 93 | } elseif (!isset($this->inputBuffer[0])) { |
| 94 | if (!\is_string($input)) { |
| 95 | if (!\is_scalar($input)) { |
| 96 | throw new InvalidArgumentException(sprintf('"%s" yielded a value of type "%s", but only scalars and stream resources are supported.', get_debug_type($this->input), get_debug_type($input))); |
| 97 | } |
| 98 | $input = (string) $input; |
| 99 | } |
| 100 | $this->inputBuffer = $input; |
| 101 | $this->input->next(); |
| 102 | $input = null; |
| 103 | } else { |
| 104 | $input = null; |
| 105 | } |
| 106 | } |
| 107 | $r = $e = []; |
| 108 | $w = [$this->pipes[0]]; |
| 109 | // let's have a look if something changed in streams |
| 110 | if (\false === @stream_select($r, $w, $e, 0, 0)) { |
| 111 | return null; |
| 112 | } |
| 113 | foreach ($w as $stdin) { |
| 114 | if (isset($this->inputBuffer[0])) { |
| 115 | $written = fwrite($stdin, $this->inputBuffer); |
| 116 | $this->inputBuffer = substr($this->inputBuffer, $written); |
| 117 | if (isset($this->inputBuffer[0])) { |
| 118 | return [$this->pipes[0]]; |
| 119 | } |
| 120 | } |
| 121 | if ($input) { |
| 122 | while (\true) { |
| 123 | $data = fread($input, self::CHUNK_SIZE); |
| 124 | if (!isset($data[0])) { |
| 125 | break; |
| 126 | } |
| 127 | $written = fwrite($stdin, $data); |
| 128 | $data = substr($data, $written); |
| 129 | if (isset($data[0])) { |
| 130 | $this->inputBuffer = $data; |
| 131 | return [$this->pipes[0]]; |
| 132 | } |
| 133 | } |
| 134 | if (feof($input)) { |
| 135 | if ($this->input instanceof \Iterator) { |
| 136 | $this->input->next(); |
| 137 | } else { |
| 138 | $this->input = null; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | // no input to read on resource, buffer is empty |
| 144 | if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) { |
| 145 | $this->input = null; |
| 146 | fclose($this->pipes[0]); |
| 147 | unset($this->pipes[0]); |
| 148 | } elseif (!$w) { |
| 149 | return [$this->pipes[0]]; |
| 150 | } |
| 151 | return null; |
| 152 | } |
| 153 | /** |
| 154 | * @internal |
| 155 | */ |
| 156 | public function handleError(int $type, string $msg) |
| 157 | { |
| 158 | $this->lastError = $msg; |
| 159 | } |
| 160 | } |
| 161 |