]>
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(); |
73f28fe9 | 7 | private $commands = array(); |
19c73507 AD |
8 | |
9 | const HOOK_ARTICLE_BUTTON = 1; | |
10 | const HOOK_ARTICLE_FILTER = 2; | |
6065f3ad AD |
11 | const HOOK_PREFS_TAB = 3; |
12 | const HOOK_PREFS_SECTION = 4; | |
6cbe53c9 | 13 | const HOOK_PREFS_TABS = 5; |
19c73507 AD |
14 | |
15 | function __construct($link) { | |
16 | $this->link = $link; | |
17 | } | |
18 | ||
19 | private function register_plugin($name, $plugin) { | |
20 | //array_push($this->plugins, $plugin); | |
21 | $this->plugins[$name] = $plugin; | |
22 | } | |
23 | ||
24 | function get_link() { | |
25 | return $this->link; | |
26 | } | |
27 | ||
28 | function get_plugins() { | |
29 | return $this->plugins; | |
30 | } | |
31 | ||
32 | function get_plugin($name) { | |
33 | return $this->plugins[$name]; | |
34 | } | |
35 | ||
6065f3ad AD |
36 | function run_hooks($type, $method, $args) { |
37 | foreach ($this->get_hooks($type) as $hook) { | |
38 | $hook->$method($args); | |
39 | } | |
40 | } | |
41 | ||
19c73507 AD |
42 | function add_hook($type, $sender) { |
43 | if (!is_array($this->hooks[$type])) { | |
44 | $this->hooks[$type] = array(); | |
45 | } | |
46 | ||
47 | array_push($this->hooks[$type], $sender); | |
48 | } | |
49 | ||
50 | function del_hook($type, $sender) { | |
51 | if (is_array($this->hooks[$type])) { | |
52 | $key = array_Search($this->hooks[$type], $sender); | |
53 | if ($key !== FALSE) { | |
54 | unset($this->hooks[$type][$key]); | |
55 | } | |
56 | } | |
57 | } | |
58 | ||
59 | function get_hooks($type) { | |
19b3992b AD |
60 | if (isset($this->hooks[$type])) { |
61 | return $this->hooks[$type]; | |
62 | } else { | |
63 | return array(); | |
64 | } | |
19c73507 AD |
65 | } |
66 | ||
67 | function load($classlist) { | |
68 | $plugins = explode(",", $classlist); | |
69 | ||
70 | foreach ($plugins as $class) { | |
71 | $class = trim($class); | |
8dcb2b47 | 72 | $class_file = strtolower(basename($class)); |
19c73507 AD |
73 | $file = dirname(__FILE__)."/../plugins/$class_file/$class_file.php"; |
74 | ||
75 | if (file_exists($file)) require_once $file; | |
76 | ||
5a0e0392 | 77 | if (class_exists($class) && is_subclass_of($class, "Plugin")) { |
19c73507 AD |
78 | $plugin = new $class($this); |
79 | ||
80 | $this->register_plugin($class, $plugin); | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
8dcb2b47 | 85 | function add_handler($handler, $method, $sender) { |
6cbe53c9 | 86 | $handler = str_replace("-", "_", strtolower($handler)); |
8dcb2b47 AD |
87 | $method = strtolower($method); |
88 | ||
89 | if (!is_array($this->handlers[$handler])) { | |
90 | $this->handlers[$handler] = array(); | |
91 | } | |
92 | ||
93 | $this->handlers[$handler][$method] = $sender; | |
94 | } | |
95 | ||
96 | function del_handler($handler, $method) { | |
6cbe53c9 | 97 | $handler = str_replace("-", "_", strtolower($handler)); |
8dcb2b47 AD |
98 | $method = strtolower($method); |
99 | ||
100 | unset($this->handlers[$handler][$method]); | |
101 | } | |
102 | ||
103 | function lookup_handler($handler, $method) { | |
6cbe53c9 | 104 | $handler = str_replace("-", "_", strtolower($handler)); |
8dcb2b47 AD |
105 | $method = strtolower($method); |
106 | ||
107 | if (is_array($this->handlers[$handler])) { | |
6cbe53c9 AD |
108 | if (isset($this->handlers[$handler]["*"])) { |
109 | return $this->handlers[$handler]["*"]; | |
110 | } else { | |
111 | return $this->handlers[$handler][$method]; | |
112 | } | |
8dcb2b47 AD |
113 | } |
114 | ||
115 | return false; | |
116 | } | |
73f28fe9 AD |
117 | |
118 | function add_command($command, $description, $sender) { | |
119 | $command = "-" . str_replace("-", "_", strtolower($command)); | |
120 | ||
121 | $this->commands[$command] = array("description" => $description, | |
122 | "class" => $sender); | |
123 | } | |
124 | ||
125 | function del_command($command) { | |
126 | $command = "-" . strtolower($command); | |
127 | ||
128 | unset($this->commands[$command]); | |
129 | } | |
130 | ||
131 | function lookup_command($command) { | |
132 | $command = "-" . strtolower($command); | |
133 | ||
134 | if (is_array($this->commands[$command])) { | |
135 | return $this->commands[$command]["class"]; | |
136 | } else { | |
137 | return false; | |
138 | } | |
139 | ||
140 | return false; | |
141 | } | |
142 | ||
143 | function get_commands() { | |
144 | return $this->commands; | |
145 | } | |
146 | ||
147 | function run_commands($args) { | |
148 | foreach ($this->get_commands() as $command => $data) { | |
149 | if (in_array($command, $args)) { | |
150 | $command = str_replace("-", "", $command); | |
151 | $data["class"]->$command($args); | |
152 | } | |
153 | } | |
154 | } | |
155 | ||
19c73507 AD |
156 | } |
157 | ?> |