aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/console/descriptor/xml_descriptor.rs
blob: fc2b948321f9d0fd179d34912645d4b7517cfe55 (plain)
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! ref: composer/vendor/symfony/console/Descriptor/XmlDescriptor.php

use crate::symfony::console::application::Application;
use crate::symfony::console::command::command::Command;
use crate::symfony::console::descriptor::application_description::ApplicationDescription;
use crate::symfony::console::descriptor::descriptor::Descriptor;
use crate::symfony::console::descriptor::descriptor_interface::{
    DescribableObject, DescriptorInterface,
};
use crate::symfony::console::input::input_argument::InputArgument;
use crate::symfony::console::input::input_definition::InputDefinition;
use crate::symfony::console::input::input_option::InputOption;
use crate::symfony::console::output::output_interface::OutputInterface;
use indexmap::IndexMap;
use shirabe_php_shim::{DOMDocument, DOMNode, PhpMixed};

/// XML descriptor.
///
/// @internal
#[derive(Debug, Default)]
pub struct XmlDescriptor {
    output: Option<std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>>,
}

impl XmlDescriptor {
    pub fn get_input_definition_document(&self, definition: &InputDefinition) -> DOMDocument {
        let dom = DOMDocument::new("1.0", "UTF-8");
        let definition_xml = dom.append_child(dom.create_element("definition"));

        let arguments_xml = definition_xml.append_child(dom.create_element("arguments"));
        for argument in definition.get_arguments().values() {
            let argument_xml = self.get_input_argument_document(argument);
            self.append_document(&arguments_xml, &argument_xml.as_node());
        }

        let options_xml = definition_xml.append_child(dom.create_element("options"));
        for option in definition.get_options().values() {
            let option_xml = self.get_input_option_document(option);
            self.append_document(&options_xml, &option_xml.as_node());
        }

        dom
    }

    pub fn get_command_document(&self, command: &dyn Command, short: bool) -> DOMDocument {
        let dom = DOMDocument::new("1.0", "UTF-8");
        let command_xml = dom.append_child(dom.create_element("command"));

        let name = command.get_name().unwrap_or_default();
        command_xml.set_attribute("id", &name);
        command_xml.set_attribute("name", &name);
        command_xml.set_attribute("hidden", if command.is_hidden() { "1" } else { "0" });

        let usages_xml = command_xml.append_child(dom.create_element("usages"));

        let description_xml = command_xml.append_child(dom.create_element("description"));
        description_xml.append_child(dom.create_text_node(&shirabe_php_shim::str_replace(
            "\n",
            "\n ",
            &command.get_description(),
        )));

        if short {
            for usage in command.get_aliases() {
                usages_xml.append_child(dom.create_element_with_value("usage", &usage));
            }
        } else {
            command.merge_application_definition(false);

            let mut usages = vec![command.get_synopsis(false)];
            usages.extend(command.get_aliases());
            usages.extend(command.get_usages());
            for usage in usages {
                usages_xml.append_child(dom.create_element_with_value("usage", &usage));
            }

            let help_xml = command_xml.append_child(dom.create_element("help"));
            help_xml.append_child(dom.create_text_node(&shirabe_php_shim::str_replace(
                "\n",
                "\n ",
                &command.get_processed_help(),
            )));

            let command_definition = command.get_definition().clone();
            let definition_xml = self.get_input_definition_document(&command_definition);
            let definition_node = definition_xml
                .get_elements_by_tag_name("definition")
                .item(0)
                .expect("input definition document always contains a <definition> element");
            self.append_document(&command_xml, &definition_node);
        }

        dom
    }

    pub fn get_application_document(
        &self,
        application: std::rc::Rc<std::cell::RefCell<dyn Application>>,
        namespace: Option<String>,
        short: bool,
    ) -> DOMDocument {
        let dom = DOMDocument::new("1.0", "UTF-8");
        let root_xml = dom.append_child(dom.create_element("symfony"));

        let app_name = application.borrow().get_name();
        if app_name != "UNKNOWN" {
            root_xml.set_attribute("name", &app_name);
            let app_version = application.borrow().get_version();
            if app_version != "UNKNOWN" {
                root_xml.set_attribute("version", &app_version);
            }
        }

        let commands_xml = root_xml.append_child(dom.create_element("commands"));

        let mut description =
            ApplicationDescription::new(application.clone(), namespace.clone(), true);

        if let Some(ref namespace) = namespace {
            commands_xml.set_attribute("namespace", namespace);
        }

        let command_list: Vec<_> = description
            .get_commands()
            .values()
            .map(|c| c.borrow().clone_box())
            .collect();
        for command in command_list {
            let command_xml = self.get_command_document(command.as_ref(), short);
            self.append_document(&commands_xml, &command_xml.as_node());
        }

        if namespace.is_none() {
            let namespaces_xml = root_xml.append_child(dom.create_element("namespaces"));

            let namespaces = description.get_namespaces();
            for namespace_description in namespaces.values() {
                let namespace_array_xml =
                    namespaces_xml.append_child(dom.create_element("namespace"));
                let id = match namespace_description.get("id") {
                    Some(PhpMixed::String(s)) => s.as_str(),
                    _ => "",
                };
                namespace_array_xml.set_attribute("id", id);

                if let Some(PhpMixed::List(names)) = namespace_description.get("commands") {
                    for name in names {
                        let command_xml =
                            namespace_array_xml.append_child(dom.create_element("command"));
                        command_xml
                            .append_child(dom.create_text_node(name.as_string().unwrap_or("")));
                    }
                }
            }
        }

        dom
    }

