2010-04-17 13:49:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2010-04-24 00:22:16 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-04-17 13:49:58 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-04-17 13:49:58 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-01-15 13:29:43 +00:00
|
|
|
namespace Symfony\Component\Process;
|
|
|
|
|
2010-04-17 13:49:58 +01:00
|
|
|
/**
|
|
|
|
* Process is a thin wrapper around proc_* functions to ease
|
2010-09-19 23:40:30 +01:00
|
|
|
* start independent PHP processes.
|
2010-04-17 13:49:58 +01:00
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-03-24 08:13:58 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-04-17 13:49:58 +01:00
|
|
|
*/
|
|
|
|
class Process
|
|
|
|
{
|
2011-03-24 08:10:42 +00:00
|
|
|
private $commandline;
|
|
|
|
private $cwd;
|
|
|
|
private $env;
|
|
|
|
private $stdin;
|
|
|
|
private $timeout;
|
|
|
|
private $options;
|
|
|
|
private $exitcode;
|
|
|
|
private $status;
|
|
|
|
private $stdout;
|
|
|
|
private $stderr;
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param string $commandline The command line to run
|
|
|
|
* @param string $cwd The working directory
|
|
|
|
* @param array $env The environment variables
|
|
|
|
* @param string $stdin The STDIN content
|
|
|
|
* @param integer $timeout The timeout in seconds
|
|
|
|
* @param array $options An array of options for proc_open
|
|
|
|
*
|
|
|
|
* @throws \RuntimeException When proc_open is not installed
|
2011-03-24 08:13:58 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
2011-05-19 15:52:45 +01:00
|
|
|
public function __construct($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())
|
2010-04-17 13:49:58 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!function_exists('proc_open')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
|
|
|
|
}
|
2010-04-17 13:49:58 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->commandline = $commandline;
|
|
|
|
$this->cwd = null === $cwd ? getcwd() : $cwd;
|
2011-05-19 15:52:45 +01:00
|
|
|
if (null !== $env) {
|
|
|
|
$this->env = array();
|
|
|
|
foreach ($env as $key => $value) {
|
|
|
|
$this->env[(binary) $key] = (binary) $value;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->env = null;
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
$this->stdin = $stdin;
|
|
|
|
$this->timeout = $timeout;
|
2011-05-19 15:52:45 +01:00
|
|
|
$this->options = array_merge(array('suppress_errors' => true, 'binary_pipes' => true, 'bypass_shell' => false), $options);
|
2010-04-17 13:49:58 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
2010-12-20 09:30:59 +00:00
|
|
|
* Runs the process.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* The callback receives the type of output (out or err) and
|
|
|
|
* some bytes from the output in real-time. It allows to have feedback
|
2010-09-19 23:40:30 +01:00
|
|
|
* from the independent process during execution.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
2011-04-01 17:07:53 +01:00
|
|
|
* The STDOUT and STDERR are also available after the process is finished
|
|
|
|
* via the getOutput() and getErrorOutput() methods.
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @param Closure|string|array $callback A PHP callback to run whenever there is some
|
|
|
|
* output available on STDOUT or STDERR
|
|
|
|
*
|
|
|
|
* @return integer The exit status code
|
|
|
|
*
|
|
|
|
* @throws \RuntimeException When process can't be launch or is stopped
|
2011-03-24 08:13:58 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function run($callback = null)
|
2010-04-18 08:27:43 +01:00
|
|
|
{
|
2011-04-01 17:07:53 +01:00
|
|
|
$this->stdout = '';
|
|
|
|
$this->stderr = '';
|
|
|
|
$that = $this;
|
|
|
|
$callback = function ($type, $line) use ($that, $callback)
|
|
|
|
{
|
|
|
|
if ('out' == $type) {
|
|
|
|
$that->addOutput($line);
|
|
|
|
} else {
|
|
|
|
$that->addErrorOutput($line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null !== $callback) {
|
|
|
|
call_user_func($callback, $type, $line);
|
|
|
|
}
|
|
|
|
};
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2011-05-14 16:25:55 +01:00
|
|
|
// Workaround for http://bugs.php.net/bug.php?id=51800
|
|
|
|
if (strstr(PHP_OS, 'WIN')) {
|
|
|
|
$stderrPipeMode = 'a';
|
|
|
|
} else {
|
|
|
|
$stderrPipeMode = 'w';
|
|
|
|
}
|
|
|
|
|
|
|
|
$descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', $stderrPipeMode));
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$process = proc_open($this->commandline, $descriptors, $pipes, $this->cwd, $this->env, $this->options);
|
|
|
|
|
|
|
|
stream_set_blocking($pipes[1], false);
|
|
|
|
stream_set_blocking($pipes[2], false);
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!is_resource($process)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \RuntimeException('Unable to launch a new process.');
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null !== $this->stdin) {
|
2010-05-06 12:25:53 +01:00
|
|
|
fwrite($pipes[0], (binary) $this->stdin);
|
|
|
|
}
|
|
|
|
fclose($pipes[0]);
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
while (true) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$r = $pipes;
|
|
|
|
$w = null;
|
|
|
|
$e = null;
|
|
|
|
|
|
|
|
$n = @stream_select($r, $w, $e, $this->timeout);
|
|
|
|
|
2010-12-21 02:59:17 +00:00
|
|
|
if (false === $n) {
|
2010-05-06 12:25:53 +01:00
|
|
|
break;
|
2010-05-07 15:09:11 +01:00
|
|
|
} elseif ($n === 0) {
|
2010-05-06 12:25:53 +01:00
|
|
|
proc_terminate($process);
|
|
|
|
|
|
|
|
throw new \RuntimeException('The process timed out.');
|
2010-05-07 15:09:11 +01:00
|
|
|
} elseif ($n > 0) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$called = false;
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
while (true) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$c = false;
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($line = (binary) fgets($pipes[1], 1024)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$called = $c = true;
|
|
|
|
call_user_func($callback, 'out', $line);
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($line = fgets($pipes[2], 1024)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$called = $c = true;
|
|
|
|
call_user_func($callback, 'err', $line);
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$c) {
|
2010-05-06 12:25:53 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$called) {
|
2010-05-06 12:25:53 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-04-18 08:27:43 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->status = proc_get_status($process);
|
2010-04-17 13:49:58 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
proc_close($process);
|
2010-04-17 13:49:58 +01:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($this->status['signaled']) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->status['stopsig']));
|
|
|
|
}
|
2010-04-17 13:49:58 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->exitcode = $this->status['exitcode'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the output of the process (STDOUT).
|
|
|
|
*
|
|
|
|
* This only returns the output if you have not supplied a callback
|
|
|
|
* to the run() method.
|
|
|
|
*
|
|
|
|
* @return string The process output
|
2011-03-24 08:13:58 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getOutput()
|
2010-04-17 13:49:58 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->stdout;
|
2010-04-17 13:49:58 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns the error output of the process (STDERR).
|
|
|
|
*
|
|
|
|
* This only returns the error output if you have not supplied a callback
|
|
|
|
* to the run() method.
|
|
|
|
*
|
|
|
|
* @return string The process error output
|
2011-03-24 08:13:58 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getErrorOutput()
|
2010-04-17 13:49:58 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->stderr;
|
2010-04-17 13:49:58 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns the exit code returned by the process.
|
|
|
|
*
|
|
|
|
* @return integer The exit status code
|
2011-03-24 08:13:58 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getExitCode()
|
2010-04-17 13:49:58 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
return $this->exitcode;
|
|
|
|
}
|
2010-04-17 13:49:58 +01:00
|
|
|
|
2010-09-16 07:55:44 +01:00
|
|
|
/**
|
|
|
|
* Checks if the process ended successfully.
|
|
|
|
*
|
|
|
|
* @return Boolean true if the process ended successfully, false otherwise
|
2011-03-24 08:13:58 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-09-16 07:55:44 +01:00
|
|
|
*/
|
|
|
|
public function isSuccessful()
|
|
|
|
{
|
|
|
|
return 0 == $this->exitcode;
|
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the child process has been terminated by an uncaught signal.
|
|
|
|
*
|
|
|
|
* It always returns false on Windows.
|
|
|
|
*
|
|
|
|
* @return Boolean
|
2011-03-24 08:13:58 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function hasBeenSignaled()
|
|
|
|
{
|
|
|
|
return $this->status['signaled'];
|
|
|
|
}
|
2010-04-17 13:49:58 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns the number of the signal that caused the child process to terminate its execution.
|
|
|
|
*
|
|
|
|
* It is only meaningful if hasBeenSignaled() returns true.
|
|
|
|
*
|
|
|
|
* @return integer
|
2011-03-24 08:13:58 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getTermSignal()
|
|
|
|
{
|
|
|
|
return $this->status['termsig'];
|
2010-04-17 13:49:58 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the child process has been stopped by a signal.
|
|
|
|
*
|
|
|
|
* It always returns false on Windows.
|
|
|
|
*
|
|
|
|
* @return Boolean
|
2011-03-24 08:13:58 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function hasBeenStopped()
|
|
|
|
{
|
|
|
|
return $this->status['stopped'];
|
|
|
|
}
|
2010-04-17 13:49:58 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Returns the number of the signal that caused the child process to stop its execution
|
|
|
|
*
|
|
|
|
* It is only meaningful if hasBeenStopped() returns true.
|
|
|
|
*
|
|
|
|
* @return integer
|
2011-03-24 08:13:58 +00:00
|
|
|
*
|
|
|
|
* @api
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function getStopSignal()
|
|
|
|
{
|
|
|
|
return $this->status['stopsig'];
|
|
|
|
}
|
2010-04-17 13:49:58 +01:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function addOutput($line)
|
2010-04-17 13:49:58 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->stdout .= $line;
|
2010-04-17 13:49:58 +01:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
public function addErrorOutput($line)
|
|
|
|
{
|
|
|
|
$this->stderr .= $line;
|
|
|
|
}
|
2011-03-24 08:10:42 +00:00
|
|
|
|
2011-03-24 08:15:33 +00:00
|
|
|
public function getCommandLine()
|
|
|
|
{
|
|
|
|
return $this->commandline;
|
|
|
|
}
|
|
|
|
|
2011-03-24 08:10:42 +00:00
|
|
|
public function setCommandLine($commandline)
|
|
|
|
{
|
|
|
|
$this->commandline = $commandline;
|
|
|
|
}
|
2011-04-02 09:08:01 +01:00
|
|
|
|
|
|
|
public function getTimeout()
|
|
|
|
{
|
|
|
|
return $this->timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setTimeout($timeout)
|
|
|
|
{
|
|
|
|
$this->timeout = $timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getWorkingDirectory()
|
|
|
|
{
|
|
|
|
return $this->cwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setWorkingDirectory($cwd)
|
|
|
|
{
|
|
|
|
$this->cwd = $cwd;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEnv()
|
|
|
|
{
|
|
|
|
return $this->env;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setEnv(array $env)
|
|
|
|
{
|
|
|
|
$this->env = $env;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getStdin()
|
|
|
|
{
|
|
|
|
return $this->stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setStdin($stdin)
|
|
|
|
{
|
|
|
|
$this->stdin = $stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getOptions()
|
|
|
|
{
|
|
|
|
return $this->options;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setOptions(array $options)
|
|
|
|
{
|
|
|
|
$this->options = $options;
|
|
|
|
}
|
2010-04-17 13:49:58 +01:00
|
|
|
}
|