From 5801c6083ea4fbb4e24fe61378fc993a6a7d3935 Mon Sep 17 00:00:00 2001 From: "steve.brazier" Date: Thu, 12 Dec 2013 14:15:44 +0000 Subject: [PATCH] move command execution code out of builder class --- PHPCI/Builder.php | 32 +++++-------- PHPCI/Helper/CommandExecutor.php | 77 ++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 22 deletions(-) create mode 100644 PHPCI/Helper/CommandExecutor.php diff --git a/PHPCI/Builder.php b/PHPCI/Builder.php index 08fd3bed..c53fd670 100644 --- a/PHPCI/Builder.php +++ b/PHPCI/Builder.php @@ -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(); } /** diff --git a/PHPCI/Helper/CommandExecutor.php b/PHPCI/Helper/CommandExecutor.php new file mode 100644 index 00000000..0515f83c --- /dev/null +++ b/PHPCI/Helper/CommandExecutor.php @@ -0,0 +1,77 @@ +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); + } +}