Add code to allow configuration for the plugin factory to be stored on a per install basis.

This commit is contained in:
meadsteve 2014-03-02 19:59:52 +00:00
parent f09ed3edad
commit 6d4e49d07b
3 changed files with 71 additions and 0 deletions

View file

@ -37,6 +37,28 @@ class Factory {
);
}
/**
* 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;
}
public function getLastOptions() {
return $this->currentPluginOptions;
}

View file

@ -0,0 +1,22 @@
<?php
return function (PHPCI\Plugin\Util\Factory $factory) {
$factory->registerResource(
// This function will be called when the resource is needed.
function() {
return array(
'Foo' => "Stuff",
'Bar' => "More Stuff"
);
},
// In addition to the function for building the resource the system
// also needs to be told when to load the resource. Either or both
// of the following arguments can be used (null to ignore)
// This resource will only be given when the argument name is:
"ResourceArray",
// The resource will only be given when the type hint is:
PHPCI\Plugin\Util\Factory::TYPE_ARRAY
);
};

View file

@ -148,6 +148,33 @@ class FactoryTest extends \PHPUnit_Framework_TestCase {
$this->assertArrayHasKey('thing', $plugin->Options);
}
public function testAddConfigFromFile_ReturnsTrueForValidFile()
{
$result = $this->testedFactory->addConfigFromFile(
realpath(__DIR__ . "/ExamplePluginConfig.php")
);
$this->assertTrue($result);
}
public function testAddConfigFromFile_RegistersResources()
{
$this->testedFactory->addConfigFromFile(
realpath(__DIR__ . "/ExamplePluginConfig.php")
);
$namespace = '\\PHPCI\\Plugin\\Tests\\Util\\';
$pluginName = $namespace . 'ExamplePluginWithSingleRequiredArg';
$plugin = $this->testedFactory->buildPlugin($pluginName);
// The Example config file defines an array as the resource.
$this->assertEquals(
array("bar" => "Hello"),
$plugin->RequiredArgument
);
}
/**
* Registers mocked Builder and Build classes so that realistic plugins
* can be tested.