add LoadedPluginInformation class to process composer's installed.json file and pull out information about phpci plugins.
This commit is contained in:
parent
f09ed3edad
commit
71588cd8e1
147
PHPCI/Plugin/Util/LoadedPluginInformation.php
Normal file
147
PHPCI/Plugin/Util/LoadedPluginInformation.php
Normal file
|
@ -0,0 +1,147 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCI\Plugin\Util;
|
||||
|
||||
|
||||
class LoadedPluginInformation
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $composerPackages;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $pluginInfo = null;
|
||||
|
||||
/**
|
||||
* @param string $filePath The path of installed.json created by composer.
|
||||
* @return LoadedPluginInformation
|
||||
*/
|
||||
public static function buildFromYaml($filePath)
|
||||
{
|
||||
if (file_exists($filePath)) {
|
||||
$installed = json_decode(file_get_contents($filePath));
|
||||
}
|
||||
else {
|
||||
$installed = array();
|
||||
}
|
||||
return new self($installed);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass[] $composerPackages This should be the contents of the
|
||||
* installed.json file created by composer
|
||||
*/
|
||||
public function __construct(array $composerPackages)
|
||||
{
|
||||
$this->composerPackages = $composerPackages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of objects. Each one represents an available plugin
|
||||
* and will have the following properties:
|
||||
* name - The friendly name of the plugin (may be an empty string)
|
||||
* class - The class of the plugin (will include namespace)
|
||||
* @return \stdClass[]
|
||||
*/
|
||||
public function getInstalledPlugins()
|
||||
{
|
||||
$this->loadPluginInfo();
|
||||
return $this->pluginInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of all the class names of plugins that have been
|
||||
* loaded.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getPluginClasses()
|
||||
{
|
||||
return array_map(
|
||||
function($plugin) {
|
||||
return $plugin->class;
|
||||
},
|
||||
$this->getInstalledPlugins()
|
||||
);
|
||||
}
|
||||
|
||||
protected function loadPluginInfo()
|
||||
{
|
||||
if ($this->pluginInfo !== null) {
|
||||
return;
|
||||
}
|
||||
$this->pluginInfo = array();
|
||||
foreach($this->composerPackages as $package) {
|
||||
$this->addPluginsFromPackage($package);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass $package
|
||||
*/
|
||||
protected function addPluginsFromPackage($package)
|
||||
{
|
||||
if (isset($package->extra->phpci)) {
|
||||
$phpciData = $package->extra->phpci;
|
||||
|
||||
if (isset($phpciData->pluginNamespace)) {
|
||||
$rootNamespace = $phpciData->pluginNamespace;
|
||||
}
|
||||
else {
|
||||
$rootNamespace = "";
|
||||
}
|
||||
|
||||
if (is_array($phpciData->suppliedPlugins)) {
|
||||
$this->addPlugins(
|
||||
$phpciData->suppliedPlugins,
|
||||
$package->name,
|
||||
$rootNamespace
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass[] $plugins
|
||||
* @param string $sourcePackageName
|
||||
* @param string $rootNamespace
|
||||
*/
|
||||
protected function addPlugins(
|
||||
array $plugins,
|
||||
$sourcePackageName,
|
||||
$rootNamespace = "")
|
||||
{
|
||||
foreach($plugins as $plugin) {
|
||||
if (!isset($plugin->class)) {
|
||||
continue;
|
||||
}
|
||||
$this->addPlugin($plugin, $sourcePackageName, $rootNamespace);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \stdClass $plugin
|
||||
* @param string $sourcePackageName
|
||||
* @param string $rootNamespace
|
||||
*/
|
||||
protected function addPlugin(
|
||||
$plugin,
|
||||
$sourcePackageName,
|
||||
$rootNamespace = "")
|
||||
{
|
||||
$newPlugin = clone $plugin;
|
||||
|
||||
$newPlugin->class = $rootNamespace . $newPlugin->class;
|
||||
|
||||
if (!isset($newPlugin->name)) {
|
||||
$newPlugin->name = "";
|
||||
}
|
||||
|
||||
$newPlugin->source = $sourcePackageName;
|
||||
|
||||
$this->pluginInfo[] = $newPlugin;
|
||||
}
|
||||
}
|
51
Tests/PHPCI/Plugin/Util/LoadedPluginInformationTest.php
Normal file
51
Tests/PHPCI/Plugin/Util/LoadedPluginInformationTest.php
Normal file
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace PHPCI\Plugin\Tests\Util;
|
||||
|
||||
use PHPCI\Plugin\Util\LoadedPluginInformation;
|
||||
|
||||
class LoadedPluginInformationTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var LoadedPluginInformation
|
||||
*/
|
||||
protected $testedInformation;
|
||||
|
||||
protected function setUpFromFile($file)
|
||||
{
|
||||
$this->testedInformation = LoadedPluginInformation::buildFromYaml($file);
|
||||
}
|
||||
|
||||
protected function phpciSetup()
|
||||
{
|
||||
$this->setUpFromFile(
|
||||
__DIR__ . "/../../../../vendor/composer/installed.json"
|
||||
);
|
||||
}
|
||||
|
||||
public function testBuildFromYaml_ReturnsInstance()
|
||||
{
|
||||
$this->phpciSetup();
|
||||
$this->assertInstanceOf(
|
||||
'\PHPCI\Plugin\Util\LoadedPluginInformation',
|
||||
$this->testedInformation
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetInstalledPlugins_ReturnsStdClassArray()
|
||||
{
|
||||
$this->phpciSetup();
|
||||
$plugins = $this->testedInformation->getInstalledPlugins();
|
||||
$this->assertInternalType("array", $plugins);
|
||||
$this->assertContainsOnly("stdClass", $plugins);
|
||||
}
|
||||
|
||||
public function testGetPluginClasses_ReturnsStringArray()
|
||||
{
|
||||
$this->phpciSetup();
|
||||
$classes = $this->testedInformation->getPluginClasses();
|
||||
$this->assertInternalType("array", $classes);
|
||||
$this->assertContainsOnly("string", $classes);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in a new issue