From 6051927c8fa32cfffa102d2a170c5a6cf747a1b9 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 9 Aug 2026 11:19:03 +0900 Subject: refactor(symfony-console): extract symfony/console into the shirabe-symfony-console crate Move `Symfony\Component\Console` out of shirabe-external-packages and into its own crate, so the path is `shirabe_symfony_console::application::Application` instead of `shirabe_external_packages::symfony::console::application::Application`. The `delegate_to_inner!` and `delegate_command_trait_impls_to_inner!` macros move with it. Co-Authored-By: Claude Opus 5 (1M context) --- .../src/helper/descriptor_helper.rs | 119 +++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 crates/shirabe-symfony-console/src/helper/descriptor_helper.rs (limited to 'crates/shirabe-symfony-console/src/helper/descriptor_helper.rs') diff --git a/crates/shirabe-symfony-console/src/helper/descriptor_helper.rs b/crates/shirabe-symfony-console/src/helper/descriptor_helper.rs new file mode 100644 index 00000000..23bb4f66 --- /dev/null +++ b/crates/shirabe-symfony-console/src/helper/descriptor_helper.rs @@ -0,0 +1,119 @@ +//! ref: composer/vendor/symfony/console/Helper/DescriptorHelper.php + +use crate::descriptor::descriptor_interface::{DescribableObject, DescriptorInterface}; +use crate::descriptor::json_descriptor::JsonDescriptor; +use crate::descriptor::markdown_descriptor::MarkdownDescriptor; +use crate::descriptor::text_descriptor::TextDescriptor; +use crate::descriptor::xml_descriptor::XmlDescriptor; +use crate::exception::invalid_argument_exception::InvalidArgumentException; +use crate::helper::helper::Helper; +use crate::helper::helper_interface::HelperInterface; +use crate::helper::helper_set::HelperSet; +use crate::output::output_interface::OutputInterface; +use indexmap::IndexMap; + +/// This class adds helper method to describe objects in various formats. +#[derive(Default)] +pub struct DescriptorHelper { + inner: Helper, + /// @var DescriptorInterface[] + descriptors: IndexMap>, +} + +// `DescriptorInterface` does not require `Debug`, so the derive cannot see +// through the trait object; provide a minimal manual impl. +impl std::fmt::Debug for DescriptorHelper { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("DescriptorHelper") + .field("inner", &self.inner) + .field("descriptors", &self.descriptors.keys().collect::>()) + .finish() + } +} + +impl DescriptorHelper { + pub fn new() -> Self { + let mut this = Self { + inner: Helper::default(), + descriptors: IndexMap::new(), + }; + this.register("txt", Box::new(TextDescriptor::default())) + .register("xml", Box::new(XmlDescriptor::default())) + .register("json", Box::new(JsonDescriptor::default())) + .register("md", Box::new(MarkdownDescriptor::default())); + this + } + + /// Describes an object if supported. + /// + /// Available options are: + /// * format: string, the output format name + /// * raw_text: boolean, sets output type as raw + /// + /// @throws InvalidArgumentException when the given format is not supported + pub fn describe2( + &mut self, + output: std::rc::Rc>, + object: DescribableObject, + options: IndexMap, + ) -> anyhow::Result<()> { + let mut merged: IndexMap = IndexMap::new(); + merged.insert( + "raw_text".to_string(), + shirabe_php_shim::PhpMixed::Bool(false), + ); + merged.insert( + "format".to_string(), + shirabe_php_shim::PhpMixed::String("txt".to_string()), + ); + for (key, value) in options { + merged.insert(key, value); + } + let options = merged; + + let format = match &options["format"] { + shirabe_php_shim::PhpMixed::String(format) => format.clone(), + _ => String::new(), + }; + + if !self.descriptors.contains_key(&format) { + return Err(InvalidArgumentException::new(format!( + "Unsupported format \"{}\".", + format.clone() + )) + .into()); + } + + let descriptor = self.descriptors.get_mut(&format).unwrap(); + descriptor.describe(output, object, options) + } + + /// Registers a descriptor. + pub fn register( + &mut self, + format: &str, + descriptor: Box, + ) -> &mut Self { + self.descriptors.insert(format.to_string(), descriptor); + + self + } + + pub fn get_formats(&self) -> Vec { + self.descriptors.keys().cloned().collect() + } +} + +impl HelperInterface for DescriptorHelper { + fn set_helper_set(&mut self, helper_set: Option>>) { + self.inner.set_helper_set(helper_set); + } + + fn get_helper_set(&self) -> Option>> { + self.inner.get_helper_set() + } + + fn get_name(&self) -> String { + "descriptor".to_string() + } +} -- cgit v1.3.1-4-g156e