2013-12-12 15:15:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PHPCI\Helper;
|
|
|
|
|
|
|
|
|
|
|
|
use PHPCI\BuildLogger;
|
|
|
|
|
|
|
|
class CommandExecutor
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \PHPCI\BuildLogger
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $quiet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $verbose;
|
|
|
|
|
|
|
|
protected $lastOutput;
|
|
|
|
|
2013-12-12 17:17:11 +01:00
|
|
|
/**
|
|
|
|
* The path which findBinary will look in.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $rootDir;
|
|
|
|
|
2013-12-12 15:15:44 +01:00
|
|
|
/**
|
|
|
|
* @param BuildLogger $logger
|
2013-12-12 17:17:11 +01:00
|
|
|
* @param $rootDir
|
2013-12-12 15:15:44 +01:00
|
|
|
* @param bool $quiet
|
|
|
|
* @param bool $verbose
|
|
|
|
*/
|
2013-12-12 17:17:11 +01:00
|
|
|
public function __construct(BuildLogger $logger, $rootDir, &$quiet = false, &$verbose = false)
|
2013-12-12 15:15:44 +01:00
|
|
|
{
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->quiet = $quiet;
|
|
|
|
$this->verbose = $verbose;
|
|
|
|
|
|
|
|
$this->lastOutput = array();
|
2013-12-12 17:17:11 +01:00
|
|
|
|
|
|
|
$this->rootDir = $rootDir;
|
2013-12-12 15:15:44 +01:00
|
|
|
}
|
|
|
|
|
2013-12-13 15:41:02 +01:00
|
|
|
/**
|
|
|
|
* Executes shell commands. Accepts multiple arguments the first
|
|
|
|
* is the template and everything else is inserted in. c.f. sprintf
|
|
|
|
* @return bool Indicates success
|
|
|
|
*/
|
|
|
|
public function executeCommand()
|
|
|
|
{
|
|
|
|
return $this->buildAndExecuteCommand(func_get_args());
|
|
|
|
}
|
|
|
|
|
2013-12-12 15:15:44 +01:00
|
|
|
/**
|
|
|
|
* Executes shell commands.
|
|
|
|
* @param array $args
|
|
|
|
* @return bool Indicates success
|
|
|
|
*/
|
2013-12-13 15:41:02 +01:00
|
|
|
public function buildAndExecuteCommand($args = array())
|
2013-12-12 15:15:44 +01:00
|
|
|
{
|
2013-12-12 15:16:14 +01:00
|
|
|
$this->lastOutput = array();
|
|
|
|
|
2013-12-12 15:15:44 +01:00
|
|
|
$command = call_user_func_array('sprintf', $args);
|
|
|
|
|
|
|
|
if ($this->quiet) {
|
|
|
|
$this->logger->log('Executing: ' . $command);
|
|
|
|
}
|
|
|
|
|
|
|
|
$status = 0;
|
|
|
|
exec($command, $this->lastOutput, $status);
|
|
|
|
|
|
|
|
if (!empty($this->lastOutput) && ($this->verbose|| $status != 0)) {
|
|
|
|
$this->logger->log($this->lastOutput);
|
|
|
|
}
|
|
|
|
|
|
|
|
$rtn = false;
|
|
|
|
|
|
|
|
if ($status == 0) {
|
|
|
|
$rtn = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the output from the last command run.
|
|
|
|
*/
|
|
|
|
public function getLastOutput()
|
|
|
|
{
|
|
|
|
return implode(PHP_EOL, $this->lastOutput);
|
|
|
|
}
|
2013-12-12 17:17:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a binary required by a plugin.
|
|
|
|
* @param $binary
|
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function findBinary($binary)
|
|
|
|
{
|
|
|
|
if (is_string($binary)) {
|
|
|
|
$binary = array($binary);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($binary as $bin) {
|
|
|
|
// Check project root directory:
|
|
|
|
if (is_file($this->rootDir . $bin)) {
|
|
|
|
return $this->rootDir . $bin;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check Composer bin dir:
|
|
|
|
if (is_file($this->rootDir . 'vendor/bin/' . $bin)) {
|
|
|
|
return $this->rootDir . 'vendor/bin/' . $bin;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use "which"
|
|
|
|
$which = trim(shell_exec('which ' . $bin));
|
|
|
|
|
|
|
|
if (!empty($which)) {
|
|
|
|
return $which;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2013-12-12 15:15:44 +01:00
|
|
|
}
|