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