From 9b39b57957cc9b4c2623102d530b4ada5d88727e Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Wed, 21 Feb 2018 16:45:14 +0100 Subject: [PATCH] Implemented getPluginEvent. This event gets called upon requestion a plugins using the Plugins::get() method, and can be used to cancel the request or provide with an alternative object. --- src/FuzeWorks/Event/PluginGetEvent.php | 93 +++++++++++++++++++++++ src/FuzeWorks/Plugins.php | 17 ++++- tests/events/event_pluginGetEventTest.php | 70 +++++++++++++++++ 3 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 src/FuzeWorks/Event/PluginGetEvent.php create mode 100644 tests/events/event_pluginGetEventTest.php diff --git a/src/FuzeWorks/Event/PluginGetEvent.php b/src/FuzeWorks/Event/PluginGetEvent.php new file mode 100644 index 0000000..76bbc2b --- /dev/null +++ b/src/FuzeWorks/Event/PluginGetEvent.php @@ -0,0 +1,93 @@ +. + * + * @author TechFuze + * @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net) + * @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/) + * @license http://opensource.org/licenses/GPL-3.0 GPLv3 License + * + * @link http://techfuze.net/fuzeworks + * @since Version 1.1.4 + * + * @version Version 1.1.4 + */ + +namespace FuzeWorks\Event; +use FuzeWorks\Event; + +/** + * Event that will get fired when a plugin is retrieved. + * + * @author Abel Hoogeveen + * @copyright Copyright (c) 2013 - 2018, Techfuze. (http://techfuze.net) + */ +class PluginGetEvent extends Event +{ + + /** + * The name of the plugin that should be loaded + * + * @var string + */ + public $pluginName; + + /** + * Array of directories where to look at for the plugin + * + * @var array + */ + public $directories = array(); + + /** + * Potential plugin to return instead. If set, the plugins class will return this object + * + * @var object + */ + public $plugin = null; + + public function init($pluginName, array $directories = array()) + { + $this->pluginName = $pluginName; + $this->directories = $directories; + } + + /** + * Allows listeners to set a plugin that will be returned instead. + * + * @param object $plugin + */ + public function setPlugin($plugin) + { + $this->plugin = $plugin; + } + + /** + * Plugin that will be returned if set by a listener. + * + * @return object|null $plugin + */ + public function getPlugin() + { + return $this->plugin; + } +} + +?> \ No newline at end of file diff --git a/src/FuzeWorks/Plugins.php b/src/FuzeWorks/Plugins.php index 5a092e5..34aa1aa 100644 --- a/src/FuzeWorks/Plugins.php +++ b/src/FuzeWorks/Plugins.php @@ -100,7 +100,7 @@ class Plugins /** * Load the header files of all plugins. */ - public function loadHeaders() + public function loadHeaders(): void { // Cycle through all pluginPaths $this->headers = array(); @@ -173,6 +173,21 @@ class Plugins $pluginFolder = $pluginName; $pluginName = ucfirst($pluginName); + // Fire pluginGetEvent, and cancel or return custom plugin if required + $event = Events::fireEvent('pluginGetEvent', $pluginName, $directories); + if ($event->isCancelled()) + { + return false; + } + elseif ($event->getPlugin() != null) + { + return $event->getPlugin(); + } + + // Otherwise just set the variables + $pluginName = $event->pluginName; + $directories = $event->directories; + // Check if the plugin is already loaded and return directly if (isset($this->plugins[$pluginName])) { diff --git a/tests/events/event_pluginGetEventTest.php b/tests/events/event_pluginGetEventTest.php new file mode 100644 index 0000000..7e5c251 --- /dev/null +++ b/tests/events/event_pluginGetEventTest.php @@ -0,0 +1,70 @@ +. + * + * @author TechFuze + * @copyright Copyright (c) 2013 - 2018, Techfuze. (http://techfuze.net) + * @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/) + * @license http://opensource.org/licenses/GPL-3.0 GPLv3 License + * + * @link http://techfuze.net/fuzeworks + * @since Version 1.1.4 + * + * @version Version 1.1.4 + */ +use FuzeWorks\Factory; +use FuzeWorks\Events; +use FuzeWorks\EventPriority; + +/** + * Class pluginGetEventTest. + */ +class pluginGetEventTest extends CoreTestAbstract +{ + /** + * Check if the event is fired when it should be. + */ + public function testPluginGetEvent() + { + // Create mock listener + Events::addListener( + function($event){$event->setCancelled(true);return $event;}, + 'pluginGetEvent', + EventPriority::NORMAL); + + // And fire the event + $this->assertFalse(Factory::getInstance()->plugins->get('test')); + } + + /** + * @depends testPluginGetEvent + */ + public function testReplacePlugin() + { + // Create mock listener + Events::addListener( + function($event){$event->setPlugin('test_string');return $event;}, + 'pluginGetEvent', + EventPriority::NORMAL); + + // And fire the event + $this->assertEquals('test_string', Factory::getInstance()->plugins->get('test')); + } +} \ No newline at end of file