-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
plugin manager #171
Open
Markcial
wants to merge
5
commits into
bobthecow:develop
Choose a base branch
from
Markcial:plugin_manager
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
plugin manager #171
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
243a74d
plugin manager
Markcial 5971754
added manager configuration load in the configuration class
Markcial c9b34d8
Added parameter that forbids plugin discovery
Markcial d44d773
boolean enforcer added to bolean parameter
Markcial f1d7610
Added testing, refactored a little bit the configuration building ser…
Markcial File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
namespace Psy\Plugin; | ||
|
||
abstract class AbstractPlugin | ||
{ | ||
public static function register() | ||
{ | ||
PluginManager::register(new static(), static::getName()); | ||
} | ||
|
||
/** | ||
* @return string | ||
* | ||
* @throws \Exception | ||
*/ | ||
public static function getName() | ||
{ | ||
throw new \Exception('Missing plugin name'); | ||
} | ||
|
||
// any publicly exposed configuration piece below here ↓ | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public static function getCommands() | ||
{ | ||
// add your own commands | ||
return array(); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public static function getPresenters() | ||
{ | ||
// add your own presenters | ||
return array(); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public static function getMatchers() | ||
{ | ||
// add your own presenters | ||
return array(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Psy\Plugin; | ||
|
||
class PluginManager | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected static $exposedConfigurationItems = array( | ||
'commands', 'matchers', 'presenters', | ||
); | ||
|
||
/** @var AbstractPlugin[] */ | ||
protected static $plugins = array(); | ||
|
||
/** | ||
* @param AbstractPlugin $plugin | ||
* @param $name | ||
* | ||
* @throws \Exception | ||
*/ | ||
public static function register(AbstractPlugin $plugin, $name) | ||
{ | ||
if (array_key_exists($name, self::$plugins)) { | ||
throw new \Exception( | ||
sprintf('The plugin "%s" was already registered.', $name) | ||
); | ||
} | ||
self::$plugins[$name] = $plugin; | ||
} | ||
|
||
/** | ||
* @param array $configuration | ||
* | ||
* @return array | ||
*/ | ||
public static function getConfiguration($configuration = array()) | ||
{ | ||
foreach (self::$plugins as $plugin) { | ||
foreach (self::$exposedConfigurationItems as $cfgBlock) { | ||
$getter = sprintf('get%s', ucfirst($cfgBlock)); | ||
$cfgData = call_user_func(array($plugin, $getter)); | ||
if (array_key_exists($cfgBlock, $configuration)) { | ||
if (is_array($configuration[$cfgBlock])) { | ||
// is array, let's merge | ||
$configuration[$cfgBlock] = array_merge( | ||
$configuration[$cfgBlock], | ||
$cfgData | ||
); | ||
} else { | ||
// not an array, it will be overwritten | ||
$configuration[$cfgBlock] = $cfgData; | ||
} | ||
} else { | ||
$configuration[$cfgBlock] = $cfgData; | ||
} | ||
} | ||
} | ||
|
||
return $configuration; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Psy\Test\Plugin; | ||
|
||
use Psy\Plugin\PluginManager; | ||
|
||
class ManagerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function setup() | ||
{ | ||
$prop = new \ReflectionProperty('Psy\Plugin\PluginManager', 'plugins'); | ||
$prop->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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Psy\Test\Plugin; | ||
|
||
use Psy\Plugin\AbstractPlugin; | ||
|
||
class PluginStub extends AbstractPlugin | ||
{ | ||
// due to the static nature, and the negative of Mr bergmann to audit static code | ||
// the data here is treated as a FIFO pile. | ||
protected static $matchers = array(); | ||
protected static $presenters = array(); | ||
protected static $commands = array(); | ||
|
||
public function setMatchers(array $matchers) | ||
{ | ||
self::$matchers[] = $matchers; | ||
} | ||
|
||
public function setPresenters(array $presenters) | ||
{ | ||
self::$presenters[] = $presenters; | ||
} | ||
|
||
public function setCommands(array $commands) | ||
{ | ||
self::$commands[] = $commands; | ||
} | ||
|
||
public static function getMatchers() | ||
{ | ||
return array_shift(self::$matchers); | ||
} | ||
|
||
public static function getPresenters() | ||
{ | ||
return array_shift(self::$presenters); | ||
} | ||
|
||
public static function getCommands() | ||
{ | ||
return array_shift(self::$commands); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this class be made final perhapse?