]> git.wh0rd.org - tt-rss.git/blame - classes/pluginhost.php
update translations
[tt-rss.git] / classes / pluginhost.php
CommitLineData
19c73507
AD
1<?php
2class 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) {
60 return $this->hooks[$type];
61 }
62
63 function load($classlist) {
64 $plugins = explode(",", $classlist);
65
66 foreach ($plugins as $class) {
67 $class = trim($class);
8dcb2b47 68 $class_file = strtolower(basename($class));
19c73507
AD
69 $file = dirname(__FILE__)."/../plugins/$class_file/$class_file.php";
70
71 if (file_exists($file)) require_once $file;
72
5a0e0392 73 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
19c73507
AD
74 $plugin = new $class($this);
75
76 $this->register_plugin($class, $plugin);
77 }
78 }
79 }
80
8dcb2b47 81 function add_handler($handler, $method, $sender) {
6cbe53c9 82 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
83 $method = strtolower($method);
84
85 if (!is_array($this->handlers[$handler])) {
86 $this->handlers[$handler] = array();
87 }
88
89 $this->handlers[$handler][$method] = $sender;
90 }
91
92 function del_handler($handler, $method) {
6cbe53c9 93 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
94 $method = strtolower($method);
95
96 unset($this->handlers[$handler][$method]);
97 }
98
99 function lookup_handler($handler, $method) {
6cbe53c9 100 $handler = str_replace("-", "_", strtolower($handler));
8dcb2b47
AD
101 $method = strtolower($method);
102
103 if (is_array($this->handlers[$handler])) {
6cbe53c9
AD
104 if (isset($this->handlers[$handler]["*"])) {
105 return $this->handlers[$handler]["*"];
106 } else {
107 return $this->handlers[$handler][$method];
108 }
8dcb2b47
AD
109 }
110
111 return false;
112 }
73f28fe9
AD
113
114 function add_command($command, $description, $sender) {
115 $command = "-" . str_replace("-", "_", strtolower($command));
116
117 $this->commands[$command] = array("description" => $description,
118 "class" => $sender);
119 }
120
121 function del_command($command) {
122 $command = "-" . strtolower($command);
123
124 unset($this->commands[$command]);
125 }
126
127 function lookup_command($command) {
128 $command = "-" . strtolower($command);
129
130 if (is_array($this->commands[$command])) {
131 return $this->commands[$command]["class"];
132 } else {
133 return false;
134 }
135
136 return false;
137 }
138
139 function get_commands() {
140 return $this->commands;
141 }
142
143 function run_commands($args) {
144 foreach ($this->get_commands() as $command => $data) {
145 if (in_array($command, $args)) {
146 $command = str_replace("-", "", $command);
147 $data["class"]->$command($args);
148 }
149 }
150 }
151
19c73507
AD
152}
153?>