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        'Invalid stream (php://temp)'   => fopen('php://temp', 'wb'),
39        'Invalid stream (php://input)'  => fopen('php://input', 'wb'),
40        'Invalid stream (php://memory)' => fopen('php://memory', 'wb'),
41        'File stream'                   => $closeMe = fopen(__FILE__, 'rb'),
42    );
43
44    foreach ($sampleStreams as $name => $stream) {
45        echo "$name:\n";
46        echo "- current value  : "; var_dump(sapi_windows_vt100_support($stream));
47        echo "- enabling VT100 : "; var_dump(sapi_windows_vt100_support($stream, true));
48        echo "- current value  : "; var_dump(sapi_windows_vt100_support($stream));
49        echo "- disabling VT100: "; var_dump(sapi_windows_vt100_support($stream, false));
50        echo "- current value  : "; var_dump(sapi_windows_vt100_support($stream));
51    }
52
53    fclose($closeMe);
54    restoreVT100State($state);
55}
56
57function testToStdErr()
58{
59    ob_start();
60    testToStdOut();
61    $result = ob_get_contents();
62    ob_end_clean();
63    fwrite(STDERR, $result);
64}
65