diff --git a/src/Psy/Configuration.php b/src/Psy/Configuration.php index 3cb934d0b..d864193f3 100644 --- a/src/Psy/Configuration.php +++ b/src/Psy/Configuration.php @@ -16,6 +16,7 @@ use Psy\ExecutionLoop\Loop; use Psy\Output\OutputPager; use Psy\Output\ShellOutput; +use Psy\Plugin\PluginManager; use Psy\Presenter\PresenterManager; use Psy\Readline\GNUReadline; use Psy\Readline\Libedit; @@ -33,9 +34,10 @@ class Configuration 'defaultIncludes', 'useReadline', 'usePcntl', 'codeCleaner', 'pager', 'loop', 'configDir', 'dataDir', 'runtimeDir', 'manualDbFile', 'requireSemicolons', 'historySize', 'eraseDuplicates', 'tabCompletion', - 'tabCompletionMatchers', + 'tabCompletionMatchers', 'registerPlugins', ); + private $registerPlugins = true; private $defaultIncludes; private $configDir; private $dataDir; @@ -96,6 +98,27 @@ public function __construct(array $config = array()) // go go gadget, config! $this->loadConfig($config); $this->init(); + + // ask the plugin manager for configurations + if ($this->getRegisterPlugins()) { + $this->loadConfig(PluginManager::getConfiguration($config)); + } + } + + /** + * @param $bool + */ + public function setRegisterPlugins($bool) + { + $this->registerPlugins = (bool) $bool; + } + + /** + * @return bool + */ + public function getRegisterPlugins() + { + return $this->registerPlugins; } /** diff --git a/src/Psy/Plugin/AbstractPlugin.php b/src/Psy/Plugin/AbstractPlugin.php new file mode 100644 index 000000000..93a8e5c02 --- /dev/null +++ b/src/Psy/Plugin/AbstractPlugin.php @@ -0,0 +1,50 @@ +setAccessible(true); + $prop->setValue('Psy\Plugin\Manager', array()); + } + + public function testRegisterMultiplePlugins() + { + $mockedPlugin = $this->getMock('Psy\Plugin\AbstractPlugin'); + PluginManager::register($mockedPlugin, 'mock1'); + PluginManager::register($mockedPlugin, 'mock2'); + + $prop = new \ReflectionProperty('Psy\Plugin\PluginManager', 'plugins'); + $prop->setAccessible(true); + $plugins = $prop->getValue('Psy\Plugin\PluginManager'); + $this->assertArrayHasKey('mock1', $plugins); + $this->assertArrayHasKey('mock2', $plugins); + } + + public function testConfigurationWithSinglePlugin() + { + $commands = array( + 'cmd1', 'cmd2', + ); + + $presenters = array( + 'presenter1', 'presenter2', + ); + + $matchers = array( + 'matcher1', 'matcher2', + ); + + $stub = new PluginStub(); + $stub->setCommands($commands); + $stub->setPresenters($presenters); + $stub->setMatchers($matchers); + + PluginManager::register($stub, 'mock'); + + $config = PluginManager::getConfiguration(); + $this->assertArraySubset($commands, $config['commands']); + $this->assertArraySubset($presenters, $config['presenters']); + $this->assertArraySubset($matchers, $config['matchers']); + } + + public function testConfigurationWithMultiplePlugins() + { + $commands1 = array( + 'cmd1', 'cmd2', + ); + + $presenters1 = array( + 'presenter1', 'presenter2', + ); + + $matchers1 = array( + 'matcher1', 'matcher2', + ); + + $stub1 = new PluginStub(); + $stub1->setCommands($commands1); + $stub1->setPresenters($presenters1); + $stub1->setMatchers($matchers1); + + PluginManager::register($stub1, 'mock1'); + + $commands2 = array( + 'cmd3', 'cmd4', + ); + + $presenters2 = array( + 'presenter3', 'presenter4', + ); + + $matchers2 = array( + 'matcher3', 'matcher4', + ); + + $stub2 = new PluginStub(); + $stub2->setCommands($commands2); + $stub2->setPresenters($presenters2); + $stub2->setMatchers($matchers2); + + PluginManager::register($stub2, 'mock2'); + + $config = PluginManager::getConfiguration(); + $this->assertArraySubset($commands1, $config['commands']); + $this->assertArraySubset($presenters1, $config['presenters']); + $this->assertArraySubset($matchers1, $config['matchers']); + } +} diff --git a/test/Psy/Test/Plugin/PluginStub.php b/test/Psy/Test/Plugin/PluginStub.php new file mode 100644 index 000000000..3dc5436ec --- /dev/null +++ b/test/Psy/Test/Plugin/PluginStub.php @@ -0,0 +1,44 @@ +