-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplugins.class.php
More file actions
104 lines (100 loc) · 2.68 KB
/
plugins.class.php
File metadata and controls
104 lines (100 loc) · 2.68 KB
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
<?php
/**
* This is a plugin class for all kind of php programms.
* With this plugin system you are able to load 3rd
* party plugins in any place of your source code.
*
* @author: Martin Lantzsch <martin@linux-doku.de>
* @website: http://linux-doku.de
* @licence: GPL
* @version: 0.1
*/
class plugins {
/**
* All hooks with plugins
*/
private static $plugins = array();
/**
* Start plugin system and define config params
*
* @param string $folder location of the plugin classes (must have a ending / )
*/
public static function start($folder) {
// check if the plugin folder exists
if(file_exists($folder))
{
// sart to scan this folder
$pluginFiles = scandir($folder);
// run trough all these files
foreach($pluginFiles as $name)
{
// get file infos
$pluginInfos = pathinfo($folder.$name);
// check if file is a PHP file
if($pluginInfos['extension'] == 'php' || $pluginInfos['extension'] == 'php5')
{
// call the plugin system and load it
self::load($folder.$name, $pluginInfos['filename']);
// TIPP: if you want you can extend the code here, the
// returns the plugin object
}
}
return true;
} else {
return false;
}
// loading plugins done!
}
/**
* Load plugin class from given file
*
* @param string $file folder of the plugin (with ending / )
* @param string $pluginName filename of the plugin
* @return object
*/
public static function load($file, $pluginName) {
// include plugin file
include($file);
// make an object
$plugin = new $pluginName;
// get methods as hooks
$hooks = get_class_methods($plugin);
// go through all hooks
foreach($hooks as $hookName)
{
// load only hooks without underscore as first char
if($hookName{0} != '_')
{
// put to plugins array
self::$plugins[$hookName][] = $pluginName;
}
}
// done, return object
return $plugin;
}
/**
* Call a hook, if you want with params
*
* @param string $hook name of the hook
* @param array $params params as array
*/
public static function call($hook, $params=false) {
// look if hooks existing
if(count(self::$plugins[$hook]) != 0)
{
// go throug all plugins
foreach(self::$plugins[$hook] as $name)
{
// check if params are given
if(!is_array($params))
{
// call plugin without params
call_user_func(array($name, $hook));
} else {
// call plugin with params
call_user_func_array(array($name, $hook), $params);
}
}
}
}
}