aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-php-shim/src/env.rs
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-06-21 15:49:24 +0900
committernsfisis <nsfisis@gmail.com>2026-06-21 15:49:37 +0900
commitfcef25f6ef36287a4984ffdaab39df84f5aceeba (patch)
tree97ceb2098fbb5642e858929da9578bb41e277ada /crates/shirabe-php-shim/src/env.rs
parentf0f5f084c883dc4f5b6e61603e82cd1c2092fd9d (diff)
downloadphp-shirabe-fcef25f6ef36287a4984ffdaab39df84f5aceeba.tar.gz
php-shirabe-fcef25f6ef36287a4984ffdaab39df84f5aceeba.tar.zst
php-shirabe-fcef25f6ef36287a4984ffdaab39df84f5aceeba.zip
refactor(php-shim): split lib.rs
Diffstat (limited to 'crates/shirabe-php-shim/src/env.rs')
-rw-r--r--crates/shirabe-php-shim/src/env.rs100
1 files changed, 100 insertions, 0 deletions
diff --git a/crates/shirabe-php-shim/src/env.rs b/crates/shirabe-php-shim/src/env.rs
new file mode 100644
index 0000000..62dc666
--- /dev/null
+++ b/crates/shirabe-php-shim/src/env.rs
@@ -0,0 +1,100 @@
+use crate::PhpMixed;
+use indexmap::IndexMap;
+
+pub fn getenv(_name: &str) -> Option<String> {
+ std::env::var(_name).ok()
+}
+
+// TODO(phase-c): only the simple `^(\w+)(=(.+))?$` form is supported.
+pub fn putenv(setting: &str) -> bool {
+ let is_word =
+ |s: &str| !s.is_empty() && s.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_');
+ // A setting without `=` deletes the variable, mirroring PHP's putenv('NAME').
+ match setting.split_once('=') {
+ Some((name, value)) => {
+ if !is_word(name) {
+ panic!("putenv: unsupported setting format: {setting:?}");
+ }
+ unsafe {
+ std::env::set_var(name, value);
+ }
+ }
+ None => {
+ if !is_word(setting) {
+ panic!("putenv: unsupported setting format: {setting:?}");
+ }
+ unsafe {
+ std::env::remove_var(setting);
+ }
+ }
+ }
+ true
+}
+
+/// PHP superglobal $_SERVER access. In the CLI SAPI $_SERVER is populated from
+/// the environment, which is the only source modeled here.
+pub fn server_get(name: &str) -> Option<String> {
+ // TODO: is var_os() better?
+ std::env::var(name).ok()
+}
+
+// TODO(php-runtime): modify the real PHP's $_SERVER.
+pub fn server_set(_name: &str, _value: String) {}
+
+// TODO(php-runtime): modify the real PHP's $_SERVER.
+pub fn server_unset(_name: &str) {}
+
+pub fn server_contains_key(name: &str) -> bool {
+ std::env::var_os(name).is_some()
+}
+
+/// PHP superglobal $_ENV access.
+pub fn env_get(name: &str) -> Option<String> {
+ // TODO: is var_os() better?
+ std::env::var(name).ok()
+}
+
+// TODO(php-runtime): modify the real PHP's $_ENV.
+pub fn env_set(_name: &str, _value: String) {}
+
+// TODO(php-runtime): modify the real PHP's $_ENV.
+pub fn env_unset(_name: &str) {}
+
+pub fn env_contains_key(name: &str) -> bool {
+ std::env::var_os(name).is_some()
+}
+
+/// PHP `getenv()` with no argument: all environment variables.
+pub fn getenv_all() -> IndexMap<String, String> {
+ todo!()
+}
+
+/// PHP superglobal `$_ENV`.
+pub fn php_env() -> IndexMap<String, PhpMixed> {
+ todo!()
+}
+
+/// PHP superglobal `$_SERVER`.
+pub fn php_server() -> IndexMap<String, PhpMixed> {
+ todo!()
+}
+
+pub fn server(key: &str) -> String {
+ if key == "PHP_SELF" {
+ return server_php_self();
+ }
+ server_get(key).unwrap_or_default()
+}
+
+pub fn server_argv() -> Vec<String> {
+ std::env::args().collect()
+}
+
+pub fn server_php_self() -> String {
+ // CLI SAPI: $_SERVER['PHP_SELF'] is the path of the executed script, i.e. argv[0].
+ std::env::args().next().unwrap_or_default()
+}
+
+pub fn server_shell() -> Option<String> {
+ todo!()
+}