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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
//! ref: composer/src/Composer/IO/BufferIO.php
use crate::io::ConsoleIO;
use anyhow::Result;
use shirabe_external_packages::composer::pcre::Preg;
use shirabe_external_packages::symfony::component::console::helper::HelperSet;
use shirabe_external_packages::symfony::component::console::input::InputInterface;
use shirabe_external_packages::symfony::component::console::input::StringInput;
use shirabe_external_packages::symfony::component::console::output::OutputInterface;
use shirabe_external_packages::symfony::console::formatter::OutputFormatterInterface;
use shirabe_external_packages::symfony::console::helper::QuestionHelper;
use shirabe_external_packages::symfony::console::output::StreamOutput;
use shirabe_php_shim::{
PHP_EOL, PhpMixed, RuntimeException, fopen, fseek, fwrite, rewind, stream_get_contents,
strip_tags,
};
#[derive(Debug)]
pub struct BufferIO {
pub(crate) inner: ConsoleIO,
}
impl BufferIO {
pub fn new(
input: String,
verbosity: i64,
formatter: Option<Box<dyn OutputFormatterInterface>>,
) -> Result<Self> {
let mut input_obj = StringInput::new(&input);
input_obj.set_interactive(false);
let stream = fopen("php://memory", "rw");
if matches!(stream, PhpMixed::Bool(false)) {
return Err(RuntimeException {
message: "Unable to open memory output stream".to_string(),
code: 0,
}
.into());
}
let decorated = formatter.as_ref().map_or(false, |f| f.is_decorated());
// TODO(phase-b): StreamOutput lives under `symfony::console` (not `symfony::component::console`)
// and so does not implement the `component::console::output::OutputInterface` ConsoleIO expects.
// Real fix requires unifying the two crate paths.
let _ = formatter;
let _ = StreamOutput::new(stream, verbosity, Some(decorated));
let output: Box<dyn OutputInterface> = todo!("StreamOutput as Box<dyn OutputInterface>");
// TODO(phase-b): symfony console helper modules live under both `symfony::console`
// and `symfony::component::console`; QuestionHelper::new is not yet provided.
let helpers: Vec<PhpMixed> = vec![/* PhpMixed::Object(QuestionHelper::new()) */];
let _ = std::marker::PhantomData::<QuestionHelper>;
let inner = ConsoleIO::new(
Box::new(input_obj) as Box<dyn InputInterface>,
output,
HelperSet::new(helpers),
);
Ok(Self { inner })
}
pub fn get_output(&self) -> String {
// TODO(phase-b): OutputInterface::get_stream returns PhpResource, while
// fseek/stream_get_contents take PhpMixed. Conversion is not yet defined.
let stream: PhpMixed =
todo!("PhpResource -> PhpMixed conversion for OutputInterface::get_stream");
fseek(stream.clone(), 0);
let output = stream_get_contents(stream).unwrap_or_default();
let output = Preg::replace_callback(
r"{(?<=^|\n|\x08)(.+?)(\x08+)}",
|matches: &indexmap::IndexMap<
shirabe_external_packages::composer::pcre::CaptureKey,
String,
>|
-> String {
let empty = String::new();
let g1 = matches
.get(&shirabe_external_packages::composer::pcre::CaptureKey::ByIndex(1))
.unwrap_or(&empty);
let g2 = matches
.get(&shirabe_external_packages::composer::pcre::CaptureKey::ByIndex(2))
.unwrap_or(&empty);
let pre = strip_tags(g1);
if pre.len() == g2.len() {
return String::new();
}
// TODO reverse parse the string, skipping span tags and \033\[([0-9;]+)m(.*?)\033\[0m style blobs
format!("{}\n", g1.trim_end())
},
&output,
);
// TODO(phase-b): Preg::replace_callback returns Result<String>, unwrap for now
output.unwrap_or_default()
}
pub fn set_user_inputs(&mut self, inputs: Vec<String>) -> Result<()> {
// TODO(phase-b): downcast Box<dyn InputInterface> to StreamableInputInterface.
// as_any/set_stream are not yet exposed on the InputInterface trait object.
let _ = inputs;
let _ = |i: &Box<dyn InputInterface>| -> bool {
let _ = i;
false
};
todo!("port BufferIO::set_user_inputs once StreamableInputInterface downcast is available")
}
fn create_stream(&self, inputs: Vec<String>) -> Result<PhpMixed> {
let stream = fopen("php://memory", "r+");
if matches!(stream, PhpMixed::Bool(false)) {
return Err(RuntimeException {
message: "Unable to open memory output stream".to_string(),
code: 0,
}
.into());
}
for input in inputs {
fwrite(stream.clone(), &format!("{}{}", input, PHP_EOL), -1);
}
rewind(stream.clone());
Ok(stream)
}
}
// TODO(phase-b): PHP `class BufferIO extends ConsoleIO` — delegate all IOInterface,
// LoggerInterface, and BaseIO methods to `self.inner` (ConsoleIO).
impl shirabe_external_packages::psr::log::LoggerInterface for BufferIO {
fn emergency(&self, message: &str, context: &[(&str, &str)]) {
self.inner.emergency(message, context)
}
fn alert(&self, message: &str, context: &[(&str, &str)]) {
self.inner.alert(message, context)
}
fn critical(&self, message: &str, context: &[(&str, &str)]) {
self.inner.critical(message, context)
}
fn error(&self, message: &str, context: &[(&str, &str)]) {
self.inner.error(message, context)
}
fn warning(&self, message: &str, context: &[(&str, &str)]) {
self.inner.warning(message, context)
}
fn notice(&self, message: &str, context: &[(&str, &str)]) {
self.inner.notice(message, context)
}
fn info(&self, message: &str, context: &[(&str, &str)]) {
self.inner.info(message, context)
}
fn debug(&self, message: &str, context: &[(&str, &str)]) {
self.inner.debug(message, context)
}
fn log(&self, level: &str, message: &str, context: &[(&str, &str)]) {
self.inner.log(level, message, context)
}
}
impl crate::io::IOInterface for BufferIO {
fn is_interactive(&self) -> bool {
self.inner.is_interactive()
}
fn is_verbose(&self) -> bool {
self.inner.is_verbose()
}
fn is_very_verbose(&self) -> bool {
self.inner.is_very_verbose()
}
fn is_debug(&self) -> bool {
self.inner.is_debug()
}
fn is_decorated(&self) -> bool {
self.inner.is_decorated()
}
fn write3(&self, message: &str, newline: bool, verbosity: i64) {
self.inner.write3(message, newline, verbosity)
}
fn write_error3(&self, message: &str, newline: bool, verbosity: i64) {
self.inner.write_error3(message, newline, verbosity)
}
fn write_raw3(&self, message: &str, newline: bool, verbosity: i64) {
self.inner.write_raw3(message, newline, verbosity)
}
fn write_error_raw3(&self, message: &str, newline: bool, verbosity: i64) {
self.inner.write_error_raw3(message, newline, verbosity)
}
fn overwrite4(&self, message: &str, newline: bool, size: Option<i64>, verbosity: i64) {
self.inner.overwrite4(message, newline, size, verbosity)
}
fn overwrite_error4(&self, message: &str, newline: bool, size: Option<i64>, verbosity: i64) {
self.inner
.overwrite_error4(message, newline, size, verbosity)
}
fn ask(&self, question: String, default: PhpMixed) -> PhpMixed {
self.inner.ask(question, default)
}
fn ask_confirmation(&self, question: String, default: bool) -> bool {
self.inner.ask_confirmation(question, default)
}
fn ask_and_validate(
&self,
question: String,
validator: Box<dyn Fn(PhpMixed) -> PhpMixed>,
attempts: Option<i64>,
default: PhpMixed,
) -> PhpMixed {
self.inner
.ask_and_validate(question, validator, attempts, default)
}
fn ask_and_hide_answer(&self, question: String) -> Option<String> {
self.inner.ask_and_hide_answer(question)
}
fn select(
&self,
question: String,
choices: Vec<String>,
default: PhpMixed,
attempts: PhpMixed,
error_message: String,
multiselect: bool,
) -> PhpMixed {
self.inner.select(
question,
choices,
default,
attempts,
error_message,
multiselect,
)
}
fn get_authentications(
&self,
) -> indexmap::IndexMap<String, indexmap::IndexMap<String, Option<String>>> {
self.inner.get_authentications()
}
fn has_authentication(&self, repository_name: &str) -> bool {
self.inner.has_authentication(repository_name)
}
fn get_authentication(
&self,
repository_name: &str,
) -> indexmap::IndexMap<String, Option<String>> {
self.inner.get_authentication(repository_name)
}
fn set_authentication(
&mut self,
repository_name: String,
username: String,
password: Option<String>,
) {
self.inner
.set_authentication(repository_name, username, password)
}
fn load_configuration(&mut self, config: &mut crate::config::Config) -> anyhow::Result<()> {
self.inner.load_configuration(config)
}
}
impl crate::io::BaseIO for BufferIO {
fn authentications(
&self,
) -> &indexmap::IndexMap<String, indexmap::IndexMap<String, Option<String>>> {
self.inner.authentications()
}
fn authentications_mut(
&mut self,
) -> &mut indexmap::IndexMap<String, indexmap::IndexMap<String, Option<String>>> {
self.inner.authentications_mut()
}
}
|