2014-05-09 14:09:27 +02:00
|
|
|
<?php
|
2014-05-12 18:26:17 +02:00
|
|
|
/**
|
|
|
|
* PHPCI - Continuous Integration for PHP
|
|
|
|
*
|
|
|
|
* @copyright Copyright 2014, Block 8 Limited.
|
|
|
|
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
|
|
|
|
* @link https://www.phptesting.org/
|
|
|
|
*/
|
2014-05-09 14:09:27 +02:00
|
|
|
|
|
|
|
namespace PHPCI\Helper;
|
|
|
|
|
|
|
|
use \PHPCI\Logging\BuildLogger;
|
|
|
|
|
2014-05-11 17:38:33 +02:00
|
|
|
abstract class BaseCommandExecutor implements CommandExecutor
|
2014-05-09 14:09:27 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \PHPCI\Logging\BuildLogger
|
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $quiet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $verbose;
|
|
|
|
|
|
|
|
protected $lastOutput;
|
|
|
|
|
|
|
|
public $logExecOutput = true;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The path which findBinary will look in.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $rootDir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param BuildLogger $logger
|
|
|
|
* @param string $rootDir
|
|
|
|
* @param bool $quiet
|
|
|
|
* @param bool $verbose
|
|
|
|
*/
|
|
|
|
public function __construct(BuildLogger $logger, $rootDir, &$quiet = false, &$verbose = false)
|
|
|
|
{
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->quiet = $quiet;
|
|
|
|
$this->verbose = $verbose;
|
|
|
|
|
|
|
|
$this->lastOutput = array();
|
|
|
|
|
|
|
|
$this->rootDir = $rootDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Executes shell commands.
|
|
|
|
* @param array $args
|
|
|
|
* @return bool Indicates success
|
|
|
|
*/
|
|
|
|
public function executeCommand($args = array())
|
|
|
|
{
|
|
|
|
$this->lastOutput = array();
|
|
|
|
|
|
|
|
$command = call_user_func_array('sprintf', $args);
|
|
|
|
|
|
|
|
if ($this->quiet) {
|
|
|
|
$this->logger->log('Executing: ' . $command);
|
|
|
|
}
|
|
|
|
|
|
|
|
$status = 0;
|
|
|
|
exec($command, $this->lastOutput, $status);
|
|
|
|
|
|
|
|
foreach ($this->lastOutput as &$lastOutput) {
|
|
|
|
$lastOutput = trim($lastOutput, '"');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->logExecOutput && !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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a binary required by a plugin.
|
|
|
|
* @param string $binary
|
|
|
|
* @return null|string
|
|
|
|
*/
|
2014-05-11 17:38:33 +02:00
|
|
|
abstract public function findBinary($binary);
|
2014-05-12 10:07:27 +02:00
|
|
|
}
|