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