aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock1
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/command/help_command.rs10
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/command/list_command.rs10
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor.rs48
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor_interface.rs16
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs6
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/descriptor/markdown_descriptor.rs8
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/descriptor/text_descriptor.rs6
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/descriptor/xml_descriptor.rs6
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/helper/descriptor_helper.rs41
-rw-r--r--crates/shirabe-external-packages/src/symfony/string/unicode_string.rs28
-rw-r--r--crates/shirabe-php-shim/Cargo.toml1
-rw-r--r--crates/shirabe-php-shim/src/lib.rs52
-rw-r--r--crates/shirabe/src/console/application.rs38
14 files changed, 161 insertions, 110 deletions
diff --git a/Cargo.lock b/Cargo.lock
index b9de65e..089dfdf 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1023,6 +1023,7 @@ dependencies = [
"indexmap",
"regex",
"serde",
+ "serde_json",
"zip",
]
diff --git a/crates/shirabe-external-packages/src/symfony/console/command/help_command.rs b/crates/shirabe-external-packages/src/symfony/console/command/help_command.rs
index 321b640..d98152f 100644
--- a/crates/shirabe-external-packages/src/symfony/console/command/help_command.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/command/help_command.rs
@@ -6,6 +6,7 @@ use crate::symfony::console::completion::completion_suggestions::{
CompletionSuggestions, StringOrSuggestion,
};
use crate::symfony::console::descriptor::application_description::ApplicationDescription;
+use crate::symfony::console::descriptor::descriptor_interface::DescribableObject;
use crate::symfony::console::helper::descriptor_helper::DescriptorHelper;
use crate::symfony::console::input::input_argument::InputArgument;
use crate::symfony::console::input::input_definition::DefinitionItem;
@@ -137,15 +138,12 @@ impl Command for HelpCommand {
self.command = Some(application.borrow_mut().find(&command_name)?);
}
- let helper = DescriptorHelper::new();
- // TODO: DescriptorHelper::describe2 takes the described object as Option<PhpMixed>,
- // but PhpMixed cannot hold a Command. The Command/Application object mixing for
- // describe needs a dedicated type (Phase C).
- let object: Option<PhpMixed> = todo!();
+ let mut helper = DescriptorHelper::new();
+ let object = DescribableObject::Command(self.command.clone().unwrap());
let mut options = indexmap::IndexMap::new();
options.insert("format".to_string(), input.borrow().get_option("format")?);
options.insert("raw_text".to_string(), input.borrow().get_option("raw")?);
- let _ = helper.describe2(&mut *output.borrow_mut(), object, options);
+ helper.describe2(output.clone(), object, options)?;
self.command = None;
diff --git a/crates/shirabe-external-packages/src/symfony/console/command/list_command.rs b/crates/shirabe-external-packages/src/symfony/console/command/list_command.rs
index 9edebe4..bfaece7 100644
--- a/crates/shirabe-external-packages/src/symfony/console/command/list_command.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/command/list_command.rs
@@ -6,6 +6,7 @@ use crate::symfony::console::completion::completion_suggestions::{
CompletionSuggestions, StringOrSuggestion,
};
use crate::symfony::console::descriptor::application_description::ApplicationDescription;
+use crate::symfony::console::descriptor::descriptor_interface::DescribableObject;
use crate::symfony::console::helper::descriptor_helper::DescriptorHelper;
use crate::symfony::console::input::input_argument::InputArgument;
use crate::symfony::console::input::input_definition::DefinitionItem;
@@ -136,11 +137,8 @@ impl Command for ListCommand {
input: Rc<RefCell<dyn InputInterface>>,
output: Rc<RefCell<dyn OutputInterface>>,
) -> anyhow::Result<i64> {
- let helper = DescriptorHelper::new();
- // TODO: DescriptorHelper::describe2 takes the described object as Option<PhpMixed>,
- // but PhpMixed cannot hold an Application. The Command/Application object mixing for
- // describe needs a dedicated type (Phase C).
- let object: Option<PhpMixed> = todo!();
+ let mut helper = DescriptorHelper::new();
+ let object = DescribableObject::Application(self.get_application().unwrap());
let mut options = indexmap::IndexMap::new();
options.insert("format".to_string(), input.borrow().get_option("format")?);
options.insert("raw_text".to_string(), input.borrow().get_option("raw")?);
@@ -149,7 +147,7 @@ impl Command for ListCommand {
input.borrow().get_argument("namespace")?,
);
options.insert("short".to_string(), input.borrow().get_option("short")?);
- let _ = helper.describe2(&mut *output.borrow_mut(), object, options);
+ helper.describe2(output.clone(), object, options)?;
Ok(0)
}
diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor.rs
index 43b1094..38824a6 100644
--- a/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor.rs
@@ -2,8 +2,9 @@
use crate::symfony::console::application::Application;
use crate::symfony::console::command::command::Command;
-use crate::symfony::console::descriptor::descriptor_interface::DescriptorInterface;
-use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException;
+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;
@@ -20,52 +21,29 @@ pub trait Descriptor: DescriptorInterface {
fn describe(
&mut self,
output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
- object: PhpMixed,
+ object: DescribableObject,
options: IndexMap<String, PhpMixed>,
) -> anyhow::Result<()> {
self.set_output(output);
- // PHP dispatches via `$object instanceof ...`. The concrete runtime type of
- // `object` must be recovered to route to the correct describe* method.
- match true {
- // case $object instanceof InputArgument:
- _ if todo!("$object instanceof InputArgument") => {
- let argument: InputArgument = todo!("downcast object to InputArgument");
+ // PHP dispatches via `$object instanceof ...`; the explicit `DescribableObject` enum makes
+ // that dispatch a `match`.
+ match object {
+ DescribableObject::InputArgument(argument) => {
self.describe_input_argument(&argument, options)?;
}
- // case $object instanceof InputOption:
- _ if todo!("$object instanceof InputOption") => {
- let option: InputOption = todo!("downcast object to InputOption");
+ DescribableObject::InputOption(option) => {
self.describe_input_option(&option, options)?;
}
- // case $object instanceof InputDefinition:
- _ if todo!("$object instanceof InputDefinition") => {
- let definition: InputDefinition = todo!("downcast object to InputDefinition");
+ DescribableObject::InputDefinition(definition) => {
self.describe_input_definition(&definition, options)?;
}
- // case $object instanceof Command:
- _ if todo!("$object instanceof Command") => {
- let mut command: Box<dyn Command> = todo!("downcast object to Command");
- self.describe_command(command.as_mut(), options)?;
+ DescribableObject::Command(command) => {
+ self.describe_command(&mut *command.borrow_mut(), options)?;
}
- // case $object instanceof Application:
- _ if todo!("$object instanceof Application") => {
- let application: std::rc::Rc<std::cell::RefCell<dyn Application>> =
- todo!("downcast object to Application");
+ DescribableObject::Application(application) => {
self.describe_application(application, options)?;
}
- _ => {
- return Err(
- InvalidArgumentException(shirabe_php_shim::InvalidArgumentException {
- message: format!(
- "Object of type \"{}\" is not describable.",
- shirabe_php_shim::get_debug_type(&object)
- ),
- code: 0,
- })
- .into(),
- );
- }
}
Ok(())
diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor_interface.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor_interface.rs
index be39ec3..be0e9ff 100644
--- a/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor_interface.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/descriptor_interface.rs
@@ -1,15 +1,29 @@
//! ref: composer/vendor/symfony/console/Descriptor/DescriptorInterface.php
+use crate::symfony::console::application::Application;
+use crate::symfony::console::command::command::Command;
+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::PhpMixed;
+/// The set of objects the descriptors know how to describe.
+pub enum DescribableObject {
+ InputArgument(InputArgument),
+ InputOption(InputOption),
+ InputDefinition(InputDefinition),
+ Command(std::rc::Rc<std::cell::RefCell<dyn Command>>),
+ Application(std::rc::Rc<std::cell::RefCell<dyn Application>>),
+}
+
/// Descriptor interface.
pub trait DescriptorInterface {
fn describe(
&mut self,
output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
- object: PhpMixed,
+ object: DescribableObject,
options: IndexMap<String, PhpMixed>,
) -> anyhow::Result<()>;
}
diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs
index 4793370..9068a0a 100644
--- a/crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/json_descriptor.rs
@@ -5,7 +5,9 @@ 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::DescriptorInterface;
+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;
@@ -370,7 +372,7 @@ impl DescriptorInterface for JsonDescriptor {
fn describe(
&mut self,
output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
- object: PhpMixed,
+ object: DescribableObject,
options: IndexMap<String, PhpMixed>,
) -> anyhow::Result<()> {
Descriptor::describe(self, output, object, options)
diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/markdown_descriptor.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/markdown_descriptor.rs
index 698cc51..49d958f 100644
--- a/crates/shirabe-external-packages/src/symfony/console/descriptor/markdown_descriptor.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/markdown_descriptor.rs
@@ -5,7 +5,9 @@ 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::DescriptorInterface;
+use crate::symfony::console::descriptor::descriptor_interface::{
+ DescribableObject, DescriptorInterface,
+};
use crate::symfony::console::helper::helper::Helper;
use crate::symfony::console::input::input_argument::InputArgument;
use crate::symfony::console::input::input_definition::InputDefinition;
@@ -26,7 +28,7 @@ impl MarkdownDescriptor {
pub fn describe(
&mut self,
output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
- object: PhpMixed,
+ object: DescribableObject,
options: IndexMap<String, PhpMixed>,
) -> anyhow::Result<()> {
let decorated = output.borrow().is_decorated();
@@ -312,7 +314,7 @@ impl DescriptorInterface for MarkdownDescriptor {
fn describe(
&mut self,
output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
- object: PhpMixed,
+ object: DescribableObject,
options: IndexMap<String, PhpMixed>,
) -> anyhow::Result<()> {
MarkdownDescriptor::describe(self, output, object, options)
diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/text_descriptor.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/text_descriptor.rs
index 915e19e..6590384 100644
--- a/crates/shirabe-external-packages/src/symfony/console/descriptor/text_descriptor.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/text_descriptor.rs
@@ -5,7 +5,9 @@ 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::DescriptorInterface;
+use crate::symfony::console::descriptor::descriptor_interface::{
+ DescribableObject, DescriptorInterface,
+};
use crate::symfony::console::formatter::output_formatter::OutputFormatter;
use crate::symfony::console::helper::helper::Helper;
use crate::symfony::console::input::input_argument::InputArgument;
@@ -549,7 +551,7 @@ impl DescriptorInterface for TextDescriptor {
fn describe(
&mut self,
output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
- object: PhpMixed,
+ object: DescribableObject,
options: IndexMap<String, PhpMixed>,
) -> anyhow::Result<()> {
Descriptor::describe(self, output, object, options)
diff --git a/crates/shirabe-external-packages/src/symfony/console/descriptor/xml_descriptor.rs b/crates/shirabe-external-packages/src/symfony/console/descriptor/xml_descriptor.rs
index 4c65893..063f279 100644
--- a/crates/shirabe-external-packages/src/symfony/console/descriptor/xml_descriptor.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/descriptor/xml_descriptor.rs
@@ -4,7 +4,9 @@ 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::DescriptorInterface;
+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;
@@ -372,7 +374,7 @@ impl DescriptorInterface for XmlDescriptor {
fn describe(
&mut self,
output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
- object: PhpMixed,
+ object: DescribableObject,
options: IndexMap<String, PhpMixed>,
) -> anyhow::Result<()> {
Descriptor::describe(self, output, object, options)
diff --git a/crates/shirabe-external-packages/src/symfony/console/helper/descriptor_helper.rs b/crates/shirabe-external-packages/src/symfony/console/helper/descriptor_helper.rs
index a0e1b2b..521284b 100644
--- a/crates/shirabe-external-packages/src/symfony/console/helper/descriptor_helper.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/helper/descriptor_helper.rs
@@ -1,6 +1,8 @@
//! ref: composer/vendor/symfony/console/Helper/DescriptorHelper.php
-use crate::symfony::console::descriptor::descriptor_interface::DescriptorInterface;
+use crate::symfony::console::descriptor::descriptor_interface::{
+ DescribableObject, DescriptorInterface,
+};
use crate::symfony::console::descriptor::json_descriptor::JsonDescriptor;
use crate::symfony::console::descriptor::markdown_descriptor::MarkdownDescriptor;
use crate::symfony::console::descriptor::text_descriptor::TextDescriptor;
@@ -39,16 +41,10 @@ impl DescriptorHelper {
inner: Helper::default(),
descriptors: IndexMap::new(),
};
- // The XML/JSON/Markdown descriptors do not yet expose a constructor
- // (no `Default`/`new`); their construction is deferred until those
- // descriptor types provide one.
- let xml: Box<dyn DescriptorInterface> = todo!();
- let json: Box<dyn DescriptorInterface> = todo!();
- let md: Box<dyn DescriptorInterface> = todo!();
this.register("txt", Box::new(TextDescriptor::default()))
- .register("xml", xml)
- .register("json", json)
- .register("md", md);
+ .register("xml", Box::new(XmlDescriptor::default()))
+ .register("json", Box::new(JsonDescriptor::default()))
+ .register("md", Box::new(MarkdownDescriptor::default()));
this
}
@@ -60,11 +56,11 @@ impl DescriptorHelper {
///
/// @throws InvalidArgumentException when the given format is not supported
pub fn describe2(
- &self,
- output: &dyn OutputInterface,
- object: Option<shirabe_php_shim::PhpMixed>,
+ &mut self,
+ output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
+ object: DescribableObject,
options: IndexMap<String, shirabe_php_shim::PhpMixed>,
- ) -> Result<(), InvalidArgumentException> {
+ ) -> anyhow::Result<()> {
let mut merged: IndexMap<String, shirabe_php_shim::PhpMixed> = IndexMap::new();
merged.insert(
"raw_text".to_string(),
@@ -85,23 +81,20 @@ impl DescriptorHelper {
};
if !self.descriptors.contains_key(&format) {
- return Err(InvalidArgumentException(
- shirabe_php_shim::InvalidArgumentException {
+ return Err(
+ InvalidArgumentException(shirabe_php_shim::InvalidArgumentException {
message: format!(
"Unsupported format \"{}\".",
shirabe_php_shim::PhpMixed::String(format.clone()),
),
code: 0,
- },
- ));
+ })
+ .into(),
+ );
}
- let _ = (&self.descriptors[&format], output, object, options);
- // `DescriptorInterface::describe` takes an owned
- // `Rc<RefCell<dyn OutputInterface>>` and `&mut self`, while the helper
- // only holds a shared `&dyn OutputInterface` and `&self`. Reconciling
- // this ownership/mutability gap is a Phase C decision.
- todo!("dispatch to DescriptorInterface::describe (Phase C ownership)");
+ let descriptor = self.descriptors.get_mut(&format).unwrap();
+ descriptor.describe(output, object, options)
}
/// Registers a descriptor.
diff --git a/crates/shirabe-external-packages/src/symfony/string/unicode_string.rs b/crates/shirabe-external-packages/src/symfony/string/unicode_string.rs
index 396dcf3..8917f20 100644
--- a/crates/shirabe-external-packages/src/symfony/string/unicode_string.rs
+++ b/crates/shirabe-external-packages/src/symfony/string/unicode_string.rs
@@ -6,12 +6,32 @@ pub struct UnicodeString {
}
impl UnicodeString {
- pub fn new(_string: &str) -> Self {
- todo!()
+ // TODO(phase-c): the real constructor runs `normalizer_normalize` (Unicode NFC normalization),
+ // which has no Rust std equivalent and would need a dedicated normalization implementation.
+ // Normalization is skipped here, which is only correct for already-NFC input such as ASCII.
+ pub fn new(string: &str) -> Self {
+ Self {
+ string: string.to_string(),
+ }
}
- pub fn width(&self, _ignore_unsupported_encoding: bool) -> i64 {
- todo!()
+ // TODO(phase-c): ASCII-only provisional implementation. The faithful `width()` uses `wcswidth`
+ // with the Unicode width tables to treat wide characters as width 2 and skip zero-width /
+ // combining characters; here every character counts as width 1, correct only for ASCII. The
+ // ANSI/control-character stripping driven by `ignore_ansi_decoration` is likewise not handled.
+ pub fn width(&self, _ignore_ansi_decoration: bool) -> i64 {
+ let s = self.string.replace(['\x00', '\x05', '\x07'], "");
+ let s = s.replace("\r\n", "\n").replace('\r', "\n");
+
+ let mut width: i64 = 0;
+ for line in s.split('\n') {
+ let line_width = line.chars().count() as i64;
+ if line_width > width {
+ width = line_width;
+ }
+ }
+
+ width
}
pub fn length(&self) -> i64 {
diff --git a/crates/shirabe-php-shim/Cargo.toml b/crates/shirabe-php-shim/Cargo.toml
index b577c1f..abfe89e 100644
--- a/crates/shirabe-php-shim/Cargo.toml
+++ b/crates/shirabe-php-shim/Cargo.toml
@@ -10,6 +10,7 @@ fastrand.workspace = true
indexmap.workspace = true
regex.workspace = true
serde.workspace = true
+serde_json.workspace = true
zip.workspace = true
[lints]
diff --git a/crates/shirabe-php-shim/src/lib.rs b/crates/shirabe-php-shim/src/lib.rs
index 6b8a229..5821819 100644
--- a/crates/shirabe-php-shim/src/lib.rs
+++ b/crates/shirabe-php-shim/src/lib.rs
@@ -492,8 +492,14 @@ pub fn array_keys<V>(_array: &IndexMap<String, V>) -> Vec<String> {
_array.keys().cloned().collect()
}
-pub fn str_replace(_search: &str, _replace: &str, _subject: &str) -> String {
- todo!()
+pub fn str_replace(search: &str, replace: &str, subject: &str) -> String {
+ // PHP returns the subject unchanged when the search string is empty, whereas Rust's
+ // `str::replace` would insert `replace` between every character.
+ if search.is_empty() {
+ return subject.to_string();
+ }
+
+ subject.replace(search, replace)
}
pub fn php_to_string(value: &PhpMixed) -> String {
@@ -1362,8 +1368,35 @@ pub fn filesize(_path: &str) -> Option<i64> {
std::fs::metadata(_path).ok().map(|m| m.len() as i64)
}
-pub fn json_encode_ex<T: serde::Serialize + ?Sized>(_value: &T, _flags: i64) -> Option<String> {
- todo!()
+pub fn json_encode_ex<T: serde::Serialize + ?Sized>(value: &T, flags: i64) -> Option<String> {
+ // serde_json's compact output already matches PHP's `json_encode` with both
+ // JSON_UNESCAPED_SLASHES and JSON_UNESCAPED_UNICODE set: forward slashes and non-ASCII
+ // characters are emitted verbatim. The two flags below re-apply PHP's default escaping when
+ // they are absent.
+ // TODO(phase-c): other flags (e.g. JSON_PRETTY_PRINT, JSON_HEX_*, JSON_THROW_ON_ERROR) are not
+ // handled yet; add them when a call site needs them.
+ let mut s = serde_json::to_string(value).ok()?;
+
+ if flags & JSON_UNESCAPED_SLASHES == 0 {
+ s = s.replace('/', "\\/");
+ }
+
+ if flags & JSON_UNESCAPED_UNICODE == 0 {
+ let mut out = String::with_capacity(s.len());
+ for c in s.chars() {
+ if (c as u32) <= 0x7F {
+ out.push(c);
+ } else {
+ let mut buf = [0u16; 2];
+ for unit in c.encode_utf16(&mut buf) {
+ out.push_str(&format!("\\u{:04x}", unit));
+ }
+ }
+ }
+ s = out;
+ }
+
+ Some(s)
}
pub const JSON_INVALID_UTF8_IGNORE: i64 = 1048576;
@@ -1650,8 +1683,15 @@ pub fn feof(_stream: PhpMixed) -> bool {
todo!()
}
-pub fn str_replace_array(_search: &[String], _replace: &[String], _subject: &str) -> String {
- todo!()
+pub fn str_replace_array(search: &[String], replace: &[String], subject: &str) -> String {
+ // PHP's array form of str_replace replaces each search element in order with the replace
+ // element at the same index, falling back to an empty string when replace is shorter.
+ let mut result = subject.to_string();
+ for (i, s) in search.iter().enumerate() {
+ let r = replace.get(i).map(String::as_str).unwrap_or("");
+ result = str_replace(s, r, &result);
+ }
+ result
}
pub fn file(_filename: &str, _flags: i64) -> Option<Vec<String>> {
diff --git a/crates/shirabe/src/console/application.rs b/crates/shirabe/src/console/application.rs
index 82aa03d..146767d 100644
--- a/crates/shirabe/src/console/application.rs
+++ b/crates/shirabe/src/console/application.rs
@@ -8,6 +8,7 @@ use shirabe_external_packages::composer::xdebug_handler::XdebugHandler;
use shirabe_external_packages::seld::json_lint::ParsingException;
use shirabe_external_packages::symfony::console::application::Application as BaseApplication;
use shirabe_external_packages::symfony::console::command::Command as SymfonyCommand;
+use shirabe_external_packages::symfony::console::command::help_command::HelpCommand;
use shirabe_external_packages::symfony::console::command::lazy_command::LazyCommand;
use shirabe_external_packages::symfony::console::command::signalable_command_interface::SignalableCommandInterface;
use shirabe_external_packages::symfony::console::command_loader::command_loader_interface::CommandLoaderInterface;
@@ -1371,15 +1372,6 @@ impl Application {
// downcasts each plugin's CommandProvider capability — this is the Plugin API surface,
// which is intentionally unimplemented (see TODO(plugin) above). Returns an empty list
// until the plugin capability model exists.
- let _ = self.get_composer(false, Some(false), None)?;
- let _ = UnexpectedValueException {
- message: String::new(),
- code: 0,
- };
- let _: fn(PhpMixed, PhpMixed) -> PhpMixed = array_merge;
- let _: fn(&PhpMixed) -> String = get_class;
- let _ = shirabe_php_shim::ArrayObject::new(None);
- let _: IndexMap<String, PhpMixed> = IndexMap::new();
Ok(commands)
}
@@ -1913,16 +1905,13 @@ impl Application {
self.want_helps = false;
let help_command = self.get("help")?;
- // $helpCommand->setCommand($command);
- // TODO(review): setCommand() is defined on HelpCommand, not on the concrete
- // `SymfonyCommand`
- // struct; calling it through the Rc<RefCell<dyn SymfonyCommand>> needs the
- // SymfonyCommand-subclass
- // representation decision (downcast to HelpCommand).
- let _ = &command;
- todo!("help_command.set_command(command)");
+ help_command
+ .borrow_mut()
+ .as_any_mut()
+ .downcast_mut::<HelpCommand>()
+ .expect("the help command is a HelpCommand instance")
+ .set_command(command);
- #[allow(unreachable_code)]
return Ok(help_command);
}
@@ -2067,7 +2056,18 @@ impl Application {
let commands_snapshot: Vec<std::rc::Rc<std::cell::RefCell<dyn SymfonyCommand>>> =
self.commands.values().cloned().collect();
for command in &commands_snapshot {
- for alias in command.borrow().get_aliases() {
+ // A command's run() can re-enter find() (e.g. HelpCommand looks up the command it
+ // describes). That command is mutably borrowed for the duration of its run(), so it
+ // cannot be borrowed here. It is safe to skip: find() always completes this alias pass
+ // before returning a command, so any command currently executing already had its
+ // aliases registered in the earlier find() call that located it.
+ // TODO: this work-around could be solved.
+ let Ok(borrowed) = command.try_borrow() else {
+ continue;
+ };
+ let aliases = borrowed.get_aliases();
+ drop(borrowed);
+ for alias in aliases {
if !self.has(&alias) {
self.commands.insert(alias, command.clone());
}