Fix plugin loading (#8917)

* Fix plugin loading
class_exists was loading the including the files, then we included it again.
Mostly, just include -> include_once is the fix.

* fix style

* forgot to update docblock
This commit is contained in:
Tony Murray
2018-07-17 13:55:23 -05:00
committed by GitHub
parent b01ccd7f9c
commit 483c931e3d

View File

@@ -100,44 +100,51 @@ class Plugins
*/ */
public static function load($file, $pluginName) public static function load($file, $pluginName)
{ {
$plugin = self::getInstance($file, $pluginName);
$plugin = false; $class = get_class($plugin);
$hooks = get_class_methods($class);
foreach ((array)$hooks as $hookName) {
if ($hookName[0] != '_') {
self::$plugins[$hookName][] = $class;
}
}
return $plugin;
}
/**
* Get an instance of this plugin
* Search various namespaces and include files if needed.
*
* @param string $file
* @param string $pluginName
* @return object|null
*/
private static function getInstance($file, $pluginName)
{
$ns_prefix = 'LibreNMS\\Plugins\\'; $ns_prefix = 'LibreNMS\\Plugins\\';
$ns_psr4 = $ns_prefix.$pluginName.'\\'.$pluginName; $ns_psr4 = $ns_prefix.$pluginName.'\\'.$pluginName;
$ns_plugin = $ns_prefix.$pluginName; $ns_plugin = $ns_prefix.$pluginName;
$ns_global = $pluginName; $ns_global = $pluginName;
if (class_exists($ns_psr4) && !$plugin) { if (class_exists($ns_psr4)) {
$pluginName = $ns_psr4; return new $ns_psr4;
$plugin = new $ns_psr4;
} }
if (class_exists($ns_plugin) && !$plugin) { if (class_exists($ns_plugin)) {
$pluginName = $ns_plugin; return new $ns_plugin;
$plugin = new $ns_plugin();
} }
// Include file because it's not psr4 // Include file because it's not psr4 (may have been included by previous class_exists calls
if (!$plugin) { include_once $file;
include $file;
if (class_exists($ns_global)) {
return new $ns_global;
} }
if (class_exists($ns_global) && !$plugin) { return null;
$pluginName = $ns_plugin;
$plugin = new $ns_plugin();
}
if (!$plugin) {
return null;
}
$hooks = get_class_methods($plugin);
foreach ($hooks as $hookName) {
if ($hookName{0} != '_') {
self::$plugins[$hookName][] = $pluginName;
}
}
return $plugin;
} }
/** /**