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