1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace League\CLImate\Util\Reader; |
4
|
|
|
|
5
|
|
|
use League\CLImate\Exceptions\RuntimeException; |
6
|
|
|
use Seld\CliPrompt\CliPrompt; |
7
|
|
|
|
8
|
|
|
class Stdin implements ReaderInterface |
9
|
|
|
{ |
10
|
|
|
protected $stdIn = false; |
11
|
|
|
|
12
|
|
|
private $interactive = true; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Read the line typed in by the user |
16
|
|
|
* |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
public function line() |
20
|
|
|
{ |
21
|
|
|
if (!$this->interactive) { |
22
|
|
|
return ""; |
23
|
|
|
} |
24
|
|
|
return trim(fgets($this->getStdIn(), 1024)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Read from STDIN until EOF (^D) is reached |
29
|
|
|
* |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function multiLine() |
33
|
|
|
{ |
34
|
|
|
if (!$this->interactive) { |
35
|
|
|
return ""; |
36
|
|
|
} |
37
|
|
|
return trim(stream_get_contents($this->getStdIn())); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
|
|
public function isIteractive(): bool |
44
|
|
|
{ |
45
|
|
|
return $this->interactive; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritdoc |
50
|
|
|
*/ |
51
|
|
|
public function setInteractive(bool $interactive): void |
52
|
|
|
{ |
53
|
|
|
$this->interactive = $interactive; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Read one character |
58
|
|
|
* |
59
|
|
|
* @param int $count |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function char($count = 1) |
64
|
|
|
{ |
65
|
|
|
if (!$this->interactive) { |
66
|
|
|
return ""; |
67
|
|
|
} |
68
|
|
|
return fread($this->getStdIn(), $count); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Read the line, but hide what the user is typing |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function hidden() |
77
|
|
|
{ |
78
|
|
|
return CliPrompt::hiddenPrompt(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return a valid STDIN, even if it previously EOF'ed |
83
|
|
|
* |
84
|
|
|
* Lazily re-opens STDIN after hitting an EOF |
85
|
|
|
* |
86
|
|
|
* @return resource |
87
|
|
|
* @throws RuntimeException |
88
|
|
|
*/ |
89
|
|
|
protected function getStdIn() |
90
|
|
|
{ |
91
|
|
|
if ($this->stdIn && !feof($this->stdIn)) { |
92
|
|
|
return $this->stdIn; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
$this->setStdIn(); |
97
|
|
|
} catch (\Error $e) { |
98
|
|
|
throw new RuntimeException('Unable to read from STDIN', 0, $e); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->stdIn; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Attempt to set the stdin property |
106
|
|
|
* |
107
|
|
|
* @return void |
108
|
|
|
* @throws RuntimeException |
109
|
|
|
*/ |
110
|
|
|
protected function setStdIn() |
111
|
|
|
{ |
112
|
|
|
if ($this->stdIn !== false) { |
113
|
|
|
fclose($this->stdIn); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$this->stdIn = fopen('php://stdin', 'r'); |
117
|
|
|
|
118
|
|
|
if (!$this->stdIn) { |
119
|
|
|
throw new RuntimeException('Unable to read from STDIN'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|