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\Process;
17 * UnixPipes implementation uses unix pipes as handles.
19 * @author Romain Neutron <imprec@gmail.com>
23 class UnixPipes extends AbstractPipes
30 private $haveReadSupport;
32 public function __construct($ttyMode, $ptyMode, $input, $haveReadSupport)
34 $this->ttyMode = (bool) $ttyMode;
35 $this->ptyMode = (bool) $ptyMode;
36 $this->haveReadSupport = (bool) $haveReadSupport;
38 parent::__construct($input);
41 public function __destruct()
49 public function getDescriptors()
51 if (!$this->haveReadSupport) {
52 $nullstream = fopen('/dev/null', 'c');
63 array('file', '/dev/tty', 'r'),
64 array('file', '/dev/tty', 'w'),
65 array('file', '/dev/tty', 'w'),
69 if ($this->ptyMode && Process::isPtySupported()) {
79 array('pipe', 'w'), // stdout
80 array('pipe', 'w'), // stderr
87 public function getFiles()
95 public function readAndWrite($blocking, $close = false)
100 $read = $e = array();
104 // let's have a look if something changed in streams
105 if (($r || $w) && false === $n = @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
106 // if a system call has been interrupted, forget about it, let's try again
107 // otherwise, an error occurred, let's reset pipes
108 if (!$this->hasSystemCallBeenInterrupted()) {
109 $this->pipes = array();
115 foreach ($r as $pipe) {
116 // prior PHP 5.4 the array passed to stream_select is modified and
117 // lose key association, we have to find back the key
118 $read[$type = array_search($pipe, $this->pipes, true)] = '';
121 $data = fread($pipe, self::CHUNK_SIZE);
122 $read[$type] .= $data;
123 } while (isset($data[0]) && ($close || isset($data[self::CHUNK_SIZE - 1])));
125 if (!isset($read[$type][0])) {
129 if ($close && feof($pipe)) {
131 unset($this->pipes[$type]);
141 public function haveReadSupport()
143 return $this->haveReadSupport;
149 public function areOpen()
151 return (bool) $this->pipes;