2013-12-06 12:26:56 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PHPCI\Plugin\Util;
|
|
|
|
|
2013-12-13 17:10:54 +01:00
|
|
|
use \PHPCI\Logging\BuildLogger;
|
2013-12-06 12:26:56 +01:00
|
|
|
|
|
|
|
class Executor
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2013-12-06 12:31:39 +01:00
|
|
|
* @var BuildLogger
|
2013-12-06 12:26:56 +01:00
|
|
|
*/
|
2013-12-06 12:31:39 +01:00
|
|
|
protected $logger;
|
2013-12-06 12:26:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Factory
|
|
|
|
*/
|
|
|
|
protected $pluginFactory;
|
|
|
|
|
2013-12-13 17:10:54 +01:00
|
|
|
function __construct(Factory $pluginFactory,BuildLogger $logger)
|
2013-12-06 12:26:56 +01:00
|
|
|
{
|
|
|
|
$this->pluginFactory = $pluginFactory;
|
2013-12-06 12:31:39 +01:00
|
|
|
$this->logger = $logger;
|
2013-12-06 12:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute a the appropriate set of plugins for a given build stage.
|
|
|
|
* @param array $config PHPCI configuration
|
|
|
|
* @param string $stage
|
2013-12-07 14:53:05 +01:00
|
|
|
* @return bool
|
2013-12-06 12:26:56 +01:00
|
|
|
*/
|
|
|
|
public function executePlugins(&$config, $stage)
|
|
|
|
{
|
2013-12-07 14:53:05 +01:00
|
|
|
$success = true;
|
2013-12-06 12:26:56 +01:00
|
|
|
// Ignore any stages for which we don't have plugins set:
|
|
|
|
if (!array_key_exists($stage, $config) || !is_array($config[$stage])) {
|
2013-12-07 14:53:05 +01:00
|
|
|
return $success;
|
2013-12-06 12:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($config[$stage] as $plugin => $options) {
|
2013-12-06 12:31:39 +01:00
|
|
|
$this->logger->log('RUNNING PLUGIN: ' . $plugin);
|
2013-12-06 12:26:56 +01:00
|
|
|
|
|
|
|
// 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:
|
2013-12-06 12:31:39 +01:00
|
|
|
$this->logger->logSuccess('PLUGIN STATUS: SUCCESS!');
|
2013-12-06 12:26:56 +01:00
|
|
|
|
|
|
|
} 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']) {
|
2013-12-07 14:53:05 +01:00
|
|
|
$success = false;
|
2013-12-06 12:26:56 +01:00
|
|
|
}
|
|
|
|
|
2013-12-06 12:31:39 +01:00
|
|
|
$this->logger->logFailure('PLUGIN STATUS: FAILED');
|
2013-12-06 12:26:56 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-07 14:53:05 +01:00
|
|
|
|
|
|
|
return $success;
|
2013-12-06 12:26:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)) {
|
2013-12-06 12:31:39 +01:00
|
|
|
$this->logger->logFailure('Plugin does not exist: ' . $plugin);
|
2013-12-06 12:26:56 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rtn = true;
|
|
|
|
|
|
|
|
// Try running it:
|
|
|
|
try {
|
|
|
|
$obj = $this->pluginFactory->buildPlugin($class, $options);
|
|
|
|
|
|
|
|
if (!$obj->execute()) {
|
|
|
|
$rtn = false;
|
|
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
2013-12-06 12:31:39 +01:00
|
|
|
$this->logger->logFailure('EXCEPTION: ' . $ex->getMessage(), $ex);
|
2013-12-06 12:26:56 +01:00
|
|
|
$rtn = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rtn;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|