Refactored plugins

This commit is contained in:
Dmitry Khomutov 2016-07-11 22:00:04 +06:00
commit 16a5add859
45 changed files with 404 additions and 1224 deletions

View file

@ -8,6 +8,7 @@ use PHPCensor\Helper\Lang;
use PHPCensor\Logging\BuildLogger;
use PHPCensor\Model\Build;
use PHPCensor\Store\BuildStore;
use PHPCensor\Builder;
/**
* Plugin Executor - Runs the configured plugins for a given build stage.
@ -15,6 +16,16 @@ use PHPCensor\Store\BuildStore;
*/
class Executor
{
/**
* @var \PHPCI\Builder
*/
protected $phpci;
/**
* @var \PHPCI\Model\Build
*/
protected $build;
/**
* @var BuildLogger
*/
@ -31,14 +42,16 @@ class Executor
protected $store;
/**
* @param Factory $pluginFactory
* @param Builder $phpci
* @param Build $build
* @param BuildLogger $logger
*/
public function __construct(Factory $pluginFactory, BuildLogger $logger, BuildStore $store = null)
public function __construct(Builder $phpci, Build $build, BuildLogger $logger)
{
$this->pluginFactory = $pluginFactory;
$this->phpci = $phpci;
$this->build = $build;
$this->logger = $logger;
$this->store = $store ?: StoreFactory::getStore('Build');
$this->store = StoreFactory::getStore('Build');
}
/**
@ -78,7 +91,7 @@ class Executor
protected function getBranchSpecificPlugins(&$config, $stage, $pluginsToExecute)
{
/** @var \PHPCensor\Model\Build $build */
$build = $this->pluginFactory->getResourceFor('PHPCensor\Model\Build');
$build = $this->build;
$branch = $build->getBranch();
// If we don't have any branch-specific plugins:
@ -190,10 +203,7 @@ class Executor
}
try {
// Build and run it
$obj = $this->pluginFactory->buildPlugin($class, $options);
return $obj->execute();
return (new $class($this->phpci, $this->build, $options))->execute();
} catch (\Exception $ex) {
$this->logger->logFailure(Lang::get('exception') . $ex->getMessage(), $ex);
@ -235,7 +245,7 @@ class Executor
private function getBuildSummary()
{
/** @var Build $build */
$build = $this->pluginFactory->getResourceFor('PHPCensor\Model\Build');
$build = $this->build;
$metas = $this->store->getMeta('plugin-summary', $build->getProjectId(), $build->getId());
return isset($metas[0]['meta_value']) ? $metas[0]['meta_value'] : [];
}
@ -248,7 +258,7 @@ class Executor
private function setBuildSummary($summary)
{
/** @var Build $build */
$build = $this->pluginFactory->getResourceFor('PHPCensor\Model\Build');
$build = $this->build;
$this->store->setMeta($build->getProjectId(), $build->getId(), 'plugin-summary', json_encode($summary));
}
}

View file

@ -1,216 +0,0 @@
<?php
namespace PHPCensor\Plugin\Util;
use Pimple\Container;
/**
* Plugin Factory - Loads Plugins and passes required dependencies.
* @package PHPCensor\Plugin\Util
*/
class Factory
{
const TYPE_ARRAY = "array";
const TYPE_CALLABLE = "callable";
const INTERFACE_PLUGIN = '\PHPCensor\Plugin';
private $currentPluginOptions;
/**
* @var Container
*/
private $container;
/**
* @param Container $container
*/
public function __construct(Container $container = null)
{
if ($container) {
$this->container = $container;
} else {
$this->container = new Container();
}
}
/**
* Trys to get a function from the file path specified. If the
* file returns a function then $this will be passed to it.
* This enables the config file to call any public methods.
*
* @param $configPath
* @return bool - true if the function exists else false.
*/
public function addConfigFromFile($configPath)
{
// The file is expected to return a function which can
// act on the pluginFactory to register any resources needed.
if (file_exists($configPath)) {
$configFunction = require($configPath);
if (is_callable($configFunction)) {
$configFunction($this);
return true;
}
}
return false;
}
/**
* Get most recently used factory options.
* @return mixed
*/
public function getLastOptions()
{
return $this->currentPluginOptions;
}
/**
* Builds an instance of plugin of class $className. $options will
* be passed along with any resources registered with the factory.
*
* @param $className
* @param array|null $options
* @throws \InvalidArgumentException if $className doesn't represent a valid plugin
* @return \PHPCensor\Plugin
*/
public function buildPlugin($className, $options = [])
{
$this->currentPluginOptions = $options;
$reflectedPlugin = new \ReflectionClass($className);
if (!$reflectedPlugin->implementsInterface(self::INTERFACE_PLUGIN)) {
throw new \InvalidArgumentException(
"Requested class must implement " . self:: INTERFACE_PLUGIN
);
}
$constructor = $reflectedPlugin->getConstructor();
if ($constructor) {
$argsToUse = [];
foreach ($constructor->getParameters() as $param) {
if ('options' === $param->getName()) {
$argsToUse[] = $options;
} else {
$argsToUse = $this->addArgFromParam($argsToUse, $param);
}
}
$plugin = $reflectedPlugin->newInstanceArgs($argsToUse);
} else {
$plugin = $reflectedPlugin->newInstance();
}
return $plugin;
}
/**
* @param callable $loader
* @param string|null $name
* @param string|null $type
* @throws \InvalidArgumentException
* @internal param mixed $resource
*/
public function registerResource(
$loader,
$name = null,
$type = null
) {
if ($name === null && $type === null) {
throw new \InvalidArgumentException(
"Type or Name must be specified"
);
}
if (!($loader instanceof \Closure)) {
throw new \InvalidArgumentException(
'$loader is expected to be a function'
);
}
$resourceID = $this->getInternalID($type, $name);
$this->container[$resourceID] = $loader;
}
/**
* Get an internal resource ID.
* @param null $type
* @param null $name
* @return string
*/
private function getInternalID($type = null, $name = null)
{
$type = $type ? : "";
$name = $name ? : "";
return $type . "-" . $name;
}
/**
* @param string $type
* @param string $name
* @return mixed
*/
public function getResourceFor($type = null, $name = null)
{
$fullId = $this->getInternalID($type, $name);
if (isset($this->container[$fullId])) {
return $this->container[$fullId];
}
$typeOnlyID = $this->getInternalID($type, null);
if (isset($this->container[$typeOnlyID])) {
return $this->container[$typeOnlyID];
}
$nameOnlyID = $this->getInternalID(null, $name);
if (isset($this->container[$nameOnlyID])) {
return $this->container[$nameOnlyID];
}
return null;
}
/**
* @param \ReflectionParameter $param
* @return null|string
*/
private function getParamType(\ReflectionParameter $param)
{
$class = $param->getClass();
if ($class) {
return $class->getName();
} elseif ($param->isArray()) {
return self::TYPE_ARRAY;
} elseif (is_callable($param)) {
return self::TYPE_CALLABLE;
} else {
return null;
}
}
/**
* @param $existingArgs
* @param \ReflectionParameter $param
* @return array
* @throws \DomainException
*/
private function addArgFromParam($existingArgs, \ReflectionParameter $param)
{
$name = $param->getName();
$type = $this->getParamType($param);
$arg = $this->getResourceFor($type, $name);
if ($arg !== null) {
$existingArgs[] = $arg;
} elseif ($arg === null && $param->isOptional()) {
$existingArgs[] = $param->getDefaultValue();
} else {
throw new \DomainException(
"Unsatisfied dependency: " . $param->getName()
);
}
return $existingArgs;
}
}