aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/console/helper/helper_set.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-06-12 01:01:35 +0900
committernsfisis <nsfisis@gmail.com>2026-06-12 01:01:43 +0900
commit1e44f5723e4c0e0903d00a61f254901e612fe5e1 (patch)
tree9c2e1eec364bbf6418a4072ee7767b04c3df0297 /crates/shirabe-external-packages/src/symfony/console/helper/helper_set.rs
parentddf0a624145b618c05c2ee47414545b99b685f37 (diff)
downloadphp-shirabe-1e44f5723e4c0e0903d00a61f254901e612fe5e1.tar.gz
php-shirabe-1e44f5723e4c0e0903d00a61f254901e612fe5e1.tar.zst
php-shirabe-1e44f5723e4c0e0903d00a61f254901e612fe5e1.zip
feat(symfony-console): port Symfony Console and make the workspace compile
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-external-packages/src/symfony/console/helper/helper_set.rs')
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/helper/helper_set.rs108
1 files changed, 98 insertions, 10 deletions
diff --git a/crates/shirabe-external-packages/src/symfony/console/helper/helper_set.rs b/crates/shirabe-external-packages/src/symfony/console/helper/helper_set.rs
index e38dbf3..a7ef820 100644
--- a/crates/shirabe-external-packages/src/symfony/console/helper/helper_set.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/helper/helper_set.rs
@@ -1,27 +1,115 @@
-use crate::symfony::console::helper::HelperInterface;
+use crate::symfony::console::command::command::Command;
+use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException;
+use crate::symfony::console::helper::helper_interface::HelperInterface;
use indexmap::IndexMap;
use std::cell::RefCell;
use std::rc::Rc;
-#[derive(Debug)]
+/// HelperSet represents a set of helpers to be used with a command.
+///
+/// @implements \IteratorAggregate<string, Helper>
+#[derive(Debug, Default, Clone)]
pub struct HelperSet {
helpers: IndexMap<String, Rc<RefCell<dyn HelperInterface>>>,
+ command: Option<Rc<RefCell<dyn Command>>>,
}
impl HelperSet {
- pub fn new(_helpers: Vec<Rc<RefCell<dyn HelperInterface>>>) -> Self {
- todo!()
+ /// @param Helper[] $helpers An array of helper
+ pub fn new(
+ this: &Rc<RefCell<HelperSet>>,
+ helpers: IndexMap<HelperSetKey, Rc<RefCell<dyn HelperInterface>>>,
+ ) {
+ for (alias, helper) in helpers {
+ let alias = match alias {
+ HelperSetKey::Int(_) => None,
+ HelperSetKey::String(alias) => Some(alias),
+ };
+ Self::set(this, helper, alias.as_deref());
+ }
}
- pub fn get<T: HelperInterface + 'static>(&self, _name: &str) -> Rc<RefCell<T>> {
- todo!()
+ pub fn set(
+ this: &Rc<RefCell<HelperSet>>,
+ helper: Rc<RefCell<dyn HelperInterface>>,
+ alias: Option<&str>,
+ ) {
+ let name = helper.borrow().get_name();
+ this.borrow_mut().helpers.insert(name, helper.clone());
+ if let Some(alias) = alias {
+ this.borrow_mut()
+ .helpers
+ .insert(alias.to_string(), helper.clone());
+ }
+
+ helper.borrow_mut().set_helper_set(Some(this.clone()));
+ }
+
+ /// Returns true if the helper if defined.
+ pub fn has(&self, name: &str) -> bool {
+ self.helpers.contains_key(name)
+ }
+
+ /// Gets a helper value.
+ ///
+ /// @throws InvalidArgumentException if the helper is not defined
+ pub fn get(
+ &self,
+ name: &str,
+ ) -> Result<Rc<RefCell<dyn HelperInterface>>, InvalidArgumentException> {
+ if !self.has(name) {
+ return Err(InvalidArgumentException(
+ shirabe_php_shim::InvalidArgumentException {
+ message: shirabe_php_shim::sprintf(
+ "The helper \"%s\" is not defined.",
+ &[shirabe_php_shim::PhpMixed::String(name.to_string())],
+ ),
+ code: 0,
+ },
+ ));
+ }
+
+ Ok(self.helpers[name].clone())
}
- pub fn set(&mut self, _helper: Rc<RefCell<dyn HelperInterface>>, _alias: Option<&str>) {
- todo!()
+ /// @deprecated since Symfony 5.4
+ pub fn set_command(&mut self, command: Option<Rc<RefCell<dyn Command>>>) {
+ shirabe_php_shim::trigger_deprecation(
+ "symfony/console",
+ "5.4",
+ "Method \"%s()\" is deprecated.",
+ "HelperSet::setCommand",
+ );
+
+ self.command = command;
+ }
+
+ /// Gets the command associated with this helper set.
+ ///
+ /// @deprecated since Symfony 5.4
+ pub fn get_command(&self) -> Option<Rc<RefCell<dyn Command>>> {
+ shirabe_php_shim::trigger_deprecation(
+ "symfony/console",
+ "5.4",
+ "Method \"%s()\" is deprecated.",
+ "HelperSet::getCommand",
+ );
+
+ self.command.clone()
}
- pub fn has(&self, _name: &str) -> bool {
- todo!()
+ /// @return \Traversable<string, Helper>
+ pub fn get_iterator(
+ &self,
+ ) -> impl Iterator<Item = (&String, &Rc<RefCell<dyn HelperInterface>>)> {
+ self.helpers.iter()
}
}
+
+/// PHP array keys are either integers or strings; the HelperSet constructor
+/// distinguishes them via `\is_int($alias)`.
+#[derive(Debug, Clone, PartialEq, Eq, Hash)]
+pub enum HelperSetKey {
+ Int(i64),
+ String(String),
+}