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;
|
|
|
|
|
2015-04-14 12:17:52 +02:00
|
|
|
use Exception;
|
|
|
|
use PHPCI\Logging\BuildLogger;
|
2014-07-04 11:02:27 +02:00
|
|
|
use Psr\Log\LogLevel;
|
2014-05-09 14:09:27 +02:00
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Handles running system commands with variables.
|
|
|
|
* @package PHPCI\Helper
|
|
|
|
*/
|
2014-05-11 17:38:33 +02:00
|
|
|
abstract class BaseCommandExecutor implements CommandExecutor
|
2014-05-09 14:09:27 +02:00
|
|
|
{
|
|
|
|
/**
|
2015-04-14 12:17:52 +02:00
|
|
|
* @var BuildLogger
|
2014-05-09 14:09:27 +02:00
|
|
|
*/
|
|
|
|
protected $logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $quiet;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $verbose;
|
|
|
|
|
|
|
|
protected $lastOutput;
|
2014-07-30 14:34:45 +02:00
|
|
|
protected $lastError;
|
2014-05-09 14:09:27 +02:00
|
|
|
|
|
|
|
public $logExecOutput = true;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The path which findBinary will look in.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $rootDir;
|
|
|
|
|
2014-07-04 11:02:27 +02:00
|
|
|
/**
|
|
|
|
* Current build path
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $buildPath;
|
|
|
|
|
2014-05-09 14:09:27 +02:00
|
|
|
/**
|
|
|
|
* @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;
|
2014-07-30 14:34:45 +02:00
|
|
|
$descriptorSpec = array(
|
|
|
|
0 => array("pipe", "r"), // stdin
|
|
|
|
1 => array("pipe", "w"), // stdout
|
|
|
|
2 => array("pipe", "w"), // stderr
|
|
|
|
);
|
2014-05-09 14:09:27 +02:00
|
|
|
|
2014-07-30 14:34:45 +02:00
|
|
|
$pipes = array();
|
|
|
|
|
2015-02-23 15:40:20 +01:00
|
|
|
$process = proc_open($command, $descriptorSpec, $pipes, $this->buildPath, null);
|
2014-07-30 14:34:45 +02:00
|
|
|
|
|
|
|
if (is_resource($process)) {
|
|
|
|
fclose($pipes[0]);
|
|
|
|
|
|
|
|
$this->lastOutput = stream_get_contents($pipes[1]);
|
|
|
|
$this->lastError = stream_get_contents($pipes[2]);
|
|
|
|
|
|
|
|
fclose($pipes[1]);
|
|
|
|
fclose($pipes[2]);
|
|
|
|
|
|
|
|
$status = proc_close($process);
|
2014-05-09 14:09:27 +02:00
|
|
|
}
|
|
|
|
|
2014-07-30 15:03:55 +02:00
|
|
|
$this->lastOutput = array_filter(explode(PHP_EOL, $this->lastOutput));
|
2014-07-30 14:34:45 +02:00
|
|
|
|
|
|
|
$shouldOutput = ($this->logExecOutput && ($this->verbose || $status != 0));
|
|
|
|
|
|
|
|
if ($shouldOutput && !empty($this->lastOutput)) {
|
2014-05-09 14:09:27 +02:00
|
|
|
$this->logger->log($this->lastOutput);
|
|
|
|
}
|
|
|
|
|
2014-07-30 14:34:45 +02:00
|
|
|
if (!empty($this->lastError)) {
|
2014-07-30 14:57:29 +02:00
|
|
|
$this->logger->log("\033[0;31m" . $this->lastError . "\033[0m", LogLevel::ERROR);
|
2014-07-30 14:34:45 +02:00
|
|
|
}
|
|
|
|
|
2014-05-09 14:09:27 +02:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2014-07-30 15:58:07 +02:00
|
|
|
/**
|
|
|
|
* Returns the stderr output from the last command run.
|
|
|
|
*/
|
|
|
|
public function getLastError()
|
|
|
|
{
|
|
|
|
return $this->lastError;
|
|
|
|
}
|
|
|
|
|
2014-05-09 14:09:27 +02:00
|
|
|
/**
|
|
|
|
* Find a binary required by a plugin.
|
|
|
|
* @param string $binary
|
2015-04-14 12:17:52 +02:00
|
|
|
* @param bool $quiet
|
2014-05-09 14:09:27 +02:00
|
|
|
* @return null|string
|
|
|
|
*/
|
2015-04-14 12:17:52 +02:00
|
|
|
public function findBinary($binary, $quiet = false)
|
2014-07-11 15:07:27 +02:00
|
|
|
{
|
2015-04-14 12:17:52 +02:00
|
|
|
$composerBin = $this->getComposerBinDir(realpath($this->buildPath));
|
2014-07-04 11:02:27 +02:00
|
|
|
|
|
|
|
if (is_string($binary)) {
|
|
|
|
$binary = array($binary);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($binary as $bin) {
|
2014-12-04 16:48:52 +01:00
|
|
|
$this->logger->log(Lang::get('looking_for_binary', $bin), LogLevel::DEBUG);
|
2014-07-04 11:02:27 +02:00
|
|
|
|
|
|
|
if (is_dir($composerBin) && is_file($composerBin.'/'.$bin)) {
|
2014-12-04 16:48:52 +01:00
|
|
|
$this->logger->log(Lang::get('found_in_path', $composerBin, $bin), LogLevel::DEBUG);
|
2015-04-14 12:17:52 +02:00
|
|
|
return $composerBin . '/' . $bin;
|
2014-07-04 11:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_file($this->rootDir . $bin)) {
|
2014-12-04 16:48:52 +01:00
|
|
|
$this->logger->log(Lang::get('found_in_path', 'root', $bin), LogLevel::DEBUG);
|
2015-04-14 12:17:52 +02:00
|
|
|
return $this->rootDir . $bin;
|
2014-07-04 11:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_file($this->rootDir . 'vendor/bin/' . $bin)) {
|
2014-12-04 16:48:52 +01:00
|
|
|
$this->logger->log(Lang::get('found_in_path', 'vendor/bin', $bin), LogLevel::DEBUG);
|
2015-04-14 12:17:52 +02:00
|
|
|
return $this->rootDir . 'vendor/bin/' . $bin;
|
2014-07-04 11:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$findCmdResult = $this->findGlobalBinary($bin);
|
|
|
|
if (is_file($findCmdResult)) {
|
2014-12-04 16:48:52 +01:00
|
|
|
$this->logger->log(Lang::get('found_in_path', '', $bin), LogLevel::DEBUG);
|
2015-04-14 12:17:52 +02:00
|
|
|
return $findCmdResult;
|
2014-07-04 11:02:27 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-14 12:17:52 +02:00
|
|
|
|
|
|
|
if ($quiet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
throw new Exception(Lang::get('could_not_find', implode('/', $binary)));
|
2014-07-04 11:02:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find a binary which is installed globally on the system
|
|
|
|
* @param string $binary
|
|
|
|
* @return null|string
|
|
|
|
*/
|
2014-07-11 15:07:27 +02:00
|
|
|
abstract protected function findGlobalBinary($binary);
|
2014-07-04 11:02:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to load the composer.json file in the building project
|
|
|
|
* If the bin-dir is configured, return the full path to it
|
|
|
|
* @param string $path Current build path
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2014-07-11 15:07:27 +02:00
|
|
|
public function getComposerBinDir($path)
|
|
|
|
{
|
2014-07-04 11:02:27 +02:00
|
|
|
if (is_dir($path)) {
|
|
|
|
$composer = $path.'/composer.json';
|
2014-07-11 15:07:27 +02:00
|
|
|
if (is_file($composer)) {
|
2014-07-04 11:02:27 +02:00
|
|
|
$json = json_decode(file_get_contents($composer));
|
2014-07-29 17:23:48 +02:00
|
|
|
|
2014-07-11 15:07:27 +02:00
|
|
|
if (isset($json->config->{"bin-dir"})) {
|
2014-07-04 11:02:27 +02:00
|
|
|
return $path.'/'.$json->config->{"bin-dir"};
|
2014-07-29 17:23:48 +02:00
|
|
|
} elseif (is_dir($path . '/vendor/bin')) {
|
|
|
|
return $path . '/vendor/bin';
|
2014-07-04 11:02:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2015-02-23 15:40:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the buildPath property.
|
|
|
|
* @param string $path
|
|
|
|
*/
|
|
|
|
public function setBuildPath($path)
|
|
|
|
{
|
|
|
|
$this->buildPath = $path;
|
|
|
|
}
|
2014-05-12 10:07:27 +02:00
|
|
|
}
|