4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Process\Pipes;
14 use Symfony\Component\Process\Exception\InvalidArgumentException;
17 * @author Romain Neutron <imprec@gmail.com>
21 abstract class AbstractPipes implements PipesInterface
24 public $pipes = array();
27 private $inputBuffer = '';
28 /** @var resource|scalar|\Iterator|null */
31 private $blocked = true;
33 public function __construct($input)
35 if (is_resource($input) || $input instanceof \Iterator) {
36 $this->input = $input;
37 } elseif (is_string($input)) {
38 $this->inputBuffer = $input;
40 $this->inputBuffer = (string) $input;
47 public function close()
49 foreach ($this->pipes as $pipe) {
52 $this->pipes = array();
56 * Returns true if a system call has been interrupted.
60 protected function hasSystemCallBeenInterrupted()
62 $lastError = error_get_last();
64 // stream_select returns false when the `select` system call is interrupted by an incoming signal
65 return isset($lastError['message']) && false !== stripos($lastError['message'], 'interrupted system call');
71 protected function unblock()
73 if (!$this->blocked) {
77 foreach ($this->pipes as $pipe) {
78 stream_set_blocking($pipe, 0);
80 if (is_resource($this->input)) {
81 stream_set_blocking($this->input, 0);
84 $this->blocked = false;
88 * Writes input to stdin.
90 * @throws InvalidArgumentException When an input iterator yields a non supported value
92 protected function write()
94 if (!isset($this->pipes[0])) {
97 $input = $this->input;
99 if ($input instanceof \Iterator) {
100 if (!$input->valid()) {
102 } elseif (is_resource($input = $input->current())) {
103 stream_set_blocking($input, 0);
104 } elseif (!isset($this->inputBuffer[0])) {
105 if (!is_string($input)) {
106 if (!is_scalar($input)) {
107 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)));
109 $input = (string) $input;
111 $this->inputBuffer = $input;
112 $this->input->next();
120 $w = array($this->pipes[0]);
122 // let's have a look if something changed in streams
123 if (false === $n = @stream_select($r, $w, $e, 0, 0)) {
127 foreach ($w as $stdin) {
128 if (isset($this->inputBuffer[0])) {
129 $written = fwrite($stdin, $this->inputBuffer);
130 $this->inputBuffer = substr($this->inputBuffer, $written);
131 if (isset($this->inputBuffer[0])) {
132 return array($this->pipes[0]);
138 $data = fread($input, self::CHUNK_SIZE);
139 if (!isset($data[0])) {
142 $written = fwrite($stdin, $data);
143 $data = substr($data, $written);
144 if (isset($data[0])) {
145 $this->inputBuffer = $data;
147 return array($this->pipes[0]);
151 if ($this->input instanceof \Iterator) {
152 $this->input->next();
160 // no input to read on resource, buffer is empty
161 if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) {
163 fclose($this->pipes[0]);
164 unset($this->pipes[0]);
166 return array($this->pipes[0]);