|
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\Api\IO; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* The console input. |
|
16
|
|
|
* |
|
17
|
|
|
* This class wraps an input stream and adds convenience functionality for |
|
18
|
|
|
* reading that stream. |
|
19
|
|
|
* |
|
20
|
|
|
* @since 1.0 |
|
21
|
|
|
* |
|
22
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class Input |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var InputStream |
|
28
|
|
|
*/ |
|
29
|
|
|
private $stream; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var bool |
|
33
|
|
|
*/ |
|
34
|
|
|
private $interactive = true; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Creates an input for the given input stream. |
|
38
|
|
|
* |
|
39
|
|
|
* @param InputStream $stream The input stream. |
|
40
|
|
|
*/ |
|
41
|
238 |
|
public function __construct(InputStream $stream) |
|
42
|
|
|
{ |
|
43
|
238 |
|
$this->stream = $stream; |
|
44
|
238 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Reads the given amount of characters from the input stream. |
|
48
|
|
|
* |
|
49
|
|
|
* @param int $length The number of characters to read. |
|
50
|
|
|
* @param string $default The default to return if interaction is disabled. |
|
|
|
|
|
|
51
|
|
|
* |
|
52
|
|
|
* @return string The characters read from the input stream. |
|
|
|
|
|
|
53
|
|
|
* |
|
54
|
|
|
* @throws IOException If reading fails or if the input stream is closed. |
|
55
|
|
|
*/ |
|
56
|
4 |
|
public function read($length, $default = null) |
|
57
|
|
|
{ |
|
58
|
4 |
|
if (!$this->interactive) { |
|
59
|
1 |
|
return $default; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
3 |
|
return $this->stream->read($length); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Reads a line from the input stream. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $default The default to return if interaction is disabled. |
|
|
|
|
|
|
69
|
|
|
* @param int $length The maximum number of characters to read. If |
|
|
|
|
|
|
70
|
|
|
* `null`, all characters up to the first newline are |
|
71
|
|
|
* returned. |
|
72
|
|
|
* |
|
73
|
|
|
* @return string The characters read from the input stream. |
|
|
|
|
|
|
74
|
|
|
* |
|
75
|
|
|
* @throws IOException If reading fails or if the input stream is closed. |
|
76
|
|
|
*/ |
|
77
|
13 |
|
public function readLine($default = null, $length = null) |
|
78
|
|
|
{ |
|
79
|
13 |
|
if (!$this->interactive) { |
|
80
|
1 |
|
return $default; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
12 |
|
return $this->stream->readLine($length); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Closes the input. |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function close() |
|
90
|
|
|
{ |
|
91
|
1 |
|
$this->stream->close(); |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns whether the input is closed. |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool Returns `true` if the input is closed and `false` |
|
98
|
|
|
* otherwise. |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function isClosed() |
|
101
|
|
|
{ |
|
102
|
1 |
|
return $this->stream->isClosed(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Sets the underlying stream. |
|
107
|
|
|
* |
|
108
|
|
|
* @param InputStream $stream The input stream. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function setStream(InputStream $stream) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->stream = $stream; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns the underlying stream. |
|
117
|
|
|
* |
|
118
|
|
|
* @return InputStream The input stream. |
|
119
|
|
|
*/ |
|
120
|
4 |
|
public function getStream() |
|
121
|
|
|
{ |
|
122
|
4 |
|
return $this->stream; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Enables or disables interaction with the user. |
|
127
|
|
|
* |
|
128
|
|
|
* @param bool $interactive Whether the inputmay interact with the user. If |
|
129
|
|
|
* set to `false`, all calls to {@link read()} and |
|
130
|
|
|
* {@link readLine()} will immediately return the |
|
131
|
|
|
* default value. |
|
132
|
|
|
*/ |
|
133
|
5 |
|
public function setInteractive($interactive) |
|
134
|
|
|
{ |
|
135
|
5 |
|
$this->interactive = (bool) $interactive; |
|
136
|
5 |
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Returns whether the user may be asked for input. |
|
140
|
|
|
* |
|
141
|
|
|
* @return bool Returns `true` if the user may be asked for input and |
|
142
|
|
|
* `false` otherwise. |
|
143
|
|
|
*/ |
|
144
|
3 |
|
public function isInteractive() |
|
145
|
|
|
{ |
|
146
|
3 |
|
return $this->interactive; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.