move plugin execution code in to helper class.
This commit is contained in:
parent
9207c39105
commit
912776801d
|
@ -92,9 +92,9 @@ class Builder implements LoggerAwareInterface
|
|||
public $quiet = false;
|
||||
|
||||
/**
|
||||
* @var \PHPCI\Plugin\Util\Factory
|
||||
* @var \PHPCI\Plugin\Util\Executor
|
||||
*/
|
||||
protected $pluginFactory;
|
||||
protected $pluginExecutor;
|
||||
|
||||
/**
|
||||
* Set up the builder.
|
||||
|
@ -108,7 +108,7 @@ class Builder implements LoggerAwareInterface
|
|||
}
|
||||
$this->build = $build;
|
||||
$this->store = Store\Factory::getStore('Build');
|
||||
$this->setupPluginFactory($build);
|
||||
$this->pluginExecutor = new Plugin\Util\Executor($this->buildPluginFactory($build), $this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -170,19 +170,19 @@ class Builder implements LoggerAwareInterface
|
|||
|
||||
// Run the core plugin stages:
|
||||
foreach (array('setup', 'test', 'complete') as $stage) {
|
||||
$this->executePlugins($stage);
|
||||
$this->pluginExecutor->executePlugins($this->config, $stage);
|
||||
}
|
||||
|
||||
// Failed build? Execute failure plugins and then mark the build as failed.
|
||||
if (!$this->success) {
|
||||
$this->executePlugins('failure');
|
||||
$this->pluginExecutor->executePlugins($this->config, 'failure');
|
||||
throw new \Exception('BUILD FAILED!');
|
||||
}
|
||||
|
||||
// If we got this far, the build was successful!
|
||||
if ($this->success) {
|
||||
$this->build->setStatus(2);
|
||||
$this->executePlugins('success');
|
||||
$this->pluginExecutor->executePlugins($this->config, 'success');
|
||||
$this->logSuccess('BUILD SUCCESSFUL!');
|
||||
}
|
||||
|
||||
|
@ -372,86 +372,6 @@ class Builder implements LoggerAwareInterface
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a the appropriate set of plugins for a given build stage.
|
||||
*/
|
||||
protected function executePlugins($stage)
|
||||
{
|
||||
// Ignore any stages for which we don't have plugins set:
|
||||
if (!array_key_exists(
|
||||
$stage,
|
||||
$this->config
|
||||
) || !is_array($this->config[$stage])
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($this->config[$stage] as $plugin => $options) {
|
||||
$this->log('RUNNING PLUGIN: ' . $plugin);
|
||||
|
||||
// Is this plugin allowed to fail?
|
||||
if ($stage == 'test' && !isset($options['allow_failures'])) {
|
||||
$options['allow_failures'] = false;
|
||||
}
|
||||
|
||||
// Try and execute it:
|
||||
if ($this->executePlugin($plugin, $options)) {
|
||||
|
||||
// Execution was successful:
|
||||
$this->logSuccess('PLUGIN STATUS: SUCCESS!');
|
||||
|
||||
} else {
|
||||
|
||||
// If we're in the "test" stage and the plugin is not allowed to fail,
|
||||
// then mark the build as failed:
|
||||
if ($stage == 'test' && !$options['allow_failures']) {
|
||||
$this->success = false;
|
||||
}
|
||||
|
||||
$this->logFailure('PLUGIN STATUS: FAILED');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a given plugin, with options and returns the result.
|
||||
*/
|
||||
protected function executePlugin($plugin, $options)
|
||||
{
|
||||
// Any plugin name without a namespace separator is a PHPCI built in plugin
|
||||
// if not we assume it's a fully name-spaced class name that implements the plugin interface.
|
||||
// If not the factory will throw an exception.
|
||||
if (strpos($plugin, '\\') === false) {
|
||||
$class = str_replace('_', ' ', $plugin);
|
||||
$class = ucwords($class);
|
||||
$class = 'PHPCI\\Plugin\\' . str_replace(' ', '', $class);
|
||||
}
|
||||
else {
|
||||
$class = $plugin;
|
||||
}
|
||||
|
||||
if (!class_exists($class)) {
|
||||
$this->logFailure('Plugin does not exist: ' . $plugin);
|
||||
return false;
|
||||
}
|
||||
|
||||
$rtn = true;
|
||||
|
||||
// Try running it:
|
||||
try {
|
||||
$obj = $this->pluginFactory->buildPlugin($class, $options);
|
||||
|
||||
if (!$obj->execute()) {
|
||||
$rtn = false;
|
||||
}
|
||||
} catch (\Exception $ex) {
|
||||
$this->logFailure('EXCEPTION: ' . $ex->getMessage(), $ex);
|
||||
$rtn = false;
|
||||
}
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a binary required by a plugin.
|
||||
* @param $binary
|
||||
|
@ -506,12 +426,12 @@ class Builder implements LoggerAwareInterface
|
|||
return $this->logger;
|
||||
}
|
||||
|
||||
private function setupPluginFactory(Build $build)
|
||||
private function buildPluginFactory(Build $build)
|
||||
{
|
||||
$this->pluginFactory = new Plugin\Util\Factory();
|
||||
$pluginFactory = new Plugin\Util\Factory();
|
||||
|
||||
$self = $this;
|
||||
$this->pluginFactory->registerResource(
|
||||
$pluginFactory->registerResource(
|
||||
function () use($self) {
|
||||
return $self;
|
||||
},
|
||||
|
@ -519,7 +439,7 @@ class Builder implements LoggerAwareInterface
|
|||
'PHPCI\Builder'
|
||||
);
|
||||
|
||||
$this->pluginFactory->registerResource(
|
||||
$pluginFactory->registerResource(
|
||||
function () use($build) {
|
||||
return $build;
|
||||
},
|
||||
|
@ -527,7 +447,7 @@ class Builder implements LoggerAwareInterface
|
|||
'PHPCI\Model\Build'
|
||||
);
|
||||
|
||||
$this->pluginFactory->registerResource(
|
||||
$pluginFactory->registerResource(
|
||||
function () use ($self) {
|
||||
$factory = new MailerFactory($self->getSystemConfig('phpci'));
|
||||
return $factory->getSwiftMailerFromConfig();
|
||||
|
@ -535,5 +455,7 @@ class Builder implements LoggerAwareInterface
|
|||
null,
|
||||
'Swift_Mailer'
|
||||
);
|
||||
|
||||
return $pluginFactory;
|
||||
}
|
||||
}
|
||||
|
|
104
PHPCI/Plugin/Util/Executor.php
Normal file
104
PHPCI/Plugin/Util/Executor.php
Normal file
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCI\Plugin\Util;
|
||||
|
||||
use PHPCI\Builder;
|
||||
|
||||
class Executor
|
||||
{
|
||||
|
||||
/**
|
||||
* @var Builder
|
||||
*/
|
||||
protected $builder;
|
||||
|
||||
/**
|
||||
* @var Factory
|
||||
*/
|
||||
protected $pluginFactory;
|
||||
|
||||
function __construct(Factory $pluginFactory, Builder $builder)
|
||||
{
|
||||
$this->pluginFactory = $pluginFactory;
|
||||
$this->builder = $builder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a the appropriate set of plugins for a given build stage.
|
||||
* @param array $config PHPCI configuration
|
||||
* @param string $stage
|
||||
*/
|
||||
public function executePlugins(&$config, $stage)
|
||||
{
|
||||
// Ignore any stages for which we don't have plugins set:
|
||||
if (!array_key_exists($stage, $config) || !is_array($config[$stage])) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($config[$stage] as $plugin => $options) {
|
||||
$this->builder->log('RUNNING PLUGIN: ' . $plugin);
|
||||
|
||||
// Is this plugin allowed to fail?
|
||||
if ($stage == 'test' && !isset($options['allow_failures'])) {
|
||||
$options['allow_failures'] = false;
|
||||
}
|
||||
|
||||
// Try and execute it:
|
||||
if ($this->executePlugin($plugin, $options)) {
|
||||
|
||||
// Execution was successful:
|
||||
$this->builder->logSuccess('PLUGIN STATUS: SUCCESS!');
|
||||
|
||||
} else {
|
||||
|
||||
// If we're in the "test" stage and the plugin is not allowed to fail,
|
||||
// then mark the build as failed:
|
||||
if ($stage == 'test' && !$options['allow_failures']) {
|
||||
$this->success = false;
|
||||
}
|
||||
|
||||
$this->builder->logFailure('PLUGIN STATUS: FAILED');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a given plugin, with options and returns the result.
|
||||
*/
|
||||
public function executePlugin($plugin, $options)
|
||||
{
|
||||
// Any plugin name without a namespace separator is a PHPCI built in plugin
|
||||
// if not we assume it's a fully name-spaced class name that implements the plugin interface.
|
||||
// If not the factory will throw an exception.
|
||||
if (strpos($plugin, '\\') === false) {
|
||||
$class = str_replace('_', ' ', $plugin);
|
||||
$class = ucwords($class);
|
||||
$class = 'PHPCI\\Plugin\\' . str_replace(' ', '', $class);
|
||||
}
|
||||
else {
|
||||
$class = $plugin;
|
||||
}
|
||||
|
||||
if (!class_exists($class)) {
|
||||
$this->builder->logFailure('Plugin does not exist: ' . $plugin);
|
||||
return false;
|
||||
}
|
||||
|
||||
$rtn = true;
|
||||
|
||||
// Try running it:
|
||||
try {
|
||||
$obj = $this->pluginFactory->buildPlugin($class, $options);
|
||||
|
||||
if (!$obj->execute()) {
|
||||
$rtn = false;
|
||||
}
|
||||
} catch (\Exception $ex) {
|
||||
$this->builder->logFailure('EXCEPTION: ' . $ex->getMessage(), $ex);
|
||||
$rtn = false;
|
||||
}
|
||||
|
||||
return $rtn;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue