From 483c931e3dabe4ad34f6de173c619195c198142a Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Tue, 17 Jul 2018 13:55:23 -0500 Subject: [PATCH] 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 --- LibreNMS/Plugins.php | 59 +++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/LibreNMS/Plugins.php b/LibreNMS/Plugins.php index b9b25d2d6f..5e6997a755 100644 --- a/LibreNMS/Plugins.php +++ b/LibreNMS/Plugins.php @@ -100,44 +100,51 @@ class Plugins */ 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_psr4 = $ns_prefix.$pluginName.'\\'.$pluginName; $ns_plugin = $ns_prefix.$pluginName; $ns_global = $pluginName; - if (class_exists($ns_psr4) && !$plugin) { - $pluginName = $ns_psr4; - $plugin = new $ns_psr4; + if (class_exists($ns_psr4)) { + return new $ns_psr4; } - if (class_exists($ns_plugin) && !$plugin) { - $pluginName = $ns_plugin; - $plugin = new $ns_plugin(); + if (class_exists($ns_plugin)) { + return new $ns_plugin; } - // Include file because it's not psr4 - if (!$plugin) { - include $file; + // Include file because it's not psr4 (may have been included by previous class_exists calls + include_once $file; + + if (class_exists($ns_global)) { + return new $ns_global; } - if (class_exists($ns_global) && !$plugin) { - $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; + return null; } /**