1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Component\Console; |
4
|
|
|
|
5
|
|
|
class Input |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
const STREAM_STDIN = 'php://stdin'; |
9
|
|
|
const STREAM_MEMORY = 'php://memory'; |
10
|
|
|
const STREAM_TEMP = 'php://temp'; |
11
|
|
|
const STREAM_MODE_READ = 'r'; |
12
|
|
|
const STREAM_MODE_APPEND = 'r+'; |
13
|
|
|
const STREAM_MODE_WRITE = 'w'; |
14
|
|
|
const STREAM_MODE_WRITE_APPEND = 'w+'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* streamName |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $streamName; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* streamMode |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $streamMode; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* $streamHandler |
32
|
|
|
* |
33
|
|
|
* @var resource |
34
|
|
|
*/ |
35
|
|
|
protected $streamHandler; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* $maxLength |
39
|
|
|
* |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
protected $maxLength; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* instanciate |
46
|
|
|
* |
47
|
|
|
* @param string $streamName |
48
|
|
|
*/ |
49
|
6 |
|
public function __construct( |
50
|
|
|
string $streamName = self::STREAM_STDIN, |
51
|
|
|
string $streamMode = self::STREAM_MODE_WRITE_APPEND |
52
|
|
|
) { |
53
|
6 |
|
$this->streamName = $streamName; |
54
|
6 |
|
$this->streamMode = $streamMode; |
55
|
6 |
|
$this->setMaxLength(1); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* return the input value |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
1 |
|
public function value(string $forcedValue = ''): string |
64
|
|
|
{ |
65
|
1 |
|
$handle = $this->getStreamHandler(); |
|
|
|
|
66
|
|
|
//readline_callback_handler_install('', function () { }); |
67
|
1 |
|
if (!empty($forcedValue)) { |
68
|
1 |
|
$this->setMaxLength(strlen($forcedValue)); |
69
|
1 |
|
rewind($handle); |
|
|
|
|
70
|
1 |
|
fwrite($handle, $forcedValue); |
|
|
|
|
71
|
|
|
} |
72
|
1 |
|
$value = stream_get_contents( |
73
|
1 |
|
$handle, |
|
|
|
|
74
|
1 |
|
$this->getMaxLength(), |
75
|
|
|
0 |
76
|
|
|
); |
77
|
1 |
|
$this->closeStream(); |
78
|
1 |
|
return $value; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* set stream max length |
83
|
|
|
* |
84
|
|
|
* @return Input |
85
|
|
|
*/ |
86
|
6 |
|
public function setMaxLength(int $len): Input |
87
|
|
|
{ |
88
|
6 |
|
$this->maxLength = $len; |
89
|
6 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* returns stream handler |
94
|
|
|
* |
95
|
|
|
* @return int |
96
|
|
|
*/ |
97
|
1 |
|
protected function getMaxLength(): int |
98
|
|
|
{ |
99
|
1 |
|
return $this->maxLength; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* returns stream handler |
104
|
|
|
* |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
1 |
|
protected function getStreamHandler() |
108
|
|
|
{ |
109
|
1 |
|
$this->openStream(); |
110
|
1 |
|
return $this->streamHandler; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* returns stream name |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
1 |
|
protected function getStreamName(): string |
119
|
|
|
{ |
120
|
1 |
|
return $this->streamName; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* open resource |
125
|
|
|
* |
126
|
|
|
* @return Input |
127
|
|
|
*/ |
128
|
1 |
|
protected function openStream(): Input |
129
|
|
|
{ |
130
|
1 |
|
if (!is_resource($this->streamHandler)) { |
131
|
1 |
|
$this->streamHandler = fopen( |
|
|
|
|
132
|
1 |
|
$this->getStreamName(), |
133
|
1 |
|
$this->streamMode, |
134
|
1 |
|
false |
135
|
|
|
); |
136
|
|
|
} |
137
|
1 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* close resource |
142
|
|
|
* |
143
|
|
|
* @return Input |
144
|
|
|
*/ |
145
|
1 |
|
protected function closeStream(): Input |
146
|
|
|
{ |
147
|
1 |
|
if (is_resource($this->streamHandler)) { |
148
|
1 |
|
fclose($this->streamHandler); |
149
|
|
|
} |
150
|
1 |
|
return $this; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.