2010-04-17 13:49:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Components\Process;
|
|
|
|
|
|
|
|
/*
|
2010-04-24 00:22:16 +01:00
|
|
|
* This file is part of the Symfony package.
|
2010-04-17 13:49:58 +01:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process is a thin wrapper around proc_* functions to ease
|
|
|
|
* the forking processes from PHP.
|
|
|
|
*
|
|
|
|
* @package Symfony
|
|
|
|
* @subpackage Components_Process
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class Process
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
protected $commandline;
|
|
|
|
protected $cwd;
|
|
|
|
protected $env;
|
|
|
|
protected $stdin;
|
|
|
|
protected $timeout;
|
|
|
|
protected $options;
|
|
|
|
protected $exitcode;
|
|
|
|
protected $status;
|
|
|
|
protected $stdout;
|
|
|
|
protected $stderr;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function __construct($commandline, $cwd, array $env = array(), $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;
|
|
|
|
$this->env = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($env as $key => $value) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->env[(binary) $key] = (binary) $value;
|
|
|
|
}
|
|
|
|
$this->stdin = $stdin;
|
|
|
|
$this->timeout = $timeout;
|
|
|
|
$this->options = array_merge($options, array('suppress_errors' => true, 'binary_pipes' => true));
|
2010-04-17 13:49:58 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Forks and run the process.
|
|
|
|
*
|
|
|
|
* The callback receives the type of output (out or err) and
|
|
|
|
* some bytes from the output in real-time. It allows to have feedback
|
|
|
|
* from the forked process during exection.
|
|
|
|
*
|
|
|
|
* If you don't provide a callback, the STDOUT and STDERR are available only after
|
|
|
|
* the process is finished via the getOutput() and getErrorOutput() methods.
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
public function run($callback = null)
|
2010-04-18 08:27:43 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (null === $callback) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->stdout = '';
|
|
|
|
$this->stderr = '';
|
|
|
|
$that = $this;
|
|
|
|
$callback = function ($type, $line) use ($that)
|
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if ('out' == $type) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$that->addOutput($line);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$that->addErrorOutput($line);
|
|
|
|
}
|
|
|
|
};
|
2010-04-18 08:27:43 +01:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));
|
|
|
|
|
|
|
|
$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-05-07 15:09:11 +01:00
|
|
|
if ($n === false) {
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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-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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
2010-04-17 13:49:58 +01:00
|
|
|
}
|