ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
PipeStdinInStdoutStdErrStreamSelect.php
Go to the documentation of this file.
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 
12 define('ERR_SELECT_FAILED', 1);
13 define('ERR_TIMEOUT', 2);
14 define('ERR_READ_FAILED', 3);
15 define('ERR_WRITE_FAILED', 4);
16 
17 $read = array(STDIN);
18 $write = array(STDOUT, STDERR);
19 
20 stream_set_blocking(STDIN, 0);
21 stream_set_blocking(STDOUT, 0);
22 stream_set_blocking(STDERR, 0);
23 
24 $out = $err = '';
25 while ($read || $write) {
26  $r = $read;
27  $w = $write;
28  $e = null;
29  $n = stream_select($r, $w, $e, 5);
30 
31  if (false === $n) {
32  die(ERR_SELECT_FAILED);
33  } elseif ($n < 1) {
34  die(ERR_TIMEOUT);
35  }
36 
37  if (in_array(STDOUT, $w) && strlen($out) > 0) {
38  $written = fwrite(STDOUT, (binary) $out, 32768);
39  if (false === $written) {
40  die(ERR_WRITE_FAILED);
41  }
42  $out = (binary) substr($out, $written);
43  }
44  if (null === $read && '' === $out) {
45  $write = array_diff($write, array(STDOUT));
46  }
47 
48  if (in_array(STDERR, $w) && strlen($err) > 0) {
49  $written = fwrite(STDERR, (binary) $err, 32768);
50  if (false === $written) {
51  die(ERR_WRITE_FAILED);
52  }
53  $err = (binary) substr($err, $written);
54  }
55  if (null === $read && '' === $err) {
56  $write = array_diff($write, array(STDERR));
57  }
58 
59  if ($r) {
60  $str = fread(STDIN, 32768);
61  if (false !== $str) {
62  $out .= $str;
63  $err .= $str;
64  }
65  if (false === $str || feof(STDIN)) {
66  $read = null;
67  if (!feof(STDIN)) {
68  die(ERR_READ_FAILED);
69  }
70  }
71  }
72 }
$w
$r
Definition: example_031.php:79
$n
Definition: RandomTest.php:80
Create styles array
The data for the language used.