aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-06-24 03:09:17 +0900
committernsfisis <nsfisis@gmail.com>2026-06-24 03:09:17 +0900
commitc5fc34a106a706ac12925c4df273a926811d260b (patch)
tree24e798c4a00b32aa1a1f155b705d02fba9895875 /crates/shirabe-external-packages
parentb2b321a26f6a628ee8e6757eb8577613e2024e70 (diff)
downloadphp-shirabe-c5fc34a106a706ac12925c4df273a926811d260b.tar.gz
php-shirabe-c5fc34a106a706ac12925c4df273a926811d260b.tar.zst
php-shirabe-c5fc34a106a706ac12925c4df273a926811d260b.zip
feat(process): redesign proc_* on PhpResource process handles
proc_open/proc_close/proc_get_status/proc_terminate represented the process handle as a PhpMixed, which cannot hold a live child process or its pipes, so they were stubs or todo!(). Model the handle as a new PhpResource::Process variant and child pipes as a StreamBacking::Pipe, with a native Descriptor enum for descriptorspec; proc_open now returns io::Result and fills pipes as IndexMap<i64, PhpResource>. Rewire the Symfony Process pipes and the Console terminal/cursor onto the new types, removing the "PhpMixed cannot carry a PhpResource" todo!()s. The remaining todo!()s are genuine syscall leaves (proc_terminate signal delivery, stream_select, stream_set_blocking, posix_kill, pty, fd>=3) left unimplemented since no syscall crate is introduced. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-external-packages')
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/cursor.rs27
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs15
-rw-r--r--crates/shirabe-external-packages/src/symfony/console/terminal.rs67
-rw-r--r--crates/shirabe-external-packages/src/symfony/process/pipes/abstract_pipes.rs63
-rw-r--r--crates/shirabe-external-packages/src/symfony/process/pipes/pipes_interface.rs10
-rw-r--r--crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs105
-rw-r--r--crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs14
-rw-r--r--crates/shirabe-external-packages/src/symfony/process/process.rs98
8 files changed, 246 insertions, 153 deletions
diff --git a/crates/shirabe-external-packages/src/symfony/console/cursor.rs b/crates/shirabe-external-packages/src/symfony/console/cursor.rs
index 811553f..c36f9b5 100644
--- a/crates/shirabe-external-packages/src/symfony/console/cursor.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/cursor.rs
@@ -181,31 +181,20 @@ impl Cursor {
let is_tty_supported = if shirabe_php_shim::function_exists("proc_open") {
*IS_TTY_SUPPORTED.get_or_init(|| {
- let mut pipes = shirabe_php_shim::PhpMixed::Null;
- shirabe_php_shim::php_truthy(&shirabe_php_shim::proc_open(
+ let mut pipes = indexmap::IndexMap::new();
+ shirabe_php_shim::proc_open(
"echo 1 >/dev/null",
- &vec![
- shirabe_php_shim::PhpMixed::List(vec![
- shirabe_php_shim::PhpMixed::String("file".to_string()),
- shirabe_php_shim::PhpMixed::String("/dev/tty".to_string()),
- shirabe_php_shim::PhpMixed::String("r".to_string()),
- ]),
- shirabe_php_shim::PhpMixed::List(vec![
- shirabe_php_shim::PhpMixed::String("file".to_string()),
- shirabe_php_shim::PhpMixed::String("/dev/tty".to_string()),
- shirabe_php_shim::PhpMixed::String("w".to_string()),
- ]),
- shirabe_php_shim::PhpMixed::List(vec![
- shirabe_php_shim::PhpMixed::String("file".to_string()),
- shirabe_php_shim::PhpMixed::String("/dev/tty".to_string()),
- shirabe_php_shim::PhpMixed::String("w".to_string()),
- ]),
+ &[
+ shirabe_php_shim::Descriptor::File("/dev/tty".to_string(), "r".to_string()),
+ shirabe_php_shim::Descriptor::File("/dev/tty".to_string(), "w".to_string()),
+ shirabe_php_shim::Descriptor::File("/dev/tty".to_string(), "w".to_string()),
],
&mut pipes,
None,
None,
None,
- ))
+ )
+ .is_ok()
})
} else {
false
diff --git a/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs b/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs
index 1fc90bd..2bdd929 100644
--- a/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/helper/question_helper.rs
@@ -377,13 +377,14 @@ impl QuestionHelper {
// Read a keypress
while !shirabe_php_shim::feof(input_stream) {
while is_stdin
- && 0 == shirabe_php_shim::stream_select(
- &mut r,
- &mut w.clone(),
- &mut w.clone(),
- 0,
- Some(100),
- )
+ && Some(0)
+ == shirabe_php_shim::stream_select(
+ &mut r,
+ &mut w.clone(),
+ &mut w.clone(),
+ 0,
+ Some(100),
+ )
{
// Give signal handlers a chance to run
r = vec![input_stream.clone()];
diff --git a/crates/shirabe-external-packages/src/symfony/console/terminal.rs b/crates/shirabe-external-packages/src/symfony/console/terminal.rs
index ac4009e..c02b87f 100644
--- a/crates/shirabe-external-packages/src/symfony/console/terminal.rs
+++ b/crates/shirabe-external-packages/src/symfony/console/terminal.rs
@@ -208,49 +208,48 @@ impl Terminal {
return None;
}
- let descriptorspec: Vec<PhpMixed> = vec![
- PhpMixed::List(vec![
- PhpMixed::String("pipe".to_string()),
- PhpMixed::String("w".to_string()),
- ]),
- PhpMixed::List(vec![
- PhpMixed::String("pipe".to_string()),
- PhpMixed::String("w".to_string()),
- ]),
+ // Sparse PHP descriptorspec `[1 => ['pipe', 'w'], 2 => ['pipe', 'w']]`: fd 0 is inherited.
+ let descriptorspec = [
+ shirabe_php_shim::Descriptor::Inherit,
+ shirabe_php_shim::Descriptor::Pipe("w".to_string()),
+ shirabe_php_shim::Descriptor::Pipe("w".to_string()),
];
- let _cp = if shirabe_php_shim::function_exists("sapi_windows_cp_set") {
+ let cp = if shirabe_php_shim::function_exists("sapi_windows_cp_set") {
shirabe_php_shim::sapi_windows_cp_get(None)
} else {
0
};
- let mut pipes = PhpMixed::Null;
- let process =
- shirabe_php_shim::proc_open(command, &descriptorspec, &mut pipes, None, None, None);
- if !shirabe_php_shim::php_truthy(&process) {
- return None;
+ let mut pipes: indexmap::IndexMap<i64, shirabe_php_shim::PhpResource> =
+ indexmap::IndexMap::new();
+ let process = match shirabe_php_shim::proc_open(
+ command,
+ &descriptorspec,
+ &mut pipes,
+ None,
+ None,
+ None,
+ ) {
+ Ok(process) => process,
+ Err(_) => return None,
+ };
+
+ let info = pipes
+ .get(&1)
+ .and_then(shirabe_php_shim::stream_get_contents);
+ if let Some(pipe) = pipes.get(&1) {
+ shirabe_php_shim::fclose(pipe);
}
+ if let Some(pipe) = pipes.get(&2) {
+ shirabe_php_shim::fclose(pipe);
+ }
+ shirabe_php_shim::proc_close(&process);
- // $pipes[1] and $pipes[2] from proc_open's output array.
- let _pipe1 = php_pipe(&pipes, 1);
- let _pipe2 = php_pipe(&pipes, 2);
- // TODO(phase-d): the pipe contents are read via stream_get_contents() and the pipes are
- // fclose()d, but they are PHP stream resources the PhpMixed pipe list cannot hold.
- todo!(
- "Terminal: reading proc_open pipes needs PhpResource handles the PhpMixed pipe list cannot carry"
- );
- }
-}
+ if cp != 0 {
+ shirabe_php_shim::sapi_windows_cp_set(cp);
+ }
-/// Indexes into the `$pipes` array populated by proc_open.
-fn php_pipe(pipes: &PhpMixed, index: i64) -> PhpMixed {
- match pipes {
- PhpMixed::List(list) => list.get(index as usize).cloned().unwrap_or(PhpMixed::Null),
- PhpMixed::Array(array) => array
- .get(&index.to_string())
- .map(|v| v.clone())
- .unwrap_or(PhpMixed::Null),
- _ => PhpMixed::Null,
+ info
}
}
diff --git a/crates/shirabe-external-packages/src/symfony/process/pipes/abstract_pipes.rs b/crates/shirabe-external-packages/src/symfony/process/pipes/abstract_pipes.rs
index d8ced43..5b85813 100644
--- a/crates/shirabe-external-packages/src/symfony/process/pipes/abstract_pipes.rs
+++ b/crates/shirabe-external-packages/src/symfony/process/pipes/abstract_pipes.rs
@@ -1,11 +1,11 @@
//! ref: composer/vendor/symfony/process/Pipes/AbstractPipes.php
use indexmap::IndexMap;
-use shirabe_php_shim::{self as php, PhpMixed};
+use shirabe_php_shim::{self as php, PhpMixed, PhpResource};
#[derive(Debug)]
pub struct AbstractPipes {
- pub pipes: PhpMixed,
+ pub pipes: IndexMap<i64, PhpResource>,
input_buffer: String,
input: PhpMixed,
@@ -17,6 +17,8 @@ impl AbstractPipes {
pub fn new(input: PhpMixed) -> Self {
let mut input_buffer = String::new();
let stored_input;
+ // TODO(plugin): `$input instanceof \Iterator` is not modeled. `is_resource` on a PhpMixed is
+ // always false, so the resource branch never stores input here.
if php::is_resource(&input) {
stored_input = input;
} else if let PhpMixed::String(s) = &input {
@@ -28,7 +30,7 @@ impl AbstractPipes {
}
Self {
- pipes: PhpMixed::List(Vec::new()),
+ pipes: IndexMap::new(),
input_buffer,
input: stored_input,
blocked: true,
@@ -37,9 +39,10 @@ impl AbstractPipes {
}
pub fn close(&mut self) {
- // TODO(phase-d): each pipe is a PHP stream resource that should be fclose()d, but the pipe
- // list is a PhpMixed that cannot hold a PhpResource; the handles are dropped instead.
- self.pipes = PhpMixed::List(Vec::new());
+ for (_, pipe) in &self.pipes {
+ php::fclose(pipe);
+ }
+ self.pipes = IndexMap::new();
}
/// Returns true if a system call has been interrupted.
@@ -54,14 +57,52 @@ impl AbstractPipes {
/// Unblocks streams.
pub(crate) fn unblock(&mut self) {
- let _ = &self.input_buffer;
- let _ = &self.blocked;
- todo!()
+ if !self.blocked {
+ return;
+ }
+
+ for (_, pipe) in &self.pipes {
+ php::stream_set_blocking(pipe, false);
+ }
+ // The `is_resource($this->input)` branch does not apply: `input` is never a resource in this
+ // port (is_resource on a PhpMixed is always false).
+
+ self.blocked = false;
}
/// Writes input to stdin.
- pub(crate) fn write(&mut self) -> Option<IndexMap<i64, PhpMixed>> {
- todo!()
+ pub(crate) fn write(&mut self) -> Option<Vec<PhpResource>> {
+ let stdin = self.pipes.get(&0)?.clone();
+
+ // TODO(plugin): the `$input instanceof \Iterator` branch is not modeled. `input` is never a
+ // resource here, so the fread($input)/stream_set_blocking($input) paths do not apply and
+ // only the input buffer is written to stdin.
+
+ let mut r: Vec<PhpResource> = Vec::new();
+ let mut e: Vec<PhpResource> = Vec::new();
+ let mut w: Vec<PhpResource> = vec![stdin.clone()];
+
+ // let's have a look if something changed in streams
+ if php::stream_select(&mut r, &mut w, &mut e, 0, Some(0)).is_none() {
+ return None;
+ }
+
+ if !self.input_buffer.is_empty() {
+ let written = php::fwrite(&stdin, &self.input_buffer, None).unwrap_or(0) as usize;
+ self.input_buffer = self.input_buffer.get(written..).unwrap_or("").to_string();
+ if !self.input_buffer.is_empty() {
+ return Some(vec![stdin]);
+ }
+ }
+
+ // no input to read on resource, buffer is empty
+ if self.input_buffer.is_empty() && !php::php_truthy(&self.input) {
+ self.input = PhpMixed::Null;
+ php::fclose(&stdin);
+ self.pipes.shift_remove(&0);
+ }
+
+ None
}
pub fn handle_error(&mut self, _type: i64, msg: String) {
diff --git a/crates/shirabe-external-packages/src/symfony/process/pipes/pipes_interface.rs b/crates/shirabe-external-packages/src/symfony/process/pipes/pipes_interface.rs
index e10b3b3..92d584d 100644
--- a/crates/shirabe-external-packages/src/symfony/process/pipes/pipes_interface.rs
+++ b/crates/shirabe-external-packages/src/symfony/process/pipes/pipes_interface.rs
@@ -1,14 +1,14 @@
//! ref: composer/vendor/symfony/process/Pipes/PipesInterface.php
use indexmap::IndexMap;
-use shirabe_php_shim::PhpMixed;
+use shirabe_php_shim::{Descriptor, PhpResource};
pub const CHUNK_SIZE: i64 = 16384;
/// PipesInterface manages descriptors and pipes for the use of proc_open.
pub trait PipesInterface: std::fmt::Debug {
/// Returns an array of descriptors for the use of proc_open.
- fn get_descriptors(&mut self) -> Vec<PhpMixed>;
+ fn get_descriptors(&mut self) -> Vec<Descriptor>;
/// Returns an array of filenames indexed by their related stream in case these pipes use temporary files.
fn get_files(&self) -> IndexMap<i64, String>;
@@ -25,7 +25,7 @@ pub trait PipesInterface: std::fmt::Debug {
/// Closes file handles and pipes.
fn close(&mut self);
- /// Accessor for the `pipes` property populated by proc_open.
- fn pipes(&self) -> &PhpMixed;
- fn pipes_mut(&mut self) -> &mut PhpMixed;
+ /// Accessor for the `pipes` property populated by proc_open, keyed by fd index.
+ fn pipes(&self) -> &IndexMap<i64, PhpResource>;
+ fn pipes_mut(&mut self) -> &mut IndexMap<i64, PhpResource>;
}
diff --git a/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs b/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs
index 983ed07..795bb64 100644
--- a/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs
+++ b/crates/shirabe-external-packages/src/symfony/process/pipes/unix_pipes.rs
@@ -1,10 +1,10 @@
//! ref: composer/vendor/symfony/process/Pipes/UnixPipes.php
use indexmap::IndexMap;
-use shirabe_php_shim::PhpMixed;
+use shirabe_php_shim::{self as php, Descriptor, PhpMixed, PhpResource};
use crate::symfony::process::pipes::abstract_pipes::AbstractPipes;
-use crate::symfony::process::pipes::pipes_interface::PipesInterface;
+use crate::symfony::process::pipes::pipes_interface::{CHUNK_SIZE, PipesInterface};
use crate::symfony::process::process::Process;
/// UnixPipes implementation uses unix pipes as handles.
@@ -32,24 +32,24 @@ impl UnixPipes {
}
}
-fn descriptor(items: &[&str]) -> PhpMixed {
- PhpMixed::List(
- items
- .iter()
- .map(|s| PhpMixed::String(s.to_string()))
- .collect(),
- )
+fn descriptor(items: &[&str]) -> Descriptor {
+ match items {
+ ["pipe", mode] => Descriptor::Pipe(mode.to_string()),
+ ["file", path, mode] => Descriptor::File(path.to_string(), mode.to_string()),
+ ["pty"] => Descriptor::Pty,
+ _ => panic!("unsupported descriptor spec: {:?}", items),
+ }
}
impl PipesInterface for UnixPipes {
- fn get_descriptors(&mut self) -> Vec<PhpMixed> {
+ fn get_descriptors(&mut self) -> Vec<Descriptor> {
if !self.have_read_support {
- // TODO(phase-d): /dev/null is opened as a stream resource and placed directly into the
- // proc_open descriptor spec, but the descriptor list is a Vec<PhpMixed> that cannot
- // carry a PhpResource.
- todo!(
- "UnixPipes::get_descriptors: the /dev/null resource cannot be represented in a PhpMixed descriptor list"
- );
+ let nullstream = php::fopen("/dev/null", "c").expect("fopen('/dev/null') failed");
+ return vec![
+ descriptor(&["pipe", "r"]),
+ Descriptor::Resource(nullstream.clone()),
+ Descriptor::Resource(nullstream),
+ ];
}
if self.tty_mode == Some(true) {
@@ -79,8 +79,71 @@ impl PipesInterface for UnixPipes {
IndexMap::new()
}
- fn read_and_write(&mut self, _blocking: bool, _close: bool) -> IndexMap<i64, String> {
- todo!()
+ fn read_and_write(&mut self, blocking: bool, close: bool) -> IndexMap<i64, String> {
+ self.inner.unblock();
+ let w = self.inner.write();
+
+ let mut read: IndexMap<i64, String> = IndexMap::new();
+ // $r = $this->pipes; unset($r[0]);
+ let r: Vec<(i64, PhpResource)> = self
+ .inner
+ .pipes
+ .iter()
+ .filter(|(fd, _)| **fd != 0)
+ .map(|(fd, pipe)| (*fd, pipe.clone()))
+ .collect();
+
+ // TODO(plugin): set_error_handler/restore_error_handler around stream_select is not modeled.
+ let mut r_sel: Vec<PhpResource> = r.iter().map(|(_, p)| p.clone()).collect();
+ let mut w_sel: Vec<PhpResource> = w.clone().unwrap_or_default();
+ let mut e_sel: Vec<PhpResource> = Vec::new();
+
+ // let's have a look if something changed in streams
+ if (!r_sel.is_empty() || w.is_some())
+ && php::stream_select(
+ &mut r_sel,
+ &mut w_sel,
+ &mut e_sel,
+ 0,
+ Some(if blocking {
+ (Process::TIMEOUT_PRECISION * 1e6) as i64
+ } else {
+ 0
+ }),
+ )
+ .is_none()
+ {
+ // if a system call has been interrupted, forget about it, let's try again
+ // otherwise, an error occurred, let's reset pipes
+ if !self.inner.has_system_call_been_interrupted() {
+ self.inner.pipes = IndexMap::new();
+ }
+
+ return read;
+ }
+
+ for (fd, pipe) in &r {
+ let mut data = String::new();
+ loop {
+ let chunk = php::fread(pipe, CHUNK_SIZE).unwrap_or_default();
+ let len = chunk.len() as i64;
+ data.push_str(&chunk);
+ if !(len > 0 && (close || len >= CHUNK_SIZE)) {
+ break;
+ }
+ }
+
+ if !data.is_empty() {
+ read.insert(*fd, data);
+ }
+
+ if close && php::feof(pipe) {
+ php::fclose(pipe);
+ self.inner.pipes.shift_remove(fd);
+ }
+ }
+
+ read
}
fn have_read_support(&self) -> bool {
@@ -88,18 +151,18 @@ impl PipesInterface for UnixPipes {
}
fn are_open(&self) -> bool {
- shirabe_php_shim::php_truthy(&self.inner.pipes)
+ !self.inner.pipes.is_empty()
}
fn close(&mut self) {
self.inner.close();
}
- fn pipes(&self) -> &PhpMixed {
+ fn pipes(&self) -> &IndexMap<i64, PhpResource> {
&self.inner.pipes
}
- fn pipes_mut(&mut self) -> &mut PhpMixed {
+ fn pipes_mut(&mut self) -> &mut IndexMap<i64, PhpResource> {
&mut self.inner.pipes
}
}
diff --git a/crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs b/crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs
index 7602e7d..a31f184 100644
--- a/crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs
+++ b/crates/shirabe-external-packages/src/symfony/process/pipes/windows_pipes.rs
@@ -1,7 +1,7 @@
//! ref: composer/vendor/symfony/process/Pipes/WindowsPipes.php
use indexmap::IndexMap;
-use shirabe_php_shim::PhpMixed;
+use shirabe_php_shim::{Descriptor, PhpMixed, PhpResource};
use crate::symfony::process::pipes::abstract_pipes::AbstractPipes;
use crate::symfony::process::pipes::pipes_interface::PipesInterface;
@@ -11,8 +11,8 @@ use crate::symfony::process::pipes::pipes_interface::PipesInterface;
pub struct WindowsPipes {
inner: AbstractPipes,
files: IndexMap<i64, String>,
- file_handles: IndexMap<i64, PhpMixed>,
- lock_handles: IndexMap<i64, PhpMixed>,
+ file_handles: IndexMap<i64, PhpResource>,
+ lock_handles: IndexMap<i64, PhpResource>,
read_bytes: IndexMap<i64, i64>,
have_read_support: bool,
}
@@ -25,7 +25,7 @@ impl WindowsPipes {
}
impl PipesInterface for WindowsPipes {
- fn get_descriptors(&mut self) -> Vec<PhpMixed> {
+ fn get_descriptors(&mut self) -> Vec<Descriptor> {
let _ = (
&self.files,
&self.file_handles,
@@ -48,7 +48,7 @@ impl PipesInterface for WindowsPipes {
}
fn are_open(&self) -> bool {
- shirabe_php_shim::php_truthy(&self.inner.pipes) && !self.file_handles.is_empty()
+ !self.inner.pipes.is_empty() && !self.file_handles.is_empty()
}
fn close(&mut self) {
@@ -56,11 +56,11 @@ impl PipesInterface for WindowsPipes {
todo!()
}
- fn pipes(&self) -> &PhpMixed {
+ fn pipes(&self) -> &IndexMap<i64, PhpResource> {
&self.inner.pipes
}
- fn pipes_mut(&mut self) -> &mut PhpMixed {
+ fn pipes_mut(&mut self) -> &mut IndexMap<i64, PhpResource> {
&mut self.inner.pipes
}
}
diff --git a/crates/shirabe-external-packages/src/symfony/process/process.rs b/crates/shirabe-external-packages/src/symfony/process/process.rs
index d0d104e..caf8aa1 100644
--- a/crates/shirabe-external-packages/src/symfony/process/process.rs
+++ b/crates/shirabe-external-packages/src/symfony/process/process.rs
@@ -3,7 +3,7 @@
use std::sync::OnceLock;
use indexmap::IndexMap;
-use shirabe_php_shim::{self as php, PhpMixed, PhpResource};
+use shirabe_php_shim::{self as php, Descriptor, PhpMixed, PhpResource};
use crate::symfony::process::exception::invalid_argument_exception::InvalidArgumentException;
use crate::symfony::process::exception::logic_exception::LogicException;
@@ -50,7 +50,7 @@ pub struct Process {
output_disabled: bool,
stdout: Option<PhpResource>,
stderr: Option<PhpResource>,
- process: PhpMixed,
+ process: Option<PhpResource>,
status: String,
incremental_output_offset: i64,
incremental_error_output_offset: i64,
@@ -74,24 +74,12 @@ impl std::fmt::Debug for Process {
}
}
-fn descriptor(items: &[&str]) -> PhpMixed {
- PhpMixed::List(
- items
- .iter()
- .map(|s| PhpMixed::String(s.to_string()))
- .collect(),
- )
-}
-
-/// Index into the `$pipes` array (a PhpMixed list/array) populated by proc_open.
-fn php_pipe(pipes: &PhpMixed, index: i64) -> PhpMixed {
- match pipes {
- PhpMixed::List(list) => list.get(index as usize).cloned().unwrap_or(PhpMixed::Null),
- PhpMixed::Array(array) => array
- .get(&index.to_string())
- .cloned()
- .unwrap_or(PhpMixed::Null),
- _ => PhpMixed::Null,
+fn descriptor(items: &[&str]) -> Descriptor {
+ match items {
+ ["pipe", mode] => Descriptor::Pipe(mode.to_string()),
+ ["file", path, mode] => Descriptor::File(path.to_string(), mode.to_string()),
+ ["pty"] => Descriptor::Pty,
+ _ => panic!("unsupported descriptor spec: {:?}", items),
}
}
@@ -201,7 +189,7 @@ impl Process {
output_disabled: false,
stdout: None,
stderr: None,
- process: PhpMixed::Null,
+ process: None,
status: Self::STATUS_READY.to_string(),
incremental_output_offset: 0,
incremental_error_output_offset: 0,
@@ -397,9 +385,9 @@ impl Process {
Some(&options),
)
};
- self.process = process;
+ self.process = process.ok();
- if !php::php_truthy(&self.process) {
+ if self.process.is_none() {
return Err(
RuntimeException::new("Unable to launch a new process.".to_string()).into(),
);
@@ -407,12 +395,19 @@ impl Process {
self.status = Self::STATUS_STARTED.to_string();
if descriptors.len() > 3 {
- let _pipe3 = php_pipe(self.process_pipes.as_ref().unwrap().pipes(), 3);
- // TODO(phase-d): the pid is read with fgets() from pipe 3, a PHP stream resource. The
- // pipe list is a PhpMixed and cannot hold a PhpResource, so this cannot be expressed.
- todo!(
- "Process::start: reading the pid from pipe 3 needs a PhpResource the PhpMixed pipe list cannot carry"
- );
+ let pipe3 = self
+ .process_pipes
+ .as_ref()
+ .unwrap()
+ .pipes()
+ .get(&3)
+ .cloned();
+ let pid = pipe3
+ .and_then(|p| php::fgets(&p, None))
+ .map(|s| s.trim().parse::<i64>().unwrap_or(0))
+ .unwrap_or(0);
+ self.fallback_status
+ .insert("pid".to_string(), PhpMixed::Int(pid));
}
if self.tty {
@@ -1121,8 +1116,8 @@ impl Process {
static IS_TTY_SUPPORTED: OnceLock<bool> = OnceLock::new();
*IS_TTY_SUPPORTED.get_or_init(|| {
- let mut pipes = PhpMixed::Null;
- php::php_truthy(&php::proc_open(
+ let mut pipes = IndexMap::new();
+ php::proc_open(
"echo 1 >/dev/null",
&[
descriptor(&["file", "/dev/tty", "r"]),
@@ -1133,7 +1128,8 @@ impl Process {
None,
None,
None,
- ))
+ )
+ .is_ok()
})
}
@@ -1146,8 +1142,8 @@ impl Process {
return false;
}
- let mut pipes = PhpMixed::Null;
- php::php_truthy(&php::proc_open(
+ let mut pipes = IndexMap::new();
+ php::proc_open(
"echo 1 >/dev/null",
&[
descriptor(&["pty"]),
@@ -1158,12 +1154,13 @@ impl Process {
None,
None,
None,
- ))
+ )
+ .is_ok()
})
}
/// Creates the descriptors needed by the proc_open.
- fn get_descriptors(&mut self) -> Vec<PhpMixed> {
+ fn get_descriptors(&mut self) -> Vec<Descriptor> {
// TODO(plugin): $this->input instanceof \Iterator -> rewind() is not modeled.
if php::DIRECTORY_SEPARATOR == "\\" {
self.process_pipes = Some(Box::new(WindowsPipes::new(
@@ -1220,7 +1217,7 @@ impl Process {
return;
}
- self.process_information = Some(php::proc_get_status(&self.process));
+ self.process_information = Some(php::proc_get_status(self.process.as_ref().unwrap()));
let running = self
.process_information
.as_ref()
@@ -1358,9 +1355,9 @@ impl Process {
if let Some(p) = self.process_pipes.as_mut() {
p.close();
}
- if php::php_truthy(&self.process) {
- php::proc_close(self.process.clone());
- self.process = PhpMixed::Null;
+ if self.process.is_some() {
+ php::proc_close(self.process.as_ref().unwrap());
+ self.process = None;
}
self.exitcode = self
.process_information
@@ -1411,7 +1408,7 @@ impl Process {
Some(php::fopen(&format!("php://temp/maxmemory:{}", 1024 * 1024), "w+").unwrap());
self.stderr =
Some(php::fopen(&format!("php://temp/maxmemory:{}", 1024 * 1024), "w+").unwrap());
- self.process = PhpMixed::Null;
+ self.process = None;
self.latest_signal = None;
self.status = Self::STATUS_READY.to_string();
self.incremental_output_offset = 0;
@@ -1456,24 +1453,27 @@ impl Process {
} else {
let ok;
if !self.is_sigchild_enabled() {
- ok = php::proc_terminate(&self.process, signal);
+ ok = php::proc_terminate(self.process.as_ref().unwrap(), signal);
} else if php::function_exists("posix_kill") {
ok = php::posix_kill(pid, signal);
} else {
- let mut pipes = PhpMixed::Null;
+ let mut pipes = IndexMap::new();
let opened = php::proc_open(
&format!("kill -{} {}", signal, pid),
- &[PhpMixed::Null, PhpMixed::Null, descriptor(&["pipe", "w"])],
+ &[
+ Descriptor::Inherit,
+ Descriptor::Inherit,
+ descriptor(&["pipe", "w"]),
+ ],
&mut pipes,
None,
None,
None,
);
- let _pipe2 = php_pipe(&pipes, 2);
- // TODO(phase-d): the original also requires `fgets($pipes[2]) === false`, reading
- // from a PHP stream resource the PhpMixed pipe list cannot hold; that read is
- // dropped here.
- ok = php::php_truthy(&opened);
+ ok = match opened {
+ Ok(_) => pipes.get(&2).and_then(|p| php::fgets(p, None)).is_none(),
+ Err(_) => false,
+ };
}
if !ok {
if throw_exception {