1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
//! ref: composer/vendor/symfony/console/Input/Input.php
use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException;
use crate::symfony::console::exception::runtime_exception::RuntimeException;
use crate::symfony::console::input::input_definition::InputDefinition;
use indexmap::IndexMap;
use shirabe_php_shim::{PhpMixed, PhpResource};
/// Input is the base class for all concrete Input classes.
///
/// Three concrete classes are provided by default:
///
/// * `ArgvInput`: The input comes from the CLI arguments (argv)
/// * `StringInput`: The input is provided as a string
/// * `ArrayInput`: The input is provided as an array
#[derive(Debug, Clone)]
pub struct Input {
pub(crate) definition: InputDefinition,
pub(crate) stream: Option<PhpResource>,
pub(crate) options: IndexMap<String, PhpMixed>,
pub(crate) arguments: IndexMap<String, PhpMixed>,
pub(crate) interactive: bool,
}
impl Input {
pub fn new(definition: Option<InputDefinition>) -> anyhow::Result<Self> {
let mut input = Input {
definition: InputDefinition::new(vec![])?,
stream: None,
options: IndexMap::new(),
arguments: IndexMap::new(),
interactive: true,
};
match definition {
None => {
input.definition = InputDefinition::new(vec![])?;
}
Some(definition) => {
input.bind(&definition)?;
input.validate()?;
}
}
Ok(input)
}
pub fn bind(&mut self, definition: &InputDefinition) -> anyhow::Result<()> {
self.arguments = IndexMap::new();
self.options = IndexMap::new();
self.definition = definition.clone();
self.parse()?;
Ok(())
}
/// Processes command line arguments.
///
/// This is abstract in PHP; concrete subclasses provide their own `parse`.
/// Since `Input` is embedded via `inner` in the subclasses, the subclass
/// drives the parsing instead.
fn parse(&mut self) -> anyhow::Result<()> {
unreachable!("Input::parse is abstract and overridden by subclasses")
}
pub fn validate(&mut self) -> anyhow::Result<()> {
let definition = &self.definition;
let given_arguments = &self.arguments;
let missing_arguments: Vec<String> = shirabe_php_shim::array_filter(
&shirabe_php_shim::array_keys(definition.get_arguments()),
|argument: &String| {
!given_arguments.contains_key(argument)
&& definition
.get_argument(&PhpMixed::String(argument.clone()))
.map(|a| a.is_required())
.unwrap_or(false)
},
);
if !missing_arguments.is_empty() {
return Err(RuntimeException(shirabe_php_shim::RuntimeException {
message: format!(
"Not enough arguments (missing: \"{}\").",
shirabe_php_shim::implode(", ", &missing_arguments),
),
code: 0,
})
.into());
}
Ok(())
}
pub fn is_interactive(&self) -> bool {
self.interactive
}
pub fn set_interactive(&mut self, interactive: bool) {
self.interactive = interactive;
}
pub fn get_arguments(&self) -> IndexMap<String, PhpMixed> {
shirabe_php_shim::array_merge_map(
self.definition.get_argument_defaults(),
self.arguments.clone(),
)
}
pub fn get_argument(&self, name: &str) -> anyhow::Result<PhpMixed> {
if !self
.definition
.has_argument(&PhpMixed::String(name.to_string()))
{
return Err(
InvalidArgumentException(shirabe_php_shim::InvalidArgumentException {
message: format!("The \"{}\" argument does not exist.", name),
code: 0,
})
.into(),
);
}
Ok(match self.arguments.get(name) {
Some(value) => value.clone(),
None => self
.definition
.get_argument(&PhpMixed::String(name.to_string()))?
.get_default()
.clone(),
})
}
pub fn set_argument(&mut self, name: &str, value: PhpMixed) -> anyhow::Result<()> {
if !self
.definition
.has_argument(&PhpMixed::String(name.to_string()))
{
return Err(
InvalidArgumentException(shirabe_php_shim::InvalidArgumentException {
message: format!("The \"{}\" argument does not exist.", name),
code: 0,
})
.into(),
);
}
self.arguments.insert(name.to_string(), value);
Ok(())
}
pub fn has_argument(&self, name: &str) -> bool {
self.definition
.has_argument(&PhpMixed::String(name.to_string()))
}
pub fn get_options(&self) -> IndexMap<String, PhpMixed> {
shirabe_php_shim::array_merge_map(
self.definition.get_option_defaults(),
self.options.clone(),
)
}
pub fn get_option(&self, name: &str) -> anyhow::Result<PhpMixed> {
if self.definition.has_negation(name) {
let value = self.get_option(&self.definition.negation_to_name(name)?)?;
if matches!(value, PhpMixed::Null) {
return Ok(value);
}
return Ok(PhpMixed::Bool(!value.as_bool().unwrap_or(false)));
}
if !self.definition.has_option(name) {
return Err(
InvalidArgumentException(shirabe_php_shim::InvalidArgumentException {
message: format!("The \"{}\" option does not exist.", name),
code: 0,
})
.into(),
);
}
Ok(if self.options.contains_key(name) {
self.options[name].clone()
} else {
self.definition.get_option(name)?.get_default().clone()
})
}
pub fn set_option(&mut self, name: &str, value: PhpMixed) -> anyhow::Result<()> {
if self.definition.has_negation(name) {
let negated = self.definition.negation_to_name(name)?;
self.options
.insert(negated, PhpMixed::Bool(!value.as_bool().unwrap_or(false)));
return Ok(());
} else if !self.definition.has_option(name) {
return Err(
InvalidArgumentException(shirabe_php_shim::InvalidArgumentException {
message: format!("The \"{}\" option does not exist.", name),
code: 0,
})
.into(),
);
}
self.options.insert(name.to_string(), value);
Ok(())
}
pub fn has_option(&self, name: &str) -> bool {
self.definition.has_option(name) || self.definition.has_negation(name)
}
/// Escapes a token through escapeshellarg if it contains unsafe chars.
pub fn escape_token(&self, token: &str) -> String {
let mut matches: Vec<Option<String>> = vec![];
if shirabe_php_shim::preg_match("{^[\\w-]+$}", token, &mut matches) {
token.to_string()
} else {
shirabe_php_shim::escapeshellarg(token)
}
}
pub fn set_stream(&mut self, stream: PhpResource) {
self.stream = Some(stream);
}
pub fn get_stream(&self) -> Option<PhpResource> {
self.stream.clone()
}
}
|