diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-08-09 11:14:42 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-08-09 11:14:42 +0900 |
| commit | 446f719f6c34453f027d5ccdbf81b16e63f4c982 (patch) | |
| tree | 9c0443adbb99da5891a81d0131fc414d9a04c8ef /crates/shirabe-symfony-process/src/pipes/unix_pipes.rs | |
| parent | 880ba0fd1d05faf98588cc326b3d9fbe625ebf2b (diff) | |
| download | php-shirabe-446f719f6c34453f027d5ccdbf81b16e63f4c982.tar.gz php-shirabe-446f719f6c34453f027d5ccdbf81b16e63f4c982.tar.zst php-shirabe-446f719f6c34453f027d5ccdbf81b16e63f4c982.zip | |
refactor(symfony-process): extract symfony/process into the shirabe-symfony-process crate
Move `Symfony\Component\Process` out of shirabe-external-packages and
into its own crate, so the path is `shirabe_symfony_process::Process`
instead of `shirabe_external_packages::symfony::process::Process`.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe-symfony-process/src/pipes/unix_pipes.rs')
| -rw-r--r-- | crates/shirabe-symfony-process/src/pipes/unix_pipes.rs | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/crates/shirabe-symfony-process/src/pipes/unix_pipes.rs b/crates/shirabe-symfony-process/src/pipes/unix_pipes.rs new file mode 100644 index 00000000..bfc93d44 --- /dev/null +++ b/crates/shirabe-symfony-process/src/pipes/unix_pipes.rs @@ -0,0 +1,136 @@ +//! ref: composer/vendor/symfony/process/Pipes/UnixPipes.php + +use crate::pipes::abstract_pipes::AbstractPipes; +use crate::pipes::pipes_interface::{CHUNK_SIZE, PipesInterface}; +use crate::process::Process; +use indexmap::IndexMap; +use shirabe_php_shim::{Descriptor, PhpMixed, PhpResource}; + +/// UnixPipes implementation uses unix pipes as handles. +#[derive(Debug)] +pub struct UnixPipes { + inner: AbstractPipes, + tty_mode: Option<bool>, +} + +impl UnixPipes { + pub fn new(tty_mode: Option<bool>, input: PhpMixed) -> Self { + Self { + inner: AbstractPipes::new(input), + tty_mode, + } + } +} + +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()), + _ => panic!("unsupported descriptor spec: {:?}", items), + } +} + +impl PipesInterface for UnixPipes { + fn get_descriptors(&mut self) -> Vec<Descriptor> { + if self.tty_mode == Some(true) { + return vec![ + descriptor(&["file", "/dev/tty", "r"]), + descriptor(&["file", "/dev/tty", "w"]), + descriptor(&["file", "/dev/tty", "w"]), + ]; + } + + vec![ + descriptor(&["pipe", "r"]), + descriptor(&["pipe", "w"]), + descriptor(&["pipe", "w"]), + ] + } + + fn get_files(&self) -> IndexMap<i64, String> { + IndexMap::new() + } + + 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()) + && shirabe_php_shim::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 = shirabe_php_shim::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 && shirabe_php_shim::feof(pipe) { + shirabe_php_shim::fclose(pipe); + self.inner.pipes.shift_remove(fd); + } + } + + read + } + + fn are_open(&self) -> bool { + !self.inner.pipes.is_empty() + } + + fn close(&mut self) { + self.inner.close(); + } + + fn pipes(&self) -> &IndexMap<i64, PhpResource> { + &self.inner.pipes + } + + fn pipes_mut(&mut self) -> &mut IndexMap<i64, PhpResource> { + &mut self.inner.pipes + } +} |