    fn describe_input_argument(
        &mut self,
        argument: &InputArgument,
        _options: IndexMap<String, PhpMixed>,
    ) -> anyhow::Result<()> {
        self.write_document(self.get_input_argument_document(argument));
        Ok(())
    }

    fn describe_input_option(
        &mut self,
        option: &InputOption,
        _options: IndexMap<String, PhpMixed>,
    ) -> anyhow::Result<()> {
        self.write_document(self.get_input_option_document(option));
        Ok(())
    }

    fn describe_input_definition(
        &mut self,
        definition: &InputDefinition,
        _options: IndexMap<String, PhpMixed>,
    ) -> anyhow::Result<()> {
        self.write_document(self.get_input_definition_document(definition));
        Ok(())
    }

    fn describe_command(
        &mut self,
        command: &dyn Command,
        options: IndexMap<String, PhpMixed>,
    ) -> anyhow::Result<()> {
        let short = matches!(options.get("short"), Some(PhpMixed::Bool(true)));
        self.write_document(self.get_command_document(command, short));
        Ok(())
    }

    fn describe_application(
        &mut self,
        application: std::rc::Rc<std::cell::RefCell<dyn Application>>,
        options: IndexMap<String, PhpMixed>,
    ) -> anyhow::Result<()> {
        let namespace = match options.get("namespace") {
            Some(PhpMixed::String(s)) => Some(s.clone()),
            _ => None,
        };
        let short = matches!(options.get("short"), Some(PhpMixed::Bool(true)));
        self.write_document(self.get_application_document(application, namespace, short));
        Ok(())
    }

    /// Appends document children to parent node.
    fn append_document(&self, parent_node: &DOMNode, imported_parent: &DOMNode) {
        for child_node in imported_parent.child_nodes() {
            parent_node.append_child(parent_node.owner_document().import_node(&child_node, true));
        }
    }

    /// Writes DOM document.
    fn write_document(&self, dom: DOMDocument) {
        dom.set_format_output(true);
        let mut buf = Vec::new();
        dom.save_xml(&mut buf)
            .expect("serializing XML to an in-memory buffer cannot fail");
        let xml = String::from_utf8(buf).expect("DOM serialization yields valid UTF-8");
        self.write(&xml, false);
    }

    fn get_input_argument_document(&self, argument: &InputArgument) -> DOMDocument {
        let dom = DOMDocument::new("1.0", "UTF-8");

        let object_xml = dom.append_child(dom.create_element("argument"));
        object_xml.set_attribute("name", argument.get_name());
        object_xml.set_attribute(
            "is_required",
            if argument.is_required() { "1" } else { "0" },
        );
        object_xml.set_attribute("is_array", if argument.is_array() { "1" } else { "0" });
        let description_xml = object_xml.append_child(dom.create_element("description"));
        description_xml.append_child(dom.create_text_node(argument.get_description()));

        let defaults_xml = object_xml.append_child(dom.create_element("defaults"));
        let defaults: Vec<String> = match argument.get_default() {
            PhpMixed::List(_) | PhpMixed::Array(_) => {
                self.default_values_as_strings(argument.get_default())
            }
            PhpMixed::Bool(_) => vec![shirabe_php_shim::var_export(argument.get_default(), true)],
            d if shirabe_php_shim::php_truthy(d) => {
                vec![shirabe_php_shim::php_to_string(argument.get_default())]
            }
            _ => vec![],
        };
        for default in defaults {
            let default_xml = defaults_xml.append_child(dom.create_element("default"));
            default_xml.append_child(dom.create_text_node(&default));
        }

        dom
    }

