]>
Commit | Line | Data |
---|---|---|
19c73507 AD |
1 | <?php |
2 | class PluginHost { | |
3 | private $link; | |
4 | private $hooks = array(); | |
5 | private $plugins = array(); | |
8dcb2b47 | 6 | private $handlers = array(); |
19c73507 AD |
7 | |
8 | const HOOK_ARTICLE_BUTTON = 1; | |
9 | const HOOK_ARTICLE_FILTER = 2; | |
6065f3ad AD |
10 | const HOOK_PREFS_TAB = 3; |
11 | const HOOK_PREFS_SECTION = 4; | |
19c73507 AD |
12 | |
13 | function __construct($link) { | |
14 | $this->link = $link; | |
15 | } | |
16 | ||
17 | private function register_plugin($name, $plugin) { | |
18 | //array_push($this->plugins, $plugin); | |
19 | $this->plugins[$name] = $plugin; | |
20 | } | |
21 | ||
22 | function get_link() { | |
23 | return $this->link; | |
24 | } | |
25 | ||
26 | function get_plugins() { | |
27 | return $this->plugins; | |
28 | } | |
29 | ||
30 | function get_plugin($name) { | |
31 | return $this->plugins[$name]; | |
32 | } | |
33 | ||
6065f3ad AD |
34 | function run_hooks($type, $method, $args) { |
35 | foreach ($this->get_hooks($type) as $hook) { | |
36 | $hook->$method($args); | |
37 | } | |
38 | } | |
39 | ||
19c73507 AD |
40 | function add_hook($type, $sender) { |
41 | if (!is_array($this->hooks[$type])) { | |
42 | $this->hooks[$type] = array(); | |
43 | } | |
44 | ||
45 | array_push($this->hooks[$type], $sender); | |
46 | } | |
47 | ||
48 | function del_hook($type, $sender) { | |
49 | if (is_array($this->hooks[$type])) { | |
50 | $key = array_Search($this->hooks[$type], $sender); | |
51 | if ($key !== FALSE) { | |
52 | unset($this->hooks[$type][$key]); | |
53 | } | |
54 | } | |
55 | } | |
56 | ||
57 | function get_hooks($type) { | |
58 | return $this->hooks[$type]; | |
59 | } | |
60 | ||
61 | function load($classlist) { | |
62 | $plugins = explode(",", $classlist); | |
63 | ||
64 | foreach ($plugins as $class) { | |
65 | $class = trim($class); | |
8dcb2b47 | 66 | $class_file = strtolower(basename($class)); |
19c73507 AD |
67 | $file = dirname(__FILE__)."/../plugins/$class_file/$class_file.php"; |
68 | ||
69 | if (file_exists($file)) require_once $file; | |
70 | ||
5a0e0392 | 71 | if (class_exists($class) && is_subclass_of($class, "Plugin")) { |
19c73507 AD |
72 | $plugin = new $class($this); |
73 | ||
74 | $this->register_plugin($class, $plugin); | |
75 | } | |
76 | } | |
77 | } | |
78 | ||
8dcb2b47 AD |
79 | function add_handler($handler, $method, $sender) { |
80 | $handler = strtolower($handler); | |
81 | $method = strtolower($method); | |
82 | ||
83 | if (!is_array($this->handlers[$handler])) { | |
84 | $this->handlers[$handler] = array(); | |
85 | } | |
86 | ||
87 | $this->handlers[$handler][$method] = $sender; | |
88 | } | |
89 | ||
90 | function del_handler($handler, $method) { | |
91 | $handler = strtolower($handler); | |
92 | $method = strtolower($method); | |
93 | ||
94 | unset($this->handlers[$handler][$method]); | |
95 | } | |
96 | ||
97 | function lookup_handler($handler, $method) { | |
98 | $handler = strtolower($handler); | |
99 | $method = strtolower($method); | |
100 | ||
101 | if (is_array($this->handlers[$handler])) { | |
102 | return $this->handlers[$handler][$method]; | |
103 | } | |
104 | ||
105 | return false; | |
106 | } | |
19c73507 AD |
107 | } |
108 | ?> |