2013-12-12 15:15:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PHPCI\Helper;
|
|
|
|
|
2013-12-13 16:57:56 +01:00
|
|
|
use \PHPCI\Logging\BuildLogger;
|
2014-03-10 13:08:16 +01:00
|
|
|
use Psr\Log\LogLevel;
|
2013-12-12 15:15:44 +01:00
|
|
|
|
|
|
|
class CommandExecutor
|
|
|
|
{
|
|
|
|
/**
|
2013-12-13 16:57:56 +01:00
|
|
|
* @var \PHPCI\Logging\BuildLogger
|
2013-12-12 15:15:44 +01:00
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $quiet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $verbose;
|
|
|
|
|
|
|
|
protected $lastOutput;
|
|
|
|
|
2014-04-25 13:28:27 +02:00
|
|
|
public $logExecOutput = true;
|
|
|
|
|
|
|
|
|
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
|
2014-05-09 10:21:53 +02:00
|
|
|
* @param string $rootDir
|
|
|
|
* @param bool $quiet
|
|
|
|
* @param bool $verbose
|
2013-12-12 15:15:44 +01:00
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes shell commands.
|
|
|
|
* @param array $args
|
|
|
|
* @return bool Indicates success
|
|
|
|
*/
|
2014-05-09 09:25:24 +02:00
|
|
|
public function executeCommand($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);
|
|
|
|
|
2014-03-08 19:46:03 +01:00
|
|
|
foreach ($this->lastOutput as &$lastOutput) {
|
|
|
|
$lastOutput = trim($lastOutput, '"');
|
|
|
|
}
|
|
|
|
|
2014-04-25 13:28:27 +02:00
|
|
|
if ($this->logExecOutput && !empty($this->lastOutput) && ($this->verbose|| $status != 0)) {
|
2013-12-12 15:15:44 +01:00
|
|
|
$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.
|
2014-05-09 10:21:53 +02:00
|
|
|
* @param string $binary
|
2013-12-12 17:17:11 +01:00
|
|
|
* @return null|string
|
|
|
|
*/
|
|
|
|
public function findBinary($binary)
|
|
|
|
{
|
|
|
|
if (is_string($binary)) {
|
|
|
|
$binary = array($binary);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($binary as $bin) {
|
2014-03-10 13:08:16 +01:00
|
|
|
$this->logger->log("Looking for binary: " . $bin, LogLevel::DEBUG);
|
2013-12-12 17:17:11 +01:00
|
|
|
// Check project root directory:
|
|
|
|
if (is_file($this->rootDir . $bin)) {
|
2014-03-10 13:08:16 +01:00
|
|
|
$this->logger->log("Found in root: " . $bin, LogLevel::DEBUG);
|
2013-12-12 17:17:11 +01:00
|
|
|
return $this->rootDir . $bin;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check Composer bin dir:
|
|
|
|
if (is_file($this->rootDir . 'vendor/bin/' . $bin)) {
|
2014-03-10 13:08:16 +01:00
|
|
|
$this->logger->log("Found in vendor/bin: " . $bin, LogLevel::DEBUG);
|
2013-12-12 17:17:11 +01:00
|
|
|
return $this->rootDir . 'vendor/bin/' . $bin;
|
|
|
|
}
|
|
|
|
|
2014-03-08 18:30:00 +01:00
|
|
|
// Use "where" for windows and "which" for other OS
|
2014-03-15 06:57:06 +01:00
|
|
|
$findCmd = IS_WIN ? 'where' : 'which';
|
2014-03-08 18:30:00 +01:00
|
|
|
$findCmdResult = trim(shell_exec($findCmd . ' ' . $bin));
|
2013-12-12 17:17:11 +01:00
|
|
|
|
2014-03-08 18:30:00 +01:00
|
|
|
if (!empty($findCmdResult)) {
|
2014-03-10 13:08:16 +01:00
|
|
|
$this->logger->log("Found in " . $findCmdResult, LogLevel::DEBUG);
|
2014-03-08 18:30:00 +01:00
|
|
|
return $findCmdResult;
|
2013-12-12 17:17:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2013-12-12 15:15:44 +01:00
|
|
|
}
|