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;
|
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-02-27 10:35:26 +00:00
|
|
|
static protected $timeout = 300;
|
2012-02-21 07:59:52 +00:00
|
|
|
|
2012-01-05 10:12:54 +00:00
|
|
|
/**
|
|
|
|
* runs a process on the commandline
|
|
|
|
*
|
|
|
|
* @param $command the command to execute
|
|
|
|
* @param null $output the output will be written into this var if passed
|
|
|
|
* @return int statuscode
|
|
|
|
*/
|
2012-01-18 07:56:35 +00:00
|
|
|
public function execute($command, &$output = null)
|
2012-01-05 10:12:54 +00:00
|
|
|
{
|
2012-01-22 19:08:20 +00:00
|
|
|
$captureOutput = count(func_get_args()) > 1;
|
2012-02-21 07:59:52 +00:00
|
|
|
$process = new Process($command, null, null, null, static::getTimeout());
|
2012-01-22 19:08:20 +00:00
|
|
|
$process->run(function($type, $buffer) use ($captureOutput) {
|
|
|
|
if ($captureOutput) {
|
|
|
|
return;
|
2012-01-05 10:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
echo $buffer;
|
|
|
|
});
|
|
|
|
|
2012-01-22 19:08:20 +00:00
|
|
|
if ($captureOutput) {
|
|
|
|
$output = $process->getOutput();
|
2012-01-05 10:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $process->getExitCode();
|
|
|
|
}
|
2012-01-22 19:08:20 +00:00
|
|
|
|
|
|
|
public function splitLines($output)
|
|
|
|
{
|
|
|
|
return ((string) $output === '') ? array() : preg_split('{\r?\n}', $output);
|
|
|
|
}
|
2012-02-21 07:59:52 +00:00
|
|
|
|
|
|
|
static public function getTimeout()
|
|
|
|
{
|
|
|
|
return static::$timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
static public function setTimeout($timeout)
|
|
|
|
{
|
|
|
|
static::$timeout = $timeout;
|
|
|
|
}
|
|
|
|
}
|