123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Process;
- /**
- * Process is a thin wrapper around proc_* functions to ease
- * start independent PHP processes.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- *
- * @api
- */
- class Process
- {
- private $commandline;
- private $cwd;
- private $env;
- private $stdin;
- private $timeout;
- private $options;
- private $exitcode;
- private $status;
- private $stdout;
- private $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
- *
- * @api
- */
- public function __construct($commandline, $cwd = null, array $env = array(), $stdin = null, $timeout = 60, array $options = array())
- {
- if (!function_exists('proc_open')) {
- throw new \RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
- }
- $this->commandline = $commandline;
- $this->cwd = null === $cwd ? getcwd() : $cwd;
- $this->env = array();
- foreach ($env as $key => $value) {
- $this->env[(binary) $key] = (binary) $value;
- }
- $this->stdin = $stdin;
- $this->timeout = $timeout;
- $this->options = array_merge($options, array('suppress_errors' => true, 'binary_pipes' => true));
- }
- /**
- * Runs 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 independent process during execution.
- *
- * The STDOUT and STDERR are also available 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
- *
- * @api
- */
- public function run($callback = null)
- {
- $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);
- }
- };
- $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);
- if (!is_resource($process)) {
- throw new \RuntimeException('Unable to launch a new process.');
- }
- if (null !== $this->stdin) {
- fwrite($pipes[0], (binary) $this->stdin);
- }
- fclose($pipes[0]);
- while (true) {
- $r = $pipes;
- $w = null;
- $e = null;
- $n = @stream_select($r, $w, $e, $this->timeout);
- if (false === $n) {
- break;
- } elseif ($n === 0) {
- proc_terminate($process);
- throw new \RuntimeException('The process timed out.');
- } elseif ($n > 0) {
- $called = false;
- while (true) {
- $c = false;
- if ($line = (binary) fgets($pipes[1], 1024)) {
- $called = $c = true;
- call_user_func($callback, 'out', $line);
- }
- if ($line = fgets($pipes[2], 1024)) {
- $called = $c = true;
- call_user_func($callback, 'err', $line);
- }
- if (!$c) {
- break;
- }
- }
- if (!$called) {
- break;
- }
- }
- }
- $this->status = proc_get_status($process);
- proc_close($process);
- if ($this->status['signaled']) {
- throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->status['stopsig']));
- }
- 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
- *
- * @api
- */
- public function getOutput()
- {
- return $this->stdout;
- }
- /**
- * 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
- *
- * @api
- */
- public function getErrorOutput()
- {
- return $this->stderr;
- }
- /**
- * Returns the exit code returned by the process.
- *
- * @return integer The exit status code
- *
- * @api
- */
- public function getExitCode()
- {
- return $this->exitcode;
- }
- /**
- * Checks if the process ended successfully.
- *
- * @return Boolean true if the process ended successfully, false otherwise
- *
- * @api
- */
- public function isSuccessful()
- {
- return 0 == $this->exitcode;
- }
- /**
- * Returns true if the child process has been terminated by an uncaught signal.
- *
- * It always returns false on Windows.
- *
- * @return Boolean
- *
- * @api
- */
- public function hasBeenSignaled()
- {
- return $this->status['signaled'];
- }
- /**
- * 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
- *
- * @api
- */
- public function getTermSignal()
- {
- return $this->status['termsig'];
- }
- /**
- * Returns true if the child process has been stopped by a signal.
- *
- * It always returns false on Windows.
- *
- * @return Boolean
- *
- * @api
- */
- public function hasBeenStopped()
- {
- return $this->status['stopped'];
- }
- /**
- * 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
- *
- * @api
- */
- public function getStopSignal()
- {
- return $this->status['stopsig'];
- }
- public function addOutput($line)
- {
- $this->stdout .= $line;
- }
- public function addErrorOutput($line)
- {
- $this->stderr .= $line;
- }
- public function getCommandLine()
- {
- return $this->commandline;
- }
- public function setCommandLine($commandline)
- {
- $this->commandline = $commandline;
- }
- }
|