aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/console/helper/helper_set.rs
blob: 292a7a44394d7b3e2d695306eb75e13a1760ed27 (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
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;

/// 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 {
    /// @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 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: format!(
                        "The helper \"{}\" is not defined.",
                        shirabe_php_shim::PhpMixed::String(name.to_string()),
                    ),
                    code: 0,
                },
            ));
        }

        Ok(self.helpers[name].clone())
    }

    /// @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()
    }

    /// @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),
}