aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-php-shim/src/env.rs
blob: cf0c659e4215a80c29c9871f85d6bc0b94dcb45c (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
//! Porting rules for PHP environment variables: see `docs/dev/env-vars-porting.md`.

pub fn getenv_all() -> std::env::VarsOs {
    std::env::vars_os()
}

pub fn getenv<K: AsRef<std::ffi::OsStr>>(key: K) -> Option<std::ffi::OsString> {
    std::env::var_os(key)
}

pub unsafe fn putenv<K: AsRef<std::ffi::OsStr>, V: AsRef<std::ffi::OsStr>>(key: K, value: V) {
    // TODO: validate key and value format to avoid panic?
    unsafe { std::env::set_var(key, value) }
}

pub unsafe fn putenv_clear<K: AsRef<std::ffi::OsStr>>(key: K) {
    // TODO: validate key and value format to avoid panic?
    unsafe { std::env::remove_var(key) }
}

pub struct Superglobal {
    vars: indexmap::IndexMap<std::ffi::OsString, std::ffi::OsString>,
}

pub struct SuperglobalServer(Superglobal);

impl Superglobal {
    fn from_env_vars() -> Self {
        let vars = std::env::vars_os().collect();
        Self { vars }
    }

    pub fn get_all(&self) -> impl Iterator<Item = (std::ffi::OsString, std::ffi::OsString)> + '_ {
        self.vars.iter().map(|(k, v)| (k.clone(), v.clone()))
    }

    pub fn get<K: AsRef<std::ffi::OsStr>>(&self, key: K) -> Option<std::ffi::OsString> {
        self.vars.get(key.as_ref()).cloned()
    }

    pub fn put(&mut self, key: std::ffi::OsString, value: std::ffi::OsString) {
        self.vars.insert(key, value);
    }

    pub fn clear<K: AsRef<std::ffi::OsStr>>(&mut self, key: K) {
        self.vars.shift_remove(key.as_ref());
    }
}

impl SuperglobalServer {
    fn from_env_vars() -> Self {
        Self(Superglobal::from_env_vars())
    }

    pub fn get_all(&self) -> impl Iterator<Item = (std::ffi::OsString, std::ffi::OsString)> + '_ {
        self.0.get_all()
    }

    pub fn get<K: AsRef<std::ffi::OsStr>>(&self, key: K) -> Option<std::ffi::OsString> {
        self.0.get(key)
    }

    pub fn put(&mut self, key: std::ffi::OsString, value: std::ffi::OsString) {
        self.0.put(key, value)
    }

    pub fn clear<K: AsRef<std::ffi::OsStr>>(&mut self, key: K) {
        self.0.clear(key)
    }

    pub fn argv(&self) -> std::env::ArgsOs {
        std::env::args_os()
    }

    pub fn php_self(&self) -> Option<std::ffi::OsString> {
        self.argv().next()
    }
}

/// PHP superglobal $_SERVER. $_SERVER is a snapshot at startup. Modifying it does not affect the
/// real environment variables, while putenv() does.
/// TODO(php-runtime): modify the real PHP's $_SERVER.
pub static PHP_SERVER: std::sync::LazyLock<std::sync::Mutex<SuperglobalServer>> =
    std::sync::LazyLock::new(|| std::sync::Mutex::new(SuperglobalServer::from_env_vars()));

/// PHP superglobal $_ENV. $_ENV is a snapshot at startup. Modifying it does not affect the real
/// environment variables, while putenv() does.
/// TODO(php-runtime): modify the real PHP's $_ENV.
pub static PHP_ENV: std::sync::LazyLock<std::sync::Mutex<Superglobal>> =
    std::sync::LazyLock::new(|| std::sync::Mutex::new(Superglobal::from_env_vars()));