|
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
|
|
|
const DEBUGER = 'phpdbg'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* streamName |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $streamName; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* streamMode |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $streamMode; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* $streamHandler |
|
33
|
|
|
* |
|
34
|
|
|
* @var resource |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $streamHandler; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* $maxLength |
|
40
|
|
|
* |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $maxLength; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* instanciate |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $streamName |
|
49
|
|
|
*/ |
|
50
|
6 |
|
public function __construct( |
|
51
|
|
|
string $streamName = self::STREAM_STDIN, |
|
52
|
|
|
string $streamMode = self::STREAM_MODE_WRITE_APPEND |
|
53
|
|
|
) { |
|
54
|
6 |
|
$this->streamName = (php_sapi_name() == self::DEBUGER) |
|
55
|
6 |
|
? self::STREAM_MEMORY |
|
56
|
|
|
: $streamName; |
|
57
|
6 |
|
$this->streamMode = $streamMode; |
|
58
|
6 |
|
$this->setMaxLength(1); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* return the input value |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function value(string $forcedValue = ''): string |
|
67
|
|
|
{ |
|
68
|
1 |
|
$handle = $this->getStreamHandler(); |
|
|
|
|
|
|
69
|
|
|
//readline_callback_handler_install('', function () { }); |
|
70
|
1 |
|
if (!empty($forcedValue)) { |
|
71
|
1 |
|
$this->setMaxLength(strlen($forcedValue)); |
|
72
|
1 |
|
rewind($handle); |
|
|
|
|
|
|
73
|
1 |
|
fwrite($handle, $forcedValue); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
1 |
|
$value = stream_get_contents( |
|
76
|
1 |
|
$handle, |
|
|
|
|
|
|
77
|
1 |
|
$this->getMaxLength(), |
|
78
|
|
|
0 |
|
79
|
|
|
); |
|
80
|
1 |
|
$this->closeStream(); |
|
81
|
1 |
|
return $value; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* set stream max length |
|
86
|
|
|
* |
|
87
|
|
|
* @return Input |
|
88
|
|
|
*/ |
|
89
|
6 |
|
public function setMaxLength(int $len): Input |
|
90
|
|
|
{ |
|
91
|
6 |
|
$this->maxLength = $len; |
|
92
|
6 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* returns stream handler |
|
97
|
|
|
* |
|
98
|
|
|
* @return int |
|
99
|
|
|
*/ |
|
100
|
1 |
|
protected function getMaxLength(): int |
|
101
|
|
|
{ |
|
102
|
1 |
|
return $this->maxLength; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* returns stream handler |
|
107
|
|
|
* |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
1 |
|
protected function getStreamHandler() |
|
111
|
|
|
{ |
|
112
|
1 |
|
$this->openStream(); |
|
113
|
1 |
|
return $this->streamHandler; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* returns stream name |
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
1 |
|
protected function getStreamName(): string |
|
122
|
|
|
{ |
|
123
|
1 |
|
return $this->streamName; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* open resource |
|
128
|
|
|
* |
|
129
|
|
|
* @return Input |
|
130
|
|
|
*/ |
|
131
|
1 |
|
protected function openStream(): Input |
|
132
|
|
|
{ |
|
133
|
1 |
|
if (!is_resource($this->streamHandler)) { |
|
134
|
1 |
|
$this->streamHandler = fopen( |
|
|
|
|
|
|
135
|
1 |
|
$this->getStreamName(), |
|
136
|
1 |
|
$this->streamMode, |
|
137
|
1 |
|
false |
|
138
|
|
|
); |
|
139
|
|
|
} |
|
140
|
1 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* close resource |
|
145
|
|
|
* |
|
146
|
|
|
* @return Input |
|
147
|
|
|
*/ |
|
148
|
1 |
|
protected function closeStream(): Input |
|
149
|
|
|
{ |
|
150
|
1 |
|
if (is_resource($this->streamHandler)) { |
|
151
|
1 |
|
fclose($this->streamHandler); |
|
152
|
|
|
} |
|
153
|
1 |
|
return $this; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
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.