2012-01-05 10:12:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Util;
|
|
|
|
|
2012-01-18 07:56:35 +00:00
|
|
|
use Symfony\Component\Process\Process;
|
2013-04-28 09:12:42 +00:00
|
|
|
use Composer\IO\IOInterface;
|
2012-01-05 10:12:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Robert Schönthal <seroscho@googlemail.com>
|
|
|
|
*/
|
2012-01-18 07:56:35 +00:00
|
|
|
class ProcessExecutor
|
2012-01-05 10:12:54 +00:00
|
|
|
{
|
2012-05-22 15:13:15 +00:00
|
|
|
protected static $timeout = 300;
|
2012-02-21 07:59:52 +00:00
|
|
|
|
2012-04-04 07:36:04 +00:00
|
|
|
protected $captureOutput;
|
2012-02-29 11:05:25 +00:00
|
|
|
protected $errorOutput;
|
2013-04-28 09:12:42 +00:00
|
|
|
protected $io;
|
|
|
|
|
|
|
|
public function __construct(IOInterface $io = null)
|
|
|
|
{
|
|
|
|
$this->io = $io;
|
|
|
|
}
|
2012-02-29 11:05:25 +00:00
|
|
|
|
2012-01-05 10:12:54 +00:00
|
|
|
/**
|
|
|
|
* runs a process on the commandline
|
|
|
|
*
|
2012-03-21 23:38:05 +00:00
|
|
|
* @param string $command the command to execute
|
2012-04-04 07:36:04 +00:00
|
|
|
* @param mixed $output the output will be written into this var if passed by ref
|
|
|
|
* if a callable is passed it will be used as output handler
|
2012-05-22 10:07:08 +00:00
|
|
|
* @param string $cwd the working directory
|
|
|
|
* @return int statuscode
|
2012-01-05 10:12:54 +00:00
|
|
|
*/
|
2012-03-21 23:38:05 +00:00
|
|
|
public function execute($command, &$output = null, $cwd = null)
|
2012-01-05 10:12:54 +00:00
|
|
|
{
|
2013-04-28 09:12:42 +00:00
|
|
|
if ($this->io && $this->io->isDebug()) {
|
2013-04-28 10:01:58 +00:00
|
|
|
$safeCommand = preg_replace('{(://[^:/\s]+:)[^@\s/]+}i', '$1****', $command);
|
2013-04-28 09:12:42 +00:00
|
|
|
$this->io->write('Executing command ('.($cwd ?: 'CWD').'): '.$safeCommand);
|
|
|
|
}
|
|
|
|
|
2013-06-08 15:49:51 +00:00
|
|
|
// make sure that null translate to the proper directory in case the dir is a symlink
|
|
|
|
// and we call a git command, because msysgit does not handle symlinks properly
|
|
|
|
if (null === $cwd && defined('PHP_WINDOWS_VERSION_BUILD') && false !== strpos($command, 'git') && getcwd()) {
|
|
|
|
$cwd = realpath(getcwd());
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->captureOutput = count(func_get_args()) > 1;
|
|
|
|
$this->errorOutput = null;
|
|
|
|
$process = new Process($command, $cwd, null, null, static::getTimeout());
|
|
|
|
|
2012-04-04 07:36:04 +00:00
|
|
|
$callback = is_callable($output) ? $output : array($this, 'outputHandler');
|
|
|
|
$process->run($callback);
|
2012-01-05 10:12:54 +00:00
|
|
|
|
2012-04-04 07:36:04 +00:00
|
|
|
if ($this->captureOutput && !is_callable($output)) {
|
2012-01-22 19:08:20 +00:00
|
|
|
$output = $process->getOutput();
|
2012-01-05 10:12:54 +00:00
|
|
|
}
|
|
|
|
|
2012-02-29 11:05:25 +00:00
|
|
|
$this->errorOutput = $process->getErrorOutput();
|
|
|
|
|
2012-01-05 10:12:54 +00:00
|
|
|
return $process->getExitCode();
|
|
|
|
}
|
2012-01-22 19:08:20 +00:00
|
|
|
|
|
|
|
public function splitLines($output)
|
|
|
|
{
|
2013-06-18 15:23:41 +00:00
|
|
|
$output = trim($output);
|
|
|
|
|
2012-01-22 19:08:20 +00:00
|
|
|
return ((string) $output === '') ? array() : preg_split('{\r?\n}', $output);
|
|
|
|
}
|
2012-02-21 07:59:52 +00:00
|
|
|
|
2012-02-29 11:05:25 +00:00
|
|
|
/**
|
|
|
|
* Get any error output from the last command
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getErrorOutput()
|
|
|
|
{
|
|
|
|
return $this->errorOutput;
|
|
|
|
}
|
|
|
|
|
2012-04-04 07:36:04 +00:00
|
|
|
public function outputHandler($type, $buffer)
|
|
|
|
{
|
|
|
|
if ($this->captureOutput) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo $buffer;
|
|
|
|
}
|
|
|
|
|
2012-05-22 15:13:15 +00:00
|
|
|
public static function getTimeout()
|
2012-02-21 07:59:52 +00:00
|
|
|
{
|
|
|
|
return static::$timeout;
|
|
|
|
}
|
|
|
|
|
2012-05-22 15:13:15 +00:00
|
|
|
public static function setTimeout($timeout)
|
2012-02-21 07:59:52 +00:00
|
|
|
{
|
|
|
|
static::$timeout = $timeout;
|
|
|
|
}
|
|
|
|
}
|