    fn get_input_option_document(&self, option: &InputOption) -> DOMDocument {
        let dom = DOMDocument::new("1.0", "UTF-8");

        let object_xml = dom.append_child(dom.create_element("option"));
        object_xml.set_attribute("name", &format!("--{}", option.get_name()));
        let pos = shirabe_php_shim::strpos(option.get_shortcut().unwrap_or(""), "|");
        if let Some(pos) = pos {
            object_xml.set_attribute(
                "shortcut",
                &format!(
                    "-{}",
                    shirabe_php_shim::substr(option.get_shortcut().unwrap(), 0, Some(pos as i64))
                ),
            );
            object_xml.set_attribute(
                "shortcuts",
                &format!(
                    "-{}",
                    shirabe_php_shim::str_replace("|", "|-", option.get_shortcut().unwrap())
                ),
            );
        } else {
            object_xml.set_attribute(
                "shortcut",
                &match option.get_shortcut() {
                    Some(s) => format!("-{}", s),
                    None => String::new(),
                },
            );
        }
        object_xml.set_attribute(
            "accept_value",
            if option.accept_value() { "1" } else { "0" },
        );
        object_xml.set_attribute(
            "is_value_required",
            if option.is_value_required() { "1" } else { "0" },
        );
        object_xml.set_attribute("is_multiple", if option.is_array() { "1" } else { "0" });
        let description_xml = object_xml.append_child(dom.create_element("description"));
        description_xml.append_child(dom.create_text_node(option.get_description()));

        if option.accept_value() {
            let defaults: Vec<String> = match option.get_default() {
                PhpMixed::List(_) | PhpMixed::Array(_) => {
                    self.default_values_as_strings(option.get_default())
                }
                PhpMixed::Bool(_) => vec![shirabe_php_shim::var_export(option.get_default(), true)],
                d if shirabe_php_shim::php_truthy(d) => {
                    vec![shirabe_php_shim::php_to_string(option.get_default())]
                }
                _ => vec![],
            };
            let defaults_xml = object_xml.append_child(dom.create_element("defaults"));

            if !defaults.is_empty() {
                for default in defaults {
                    let default_xml = defaults_xml.append_child(dom.create_element("default"));
                    default_xml.append_child(dom.create_text_node(&default));
                }
            }
        }

        if option.is_negatable() {
            let object_xml = dom.append_child(dom.create_element("option"));
            object_xml.set_attribute("name", &format!("--no-{}", option.get_name()));
            object_xml.set_attribute("shortcut", "");
            object_xml.set_attribute("accept_value", "0");
            object_xml.set_attribute("is_value_required", "0");
            object_xml.set_attribute("is_multiple", "0");
            let description_xml = object_xml.append_child(dom.create_element("description"));
            description_xml.append_child(
                dom.create_text_node(&format!("Negate the \"--{}\" option", option.get_name())),
            );
        }

        dom
    }

    /// Helper used by the default-value branches of getInputArgumentDocument /
    /// getInputOptionDocument when the default is an array (returns it verbatim).
    fn default_values_as_strings(&self, default: &PhpMixed) -> Vec<String> {
        match default {
            PhpMixed::List(list) => list
                .iter()
                .map(|v| shirabe_php_shim::php_to_string(v))
                .collect(),
            PhpMixed::Array(arr) => arr
                .values()
                .map(|v| shirabe_php_shim::php_to_string(v))
                .collect(),
            _ => vec![],
        }
    }
}

impl DescriptorInterface for XmlDescriptor {
    fn describe(
        &mut self,
        output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
        object: DescribableObject,
        options: IndexMap<String, PhpMixed>,
    ) -> anyhow::Result<()> {
        Descriptor::describe(self, output, object, options)
    }
}

impl Descriptor for XmlDescriptor {
    fn output(&self) -> std::rc::Rc<std::cell::RefCell<dyn OutputInterface>> {
        self.output.clone().unwrap()
    }

    fn set_output(&mut self, output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>) {
        self.output = Some(output);
    }

    fn describe_input_argument(
        &mut self,
        argument: &InputArgument,
        options: IndexMap<String, PhpMixed>,
    ) -> anyhow::Result<()> {
        XmlDescriptor::describe_input_argument(self, argument, options)
    }

    fn describe_input_option(
        &mut self,
        option: &InputOption,
        options: IndexMap<String, PhpMixed>,
    ) -> anyhow::Result<()> {
        XmlDescriptor::describe_input_option(self, option, options)
    }

    fn describe_input_definition(
        &mut self,
        definition: &InputDefinition,
        options: IndexMap<String, PhpMixed>,
    ) -> anyhow::Result<()> {
        XmlDescriptor::describe_input_definition(self, definition, options)
    }

    fn describe_command(
        &mut self,
        command: &dyn Command,
        options: IndexMap<String, PhpMixed>,
    ) -> anyhow::Result<()> {
        XmlDescriptor::describe_command(self, command, options)
    }

    fn describe_application(
        &mut self,
        application: std::rc::Rc<std::cell::RefCell<dyn Application>>,
        options: IndexMap<String, PhpMixed>,
    ) -> anyhow::Result<()> {
        XmlDescriptor::describe_application(self, application, options)
    }
}