Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / process / Pipes / AbstractPipes.php
index 1a94755bd71c1c74d6481acb087abb60f18fc20a..4c67d5b82c31a24113ef8a4223d9f12f1cc50d70 100644 (file)
@@ -11,6 +11,8 @@
 
 namespace Symfony\Component\Process\Pipes;
 
+use Symfony\Component\Process\Exception\InvalidArgumentException;
+
 /**
  * @author Romain Neutron <imprec@gmail.com>
  *
@@ -23,14 +25,14 @@ abstract class AbstractPipes implements PipesInterface
 
     /** @var string */
     private $inputBuffer = '';
-    /** @var resource|null */
+    /** @var resource|scalar|\Iterator|null */
     private $input;
     /** @var bool */
     private $blocked = true;
 
     public function __construct($input)
     {
-        if (is_resource($input)) {
+        if (is_resource($input) || $input instanceof \Iterator) {
             $this->input = $input;
         } elseif (is_string($input)) {
             $this->inputBuffer = $input;
@@ -75,7 +77,7 @@ abstract class AbstractPipes implements PipesInterface
         foreach ($this->pipes as $pipe) {
             stream_set_blocking($pipe, 0);
         }
-        if (null !== $this->input) {
+        if (is_resource($this->input)) {
             stream_set_blocking($this->input, 0);
         }
 
@@ -84,6 +86,8 @@ abstract class AbstractPipes implements PipesInterface
 
     /**
      * Writes input to stdin.
+     *
+     * @throws InvalidArgumentException When an input iterator yields a non supported value
      */
     protected function write()
     {
@@ -91,6 +95,27 @@ abstract class AbstractPipes implements PipesInterface
             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 = array();
         $w = array($this->pipes[0]);
 
@@ -123,15 +148,18 @@ abstract class AbstractPipes implements PipesInterface
                     }
                 }
                 if (feof($input)) {
-                    // no more data to read on input resource
-                    // use an empty buffer in the next reads
-                    $this->input = null;
+                    if ($this->input instanceof \Iterator) {
+                        $this->input->next();
+                    } else {
+                        $this->input = null;
+                    }
                 }
             }
         }
 
         // no input to read on resource, buffer is empty
-        if (null === $this->input && !isset($this->inputBuffer[0])) {
+        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) {