1<?php
2
3function resetVT100State()
4{
5    $state = array(
6        sapi_windows_vt100_support(STDIN),
7        sapi_windows_vt100_support(STDOUT),
8        sapi_windows_vt100_support(STDERR),
9    );
10    sapi_windows_vt100_support(STDIN, false);
11    sapi_windows_vt100_support(STDOUT, false);
12    sapi_windows_vt100_support(STDERR, false);
13
14    return $state;
15}
16
17function restoreVT100State(array $state)
18{
19    sapi_windows_vt100_support(STDIN, $state[0]);
20    sapi_windows_vt100_support(STDOUT, $state[1]);
21    sapi_windows_vt100_support(STDERR, $state[2]);
22}
23
24function testToStdOut()
25{
26    $state = resetVT100State();
27
28    $sampleStreams = array(
29        'STDIN (constant)'              => STDIN,
30        'STDIN (fopen)'                 => fopen('php://stdin', 'rb'),
31        'STDIN (php://fd/0)'            => fopen('php://fd/0', 'rb'),
32        'STDOUT (constant)'             => STDOUT,
33        'STDOUT (fopen)'                => fopen('php://stdout', 'wb'),
34        'STDOUT (php://fd/1)'           => fopen('php://fd/1', 'wb'),
35        'STDERR (constant)'             => STDERR,
36        'STDERR (fopen)'                => fopen('php://stderr', 'wb'),
37        'STDERR (php://fd/2)'           => fopen('php://fd/2', 'wb'),
38        'Not a stream'                  => 'foo',
39        'Invalid stream (php://temp)'   => fopen('php://temp', 'wb'),
40        'Invalid stream (php://input)'  => fopen('php://input', 'wb'),
41        'Invalid stream (php://memory)' => fopen('php://memory', 'wb'),
42        'File stream'                   => $closeMe = fopen(__FILE__, 'rb'),
43    );
44
45    foreach ($sampleStreams as $name => $stream) {
46        echo "$name:\n";
47        echo "- current value  : "; var_dump(sapi_windows_vt100_support($stream));
48        echo "- enabling VT100 : "; var_dump(sapi_windows_vt100_support($stream, true));
49        echo "- current value  : "; var_dump(sapi_windows_vt100_support($stream));
50        echo "- disabling VT100: "; var_dump(sapi_windows_vt100_support($stream, false));
51        echo "- current value  : "; var_dump(sapi_windows_vt100_support($stream));
52    }
53
54    fclose($closeMe);
55    restoreVT100State($state);
56}
57
58function testToStdErr()
59{
60    ob_start();
61    testToStdOut();
62    $result = ob_get_contents();
63    ob_end_clean();
64    fwrite(STDERR, $result);
65}
66