1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the webmozart/console package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
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
|
|
|
namespace Webmozart\Console\IO\OutputStream; |
13
|
|
|
|
14
|
|
|
use Webmozart\Assert\Assert; |
15
|
|
|
use Webmozart\Console\Api\IO\IOException; |
16
|
|
|
use Webmozart\Console\Api\IO\OutputStream; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* An output stream that writes to a PHP stream. |
20
|
|
|
* |
21
|
|
|
* @since 1.0 |
22
|
|
|
* |
23
|
|
|
* @author Bernhard Schussek <[email protected]> |
24
|
|
|
*/ |
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() |
99
|
|
|
{ |
100
|
|
|
return null === $this->stream; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: