diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-05-26 20:04:02 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-05-26 20:04:02 +0900 |
| commit | f411daceacad66e0bd774fda7d3c5ef8533cc55c (patch) | |
| tree | eefb065e4d676a3f7031ca49bab21c773b00b134 /crates/shirabe/src/command/base_command.rs | |
| parent | 1921f173ea219cb4b25847294d2d3fa465550fbb (diff) | |
| download | php-shirabe-f411daceacad66e0bd774fda7d3c5ef8533cc55c.tar.gz php-shirabe-f411daceacad66e0bd774fda7d3c5ef8533cc55c.tar.zst php-shirabe-f411daceacad66e0bd774fda7d3c5ef8533cc55c.zip | |
refactor(io): share IOInterface via Rc<RefCell<dyn _>> handle
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/command/base_command.rs')
| -rw-r--r-- | crates/shirabe/src/command/base_command.rs | 33 |
1 files changed, 17 insertions, 16 deletions
diff --git a/crates/shirabe/src/command/base_command.rs b/crates/shirabe/src/command/base_command.rs index 9511c4d..6b673ed 100644 --- a/crates/shirabe/src/command/base_command.rs +++ b/crates/shirabe/src/command/base_command.rs @@ -13,6 +13,8 @@ use shirabe_php_shim::{ InvalidArgumentException, LogicException, PhpMixed, RuntimeException, UnexpectedValueException, count, explode, in_array, is_string, max, }; +use std::cell::RefCell; +use std::rc::Rc; use crate::advisory::AuditConfig; use crate::advisory::Auditor; @@ -27,6 +29,7 @@ use crate::factory::Factory; use crate::filter::platform_requirement_filter::PlatformRequirementFilterFactory; use crate::filter::platform_requirement_filter::PlatformRequirementFilterInterface; use crate::io::IOInterface; +use crate::io::IOInterfaceImmutable; use crate::io::NullIO; use crate::package::version::VersionParser; use crate::plugin::PluginEvents; @@ -184,9 +187,9 @@ pub trait BaseCommand { /// Whether or not this command is meant to call another command. fn is_proxy_command(&self) -> bool; - fn get_io(&mut self) -> &mut dyn IOInterface; + fn get_io(&mut self) -> Rc<RefCell<dyn IOInterface>>; - fn set_io(&mut self, io: Box<dyn IOInterface>); + fn set_io(&mut self, io: Rc<RefCell<dyn IOInterface>>); // TODO(cli-completion): fn complete(&self, input: &CompletionInput, suggestions: &mut CompletionSuggestions); @@ -201,7 +204,7 @@ pub trait BaseCommand { fn create_composer_instance( &self, input: &dyn InputInterface, - io: &dyn IOInterface, + io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>>, config: Option<IndexMap<String, PhpMixed>>, disable_plugins: bool, disable_scripts: Option<bool>, @@ -254,7 +257,7 @@ pub trait BaseCommand { #[derive(Debug)] pub struct BaseCommandData { pub(crate) composer: Option<PartialComposerHandle>, - pub(crate) io: Option<Box<dyn IOInterface>>, + pub(crate) io: Option<Rc<RefCell<dyn IOInterface>>>, } pub trait HasBaseCommandData { @@ -269,11 +272,11 @@ pub trait HasBaseCommandData { &mut self.base_command_data_mut().composer } - fn io(&self) -> Option<&dyn IOInterface> { - self.base_command_data().io.as_deref() + fn io(&self) -> Option<Rc<RefCell<dyn IOInterface>>> { + self.base_command_data().io.clone() } - fn io_mut(&mut self) -> &mut Option<Box<dyn IOInterface>> { + fn io_mut(&mut self) -> &mut Option<Rc<RefCell<dyn IOInterface>>> { &mut self.base_command_data_mut().io } } @@ -335,16 +338,16 @@ impl<C: HasBaseCommandData> BaseCommand for C { false } - fn get_io(&mut self) -> &mut dyn IOInterface { + fn get_io(&mut self) -> Rc<RefCell<dyn IOInterface>> { if self.io().is_none() { // TODO(phase-b): requires inner Symfony Application access - *self.io_mut() = Some(Box::new(NullIO::new())); + *self.io_mut() = Some(Rc::new(RefCell::new(NullIO::new()))); } - &mut **self.io_mut().as_mut().unwrap() + self.io().unwrap() } - fn set_io(&mut self, io: Box<dyn IOInterface>) { + fn set_io(&mut self, io: Rc<RefCell<dyn IOInterface>>) { *self.io_mut() = Some(io); } @@ -363,9 +366,7 @@ impl<C: HasBaseCommandData> BaseCommand for C { // TODO(phase-b): `$this instanceof SelfUpdateCommand` not representable let composer = self.try_composer(Some(disable_plugins), Some(disable_scripts)); - // TODO(phase-b): re-borrow self for get_io after try_composer move - let io_ptr: *const dyn IOInterface = self.get_io(); - let io = unsafe { &*io_ptr }; + let io = self.get_io(); let disable_plugins_kind = if disable_plugins { crate::factory::DisablePlugins::All @@ -373,7 +374,7 @@ impl<C: HasBaseCommandData> BaseCommand for C { crate::factory::DisablePlugins::None }; let composer = if composer.is_none() { - Factory::create_global(io, disable_plugins_kind, disable_scripts) + Factory::create_global(io.clone(), disable_plugins_kind, disable_scripts) } else { composer }; @@ -476,7 +477,7 @@ impl<C: HasBaseCommandData> BaseCommand for C { fn create_composer_instance( &self, input: &dyn InputInterface, - io: &dyn IOInterface, + io: std::rc::Rc<std::cell::RefCell<dyn IOInterface>>, config: Option<IndexMap<String, PhpMixed>>, disable_plugins: bool, disable_scripts: Option<bool>, |
