forked from librenms/librenms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlugins.php
106 lines (89 loc) · 3.05 KB
/
Plugins.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* Plugins.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* 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/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2016
* @author
*/
namespace LibreNMS;
use App\Models\Plugin;
class Plugins
{
private static $plugins;
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;
}
}
return false;
}//end start()
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 {
return null;
}
$hooks = get_class_methods($plugin);
foreach ($hooks as $hookName) {
if ($hookName{0} != '_') {
self::$plugins[$hookName][] = $pluginName;
}
}
return $plugin;
}//end load()
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);
}
}
}
}//end call()
public static function count()
{
self::start();
return count(self::$plugins);
}
}//end class