123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- <?php
- namespace Symfony\Component\Process\Pipes;
- use Symfony\Component\Process\Exception\InvalidArgumentException;
- abstract class AbstractPipes implements PipesInterface
- {
- public $pipes = [];
- private $inputBuffer = '';
- private $input;
- private $blocked = true;
- private $lastError;
-
- public function __construct($input)
- {
- if (\is_resource($input) || $input instanceof \Iterator) {
- $this->input = $input;
- } elseif (\is_string($input)) {
- $this->inputBuffer = $input;
- } else {
- $this->inputBuffer = (string) $input;
- }
- }
-
- public function close()
- {
- foreach ($this->pipes as $pipe) {
- fclose($pipe);
- }
- $this->pipes = [];
- }
-
- protected function hasSystemCallBeenInterrupted()
- {
- $lastError = $this->lastError;
- $this->lastError = null;
-
- return null !== $lastError && false !== stripos($lastError, 'interrupted system call');
- }
-
- protected function unblock()
- {
- if (!$this->blocked) {
- return;
- }
- foreach ($this->pipes as $pipe) {
- stream_set_blocking($pipe, 0);
- }
- if (\is_resource($this->input)) {
- stream_set_blocking($this->input, 0);
- }
- $this->blocked = false;
- }
-
- protected function write()
- {
- if (!isset($this->pipes[0])) {
- return;
- }
- $input = $this->input;
- if ($input instanceof \Iterator) {
- if (!$input->valid()) {
- $input = null;
- } elseif (\is_resource($input = $input->current())) {
- stream_set_blocking($input, 0);
- } elseif (!isset($this->inputBuffer[0])) {
- if (!\is_string($input)) {
- if (!is_scalar($input)) {
- throw new InvalidArgumentException(sprintf('%s yielded a value of type "%s", but only scalars and stream resources are supported', \get_class($this->input), \gettype($input)));
- }
- $input = (string) $input;
- }
- $this->inputBuffer = $input;
- $this->input->next();
- $input = null;
- } else {
- $input = null;
- }
- }
- $r = $e = [];
- $w = [$this->pipes[0]];
-
- if (false === @stream_select($r, $w, $e, 0, 0)) {
- return;
- }
- foreach ($w as $stdin) {
- if (isset($this->inputBuffer[0])) {
- $written = fwrite($stdin, $this->inputBuffer);
- $this->inputBuffer = substr($this->inputBuffer, $written);
- if (isset($this->inputBuffer[0])) {
- return [$this->pipes[0]];
- }
- }
- if ($input) {
- for (;;) {
- $data = fread($input, self::CHUNK_SIZE);
- if (!isset($data[0])) {
- break;
- }
- $written = fwrite($stdin, $data);
- $data = substr($data, $written);
- if (isset($data[0])) {
- $this->inputBuffer = $data;
- return [$this->pipes[0]];
- }
- }
- if (feof($input)) {
- if ($this->input instanceof \Iterator) {
- $this->input->next();
- } else {
- $this->input = null;
- }
- }
- }
- }
-
- if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) {
- $this->input = null;
- fclose($this->pipes[0]);
- unset($this->pipes[0]);
- } elseif (!$w) {
- return [$this->pipes[0]];
- }
- }
-
- public function handleError($type, $msg)
- {
- $this->lastError = $msg;
- }
- }
|