1 | <?php |
||
25 | class StreamOutputStream implements OutputStream |
||
26 | { |
||
27 | /** |
||
28 | * @var resource |
||
29 | */ |
||
30 | private $stream; |
||
31 | |||
32 | /** |
||
33 | * Creates the stream. |
||
34 | * |
||
35 | * @param resource $stream A stream resource. |
||
36 | */ |
||
37 | 144 | public function __construct($stream) |
|
38 | { |
||
39 | 144 | Assert::resource($stream, 'stream'); |
|
40 | |||
41 | 144 | $this->stream = $stream; |
|
42 | 144 | } |
|
43 | |||
44 | /** |
||
45 | * {@inheritdoc} |
||
46 | */ |
||
47 | 2 | public function write($string) |
|
48 | { |
||
49 | 2 | if (null === $this->stream) { |
|
50 | 1 | throw new IOException('Cannot read from a closed input.'); |
|
51 | } |
||
52 | |||
53 | 1 | if (false === fwrite($this->stream, $string)) { |
|
54 | throw new IOException('Could not write stream.'); |
||
55 | } |
||
56 | 1 | } |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 1 | public function flush() |
|
62 | { |
||
63 | 1 | if (null === $this->stream) { |
|
64 | 1 | throw new IOException('Cannot read from a closed input.'); |
|
65 | } |
||
66 | |||
67 | if (false === fflush($this->stream)) { |
||
68 | throw new IOException('Could not flush stream.'); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | 138 | public function supportsAnsi() |
|
76 | { |
||
77 | 138 | if (DIRECTORY_SEPARATOR === '\\') { |
|
78 | return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI') || 'xterm' === getenv('TERM'); |
||
79 | } |
||
80 | |||
81 | 138 | return function_exists('posix_isatty') && @posix_isatty($this->stream); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 3 | public function close() |
|
88 | { |
||
89 | 3 | if ($this->stream) { |
|
90 | 3 | @fclose($this->stream); |
|
|
|||
91 | 3 | $this->stream = null; |
|
92 | } |
||
93 | 3 | } |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function isClosed() |
||
102 | } |
||
103 |
If you suppress an error, we recommend checking for the error condition explicitly: