add FilesPluginInformation class to retrieve plugins contained with a directory.

This commit is contained in:
meadsteve 2014-02-25 20:52:19 +00:00
parent dd5ff7a835
commit 28fc30682b
2 changed files with 130 additions and 0 deletions

View file

@ -0,0 +1,104 @@
<?php
namespace PHPCI\Plugin\Util;
class FilesPluginInformation implements InstalledPluginInformation
{
/**
* A collection of all the file path information for
* the installed plugins.
*
* @var \SplFileInfo[]
*/
protected $files;
/**
* Each item in the array contains the information for
* a single plugin.
*
* @var array
*/
protected $pluginInfo = null;
public static function newFromDir($dirPath)
{
return new self(new \DirectoryIterator($dirPath));
}
function __construct(\Iterator $files)
{
$this->files = $files;
}
/**
* 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()
{
if ($this->pluginInfo === null) {
$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()
{
$this->pluginInfo = array();
foreach($this->files as $fileInfo) {
if ($fileInfo instanceof \SplFileInfo) {
if ($fileInfo->isFile()) {
$this->addPluginFromFile($fileInfo);
}
}
}
}
protected function addPluginFromFile(\SplFileInfo $fileInfo)
{
$newPlugin = new \stdClass();
$newPlugin->class = $this->getFullClassFromFile($fileInfo);
$newPlugin->source = "core";
$newPlugin->name = end(explode('\\', $newPlugin->class));
$this->pluginInfo[] = $newPlugin;
}
protected function getFullClassFromFile(\SplFileInfo $fileInfo)
{
//TODO: Something less horrible than a regular expression
// on the conents of a file
$contents = file_get_contents($fileInfo->getRealPath());
$matches = array();
preg_match('#class +([A-Za-z]+) +implements#i', $contents, $matches);
$className = $matches[1];
$matches = array();
preg_match('#namespace +([A-Za-z\\\\]+);#i', $contents, $matches);
$namespace = $matches[1];
return $namespace . '\\' . $className;
}
}

View file

@ -0,0 +1,26 @@
<?php
namespace PHPCI\Plugin\Tests\Util;
use PHPCI\Plugin\Util\FilesPluginInformation;
class FilesPluginInformationTest extends \PHPUnit_Framework_TestCase
{
public function testGetInstalledPlugins_returnsObjectes()
{
$pluginDirPath = realpath(__DIR__ . "/../../../../PHPCI/Plugin/");
$test = FilesPluginInformation::newFromDir($pluginDirPath);
$pluginInfos = $test->getInstalledPlugins();
$this->assertContainsOnlyInstancesOf('stdClass', $pluginInfos);
}
public function testGetPluginClasses_returnsStrings()
{
$pluginDirPath = realpath(__DIR__ . "/../../../../PHPCI/Plugin/");
$test = FilesPluginInformation::newFromDir($pluginDirPath);
$pluginInfos = $test->getPluginClasses();
$this->assertContainsOnly('string', $pluginInfos);
}
}