move command execution code out of builder class

This commit is contained in:
steve.brazier 2013-12-12 14:15:44 +00:00
parent 6eb38d1039
commit 5801c6083e
2 changed files with 87 additions and 22 deletions

View file

@ -9,6 +9,7 @@
namespace PHPCI;
use PHPCI\Helper\CommandExecutor;
use PHPCI\Helper\MailerFactory;
use PHPCI\Model\Build;
use b8\Store;
@ -96,6 +97,11 @@ class Builder implements LoggerAwareInterface, BuildLogger
*/
protected $pluginExecutor;
/**
* @var Helper\CommandExecutor
*/
protected $commandExecutor;
/**
* Set up the builder.
* @param \PHPCI\Model\Build $build
@ -109,6 +115,8 @@ class Builder implements LoggerAwareInterface, BuildLogger
$this->build = $build;
$this->store = Store\Factory::getStore('Build');
$this->pluginExecutor = new Plugin\Util\Executor($this->buildPluginFactory($build), $this);
$this->commandExecutor = new CommandExecutor($this, $this->quiet, $this->verbose);
}
/**
@ -209,27 +217,7 @@ class Builder implements LoggerAwareInterface, BuildLogger
*/
public function executeCommand()
{
$command = call_user_func_array('sprintf', func_get_args());
if (!$this->quiet) {
$this->log('Executing: ' . $command);
}
$status = 0;
exec($command, $this->lastOutput, $status);
if (!empty($this->lastOutput) && ($this->verbose || $status != 0)) {
$this->log($this->lastOutput);
}
$rtn = false;
if ($status == 0) {
$rtn = true;
}
return $rtn;
return $this->commandExecutor->executeCommand(func_get_args());
}
/**
@ -237,7 +225,7 @@ class Builder implements LoggerAwareInterface, BuildLogger
*/
public function getLastOutput()
{
return implode(PHP_EOL, $this->lastOutput);
return $this->commandExecutor->getLastOutput();
}
/**

View file

@ -0,0 +1,77 @@
<?php
namespace PHPCI\Helper;
use PHPCI\BuildLogger;
class CommandExecutor
{
/**
* @var \PHPCI\BuildLogger
*/
protected $logger;
/**
* @var bool
*/
protected $quiet;
/**
* @var bool
*/
protected $verbose;
protected $lastOutput;
/**
* @param BuildLogger $logger
* @param bool $quiet
* @param bool $verbose
*/
public function __construct(BuildLogger $logger, &$quiet = false, &$verbose = false)
{
$this->logger = $logger;
$this->quiet = $quiet;
$this->verbose = $verbose;
$this->lastOutput = array();
}
/**
* Executes shell commands.
* @param array $args
* @return bool Indicates success
*/
public function executeCommand($args = array())
{
$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);
}
}