Changeset View
Changeset View
Standalone View
Standalone View
src/future/exec/PhutilExecPassthru.php
Show All 14 Lines | |||||
* | * | ||||
* You can set the current working directory for the command with | * You can set the current working directory for the command with | ||||
* @{method:setCWD}, and set the environment with @{method:setEnv}. | * @{method:setCWD}, and set the environment with @{method:setEnv}. | ||||
* | * | ||||
* @task command Executing Passthru Commands | * @task command Executing Passthru Commands | ||||
*/ | */ | ||||
final class PhutilExecPassthru extends PhutilExecutableFuture { | final class PhutilExecPassthru extends PhutilExecutableFuture { | ||||
private $stdinData; | |||||
public function write($data) { | |||||
$this->stdinData = $data; | |||||
return $this; | |||||
} | |||||
/* -( Executing Passthru Commands )---------------------------------------- */ | /* -( Executing Passthru Commands )---------------------------------------- */ | ||||
/** | /** | ||||
* Execute this command. | * Execute this command. | ||||
* | * | ||||
* @return int Error code returned by the subprocess. | * @return int Error code returned by the subprocess. | ||||
* | * | ||||
* @task command | * @task command | ||||
*/ | */ | ||||
public function execute() { | public function execute() { | ||||
$command = $this->getCommand(); | $command = $this->getCommand(); | ||||
$spec = array(STDIN, STDOUT, STDERR); | $is_write = ($this->stdinData !== null); | ||||
if ($is_write) { | |||||
$stdin_spec = array('pipe', 'r'); | |||||
} else { | |||||
$stdin_spec = STDIN; | |||||
} | |||||
$spec = array($stdin_spec, STDOUT, STDERR); | |||||
$pipes = array(); | $pipes = array(); | ||||
$unmasked_command = $command->getUnmaskedString(); | $unmasked_command = $command->getUnmaskedString(); | ||||
if ($this->hasEnv()) { | if ($this->hasEnv()) { | ||||
$env = $this->getEnv(); | $env = $this->getEnv(); | ||||
} else { | } else { | ||||
$env = null; | $env = null; | ||||
Show All 30 Lines | if (!is_resource($proc)) { | ||||
} else { | } else { | ||||
throw new Exception( | throw new Exception( | ||||
pht( | pht( | ||||
'Failed to passthru %s: %s', | 'Failed to passthru %s: %s', | ||||
'proc_open()', | 'proc_open()', | ||||
$errors)); | $errors)); | ||||
} | } | ||||
} else { | } else { | ||||
if ($is_write) { | |||||
fwrite($pipes[0], $this->stdinData); | |||||
fclose($pipes[0]); | |||||
} | |||||
$err = proc_close($proc); | $err = proc_close($proc); | ||||
} | } | ||||
return $err; | return $err; | ||||
} | } | ||||
/* -( Future )------------------------------------------------------------- */ | /* -( Future )------------------------------------------------------------- */ | ||||
Show All 38 Lines |