diff --git a/PHPCI/Plugin/Util/Factory.php b/PHPCI/Plugin/Util/Factory.php index 0e526023..8adfd99d 100644 --- a/PHPCI/Plugin/Util/Factory.php +++ b/PHPCI/Plugin/Util/Factory.php @@ -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; } diff --git a/Tests/PHPCI/Plugin/Util/ExamplePluginConfig.php b/Tests/PHPCI/Plugin/Util/ExamplePluginConfig.php new file mode 100644 index 00000000..4e4279b9 --- /dev/null +++ b/Tests/PHPCI/Plugin/Util/ExamplePluginConfig.php @@ -0,0 +1,22 @@ +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 + ); +}; \ No newline at end of file diff --git a/Tests/PHPCI/Plugin/Util/FactoryTest.php b/Tests/PHPCI/Plugin/Util/FactoryTest.php index 3265e07c..72ed714b 100644 --- a/Tests/PHPCI/Plugin/Util/FactoryTest.php +++ b/Tests/PHPCI/Plugin/Util/FactoryTest.php @@ -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.