mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
PSR-4 Plugin Support (#8880)
Add phpdocblocks Plugins.php Add psr-4 plugins path to composer.json Changes to be committed: modified: LibreNMS/Plugins.php modified: composer.json
This commit is contained in:
committed by
Tony Murray
parent
19cdad8a1b
commit
67c585b7c0
@@ -15,73 +15,137 @@
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @subpackage Plugins
|
||||
* @author LibreNMS Group
|
||||
* @link http://librenms.org
|
||||
* @copyright 2016
|
||||
* @author
|
||||
*/
|
||||
|
||||
namespace LibreNMS;
|
||||
|
||||
use App\Models\Plugin;
|
||||
|
||||
/**
|
||||
* Handles loading of plugins
|
||||
*
|
||||
* @package LibreNMS
|
||||
* @subpackage Plugins
|
||||
* @author LibreNMS Group
|
||||
* @link http://librenms.org
|
||||
* @copyright 2016
|
||||
*
|
||||
* Supported hooks
|
||||
* <ul>
|
||||
* <li>menu</li>
|
||||
* <li>device_overview_container</li>
|
||||
* <li>port_container</li>
|
||||
* </ul>
|
||||
*/
|
||||
class Plugins
|
||||
{
|
||||
/**
|
||||
* Array of plugin hooks
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $plugins = null;
|
||||
|
||||
private static $plugins;
|
||||
|
||||
|
||||
/**
|
||||
* Start loading active plugins
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public static function start()
|
||||
{
|
||||
if (is_null(self::$plugins)) {
|
||||
self::$plugins = [];
|
||||
$plugin_dir = Config::get('plugin_dir');
|
||||
|
||||
if (file_exists($plugin_dir)) {
|
||||
// $plugin_files = scandir($config['plugin_dir']);
|
||||
$plugin_files = Plugin::isActive()->get()->toArray();
|
||||
foreach ($plugin_files as $plugins) {
|
||||
$plugin_info = pathinfo($plugin_dir.'/'.$plugins['plugin_name'].'/'.$plugins['plugin_name'].'.php');
|
||||
if ($plugin_info['extension'] == 'php') {
|
||||
if (is_file($plugin_dir.'/'.$plugins['plugin_name'].'/'.$plugins['plugin_name'].'.php')) {
|
||||
self::load($plugin_dir.'/'.$plugins['plugin_name'].'/'.$plugins['plugin_name'].'.php', $plugin_info['filename']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
if (!is_null(self::$plugins)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}//end start()
|
||||
self::$plugins = [];
|
||||
$plugin_dir = Config::get('plugin_dir');
|
||||
|
||||
if (!file_exists($plugin_dir)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$plugin_files = Plugin::isActive()->get()->toArray();
|
||||
foreach ($plugin_files as $plugins) {
|
||||
$plugin_file = $plugin_dir.'/'.$plugins['plugin_name'].'/'.$plugins['plugin_name'].'.php';
|
||||
$plugin_info = pathinfo($plugin_file);
|
||||
|
||||
if ($plugin_info['extension'] !== 'php') {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!is_file($plugin_file)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
self::load($plugin_file, $plugin_info['filename']);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load plugin
|
||||
*
|
||||
* @param string $file Full path and filename of plugin
|
||||
* @param string $pluginName Plugin name without any namespace
|
||||
* @return object|null
|
||||
*/
|
||||
public static function load($file, $pluginName)
|
||||
{
|
||||
include $file;
|
||||
$pluginFullName = 'LibreNMS\\Plugins\\' . $pluginName;
|
||||
if (class_exists($pluginFullName)) {
|
||||
$pluginName = $pluginFullName;
|
||||
$plugin = new $pluginFullName;
|
||||
} elseif (class_exists($pluginName)) {
|
||||
$plugin = new $pluginName;
|
||||
} else {
|
||||
|
||||
$plugin = false;
|
||||
$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_plugin) && !$plugin) {
|
||||
$pluginName = $ns_plugin;
|
||||
$plugin = new $ns_plugin();
|
||||
}
|
||||
|
||||
// Include file because it's not psr4
|
||||
if (!$plugin) {
|
||||
include $file;
|
||||
}
|
||||
|
||||
if (class_exists($ns_global) && !$plugin) {
|
||||
$pluginName = $ns_plugin;
|
||||
$plugin = new $ns_plugin();
|
||||
}
|
||||
|
||||
if (!$plugin) {
|
||||
return null;
|
||||
}
|
||||
$hooks = get_class_methods($plugin);
|
||||
|
||||
$hooks = get_class_methods($plugin);
|
||||
foreach ($hooks as $hookName) {
|
||||
if ($hookName{0} != '_') {
|
||||
self::$plugins[$hookName][] = $pluginName;
|
||||
}
|
||||
}
|
||||
|
||||
return $plugin;
|
||||
}//end load()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all plugins implementing a specific hook.
|
||||
*
|
||||
* @param string $hook Name of the hook to get count for
|
||||
* @return integer|boolean
|
||||
*/
|
||||
public static function countHooks($hook)
|
||||
{
|
||||
// count all plugins implementing a specific hook
|
||||
@@ -93,24 +157,37 @@ class Plugins
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Call hook for plugin.
|
||||
*
|
||||
* @param string $hook Name of hook to call
|
||||
* @param array $params Optional array of parameters for hook
|
||||
*/
|
||||
public static function call($hook, $params = false)
|
||||
{
|
||||
self::start();
|
||||
|
||||
if (!empty(self::$plugins[$hook])) {
|
||||
foreach (self::$plugins[$hook] as $name) {
|
||||
if (!is_array($params)) {
|
||||
@call_user_func(array($name, $hook));
|
||||
} else {
|
||||
@call_user_func_array(array($name, $hook), $params);
|
||||
}
|
||||
if (empty(self::$plugins[$hook])) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (self::$plugins[$hook] as $name) {
|
||||
if (!is_array($params)) {
|
||||
@call_user_func(array($name, $hook));
|
||||
} else {
|
||||
@call_user_func_array(array($name, $hook), $params);
|
||||
}
|
||||
}
|
||||
}//end call()
|
||||
}
|
||||
|
||||
/**
|
||||
* Get count of hooks.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public static function count()
|
||||
{
|
||||
self::start();
|
||||
return count(self::$plugins);
|
||||
}
|
||||
}//end class
|
||||
}
|
||||
|
@@ -74,6 +74,7 @@
|
||||
"psr-4": {
|
||||
"App\\": "app",
|
||||
"LibreNMS\\": "LibreNMS",
|
||||
"LibreNMS\\Plugins\\" : "html/plugins",
|
||||
"LibreNMS\\Tests\\": "tests"
|
||||
}
|
||||
},
|
||||
|
Reference in New Issue
Block a user