Public feed #601573

Untitled

public text v1 · immutable
Raw Edit
·published 2007-07-03 16:18 UTC
rendered paste body
<?php
$cmd = 'php-gtk test.php';

$process = new CycleProcess($cmd, $process);
echo get_class($process).":".$process->get_command().":".$process->get_pid()."\n";
$process->terminate();
echo get_class($process)."\n"; // test for object

class CycleProcess {
	
	protected $process = null;
	protected $stdin = null;
	protected $stdout = null;
	protected $stderr = null;
	
	protected $self = null;
	
	public function __construct($cmd, &$process) {
		$descriptor = array(
			0 => array("pipe", "r"),	// stdin
			1 => array("pipe", "w"),	// stdout
			2 => array("pipe", "w")		// stderr
		);
		$this->process = proc_open($cmd, $descriptor, $pipes);
		if (is_resource($this->process)) {
			$this->stdin = $pipes[0];
			$this->stdout = $pipes[1];
			$this->stderr = $pipes[2];
		} else {
			throw new Exception("Failed to start process with command: $cmd");
		}
		$this->self =& $process;
	}
	
	public function get_pid() {
		$info = proc_get_status($this->process);
		return $info['pid'];
	}
	
	public function get_command() {
		$info = proc_get_status($this->process);
		return $info['command'];		
	}
	
	public function get_running() {
		$info = proc_get_status($this->process);
		if ($info['running'] == 1) return true;
		return false;
	}
	
	public function terminate() {
		fclose($this->stdin);
		fclose($this->stdout);
		fclose($this->stderr);
		proc_terminate($this->process);
		$this->self &= null;
	}
	
	public function connect($signal, $callback) {
		// write code to add callbacks for
		// stderr and stdout
		// signal: bool stdout(stream $stdout)
		// signal: bool stderr(stream $stderr)
	}
	
	public function connect_simple($signal, $callback) {
		// write code to add callbacks for
		// stderr and stdout
		// signal: bool stdout(stream $stdout)
		// signal: bool stderr(stream $stderr)
	}
	
	protected function trigger($signal) {
		// trigger signal callbacks
	}
	
	protected function trigger_simple($signal) {
		// trigger signal callbacks
	}
	
}