]>
Commit | Line | Data |
---|---|---|
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_TAB_SECTION = 4; | |
13 | const HOOK_PREFS_TABS = 5; | |
14 | const HOOK_FEED_PARSED = 6; | |
15 | const HOOK_UPDATE_TASK = 7; | |
16 | ||
17 | const KIND_ALL = 1; | |
18 | const KIND_SYSTEM = 2; | |
19 | const KIND_USER = 3; | |
20 | ||
21 | function __construct($link) { | |
22 | $this->link = $link; | |
23 | } | |
24 | ||
25 | private function register_plugin($name, $plugin) { | |
26 | //array_push($this->plugins, $plugin); | |
27 | $this->plugins[$name] = $plugin; | |
28 | } | |
29 | ||
30 | function get_link() { | |
31 | return $this->link; | |
32 | } | |
33 | ||
34 | function get_plugins() { | |
35 | return $this->plugins; | |
36 | } | |
37 | ||
38 | function get_plugin($name) { | |
39 | return $this->plugins[$name]; | |
40 | } | |
41 | ||
42 | function run_hooks($type, $method, $args) { | |
43 | foreach ($this->get_hooks($type) as $hook) { | |
44 | $hook->$method($args); | |
45 | } | |
46 | } | |
47 | ||
48 | function add_hook($type, $sender) { | |
49 | if (!is_array($this->hooks[$type])) { | |
50 | $this->hooks[$type] = array(); | |
51 | } | |
52 | ||
53 | array_push($this->hooks[$type], $sender); | |
54 | } | |
55 | ||
56 | function del_hook($type, $sender) { | |
57 | if (is_array($this->hooks[$type])) { | |
58 | $key = array_Search($this->hooks[$type], $sender); | |
59 | if ($key !== FALSE) { | |
60 | unset($this->hooks[$type][$key]); | |
61 | } | |
62 | } | |
63 | } | |
64 | ||
65 | function get_hooks($type) { | |
66 | if (isset($this->hooks[$type])) { | |
67 | return $this->hooks[$type]; | |
68 | } else { | |
69 | return array(); | |
70 | } | |
71 | } | |
72 | function load_all($kind) { | |
73 | $plugins = array_map("basename", glob("plugins/*")); | |
74 | $this->load(join(",", $plugins), $kind); | |
75 | } | |
76 | ||
77 | function load($classlist, $kind) { | |
78 | $plugins = explode(",", $classlist); | |
79 | ||
80 | foreach ($plugins as $class) { | |
81 | $class = trim($class); | |
82 | $class_file = strtolower(basename($class)); | |
83 | $file = dirname(__FILE__)."/../plugins/$class_file/$class_file.php"; | |
84 | ||
85 | if (!isset($this->plugins[$class])) { | |
86 | if (file_exists($file)) require_once $file; | |
87 | ||
88 | if (class_exists($class) && is_subclass_of($class, "Plugin")) { | |
89 | $plugin = new $class($this); | |
90 | ||
91 | switch ($kind) { | |
92 | case $this::KIND_SYSTEM: | |
93 | if ($this->is_system($plugin)) { | |
94 | $plugin->init($this); | |
95 | $this->register_plugin($class, $plugin); | |
96 | } | |
97 | break; | |
98 | case $this::KIND_USER: | |
99 | if (!$this->is_system($plugin)) { | |
100 | $plugin->init($this); | |
101 | $this->register_plugin($class, $plugin); | |
102 | } | |
103 | break; | |
104 | case $this::KIND_ALL: | |
105 | $plugin->init($this); | |
106 | $this->register_plugin($class, $plugin); | |
107 | break; | |
108 | } | |
109 | } | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
114 | function is_system($plugin) { | |
115 | $about = $plugin->about(); | |
116 | ||
117 | return @$about[3]; | |
118 | } | |
119 | ||
120 | // only system plugins are allowed to modify routing | |
121 | function add_handler($handler, $method, $sender) { | |
122 | $handler = str_replace("-", "_", strtolower($handler)); | |
123 | $method = strtolower($method); | |
124 | ||
125 | if ($this->is_system($sender)) { | |
126 | if (!is_array($this->handlers[$handler])) { | |
127 | $this->handlers[$handler] = array(); | |
128 | } | |
129 | ||
130 | $this->handlers[$handler][$method] = $sender; | |
131 | } | |
132 | } | |
133 | ||
134 | function del_handler($handler, $method) { | |
135 | $handler = str_replace("-", "_", strtolower($handler)); | |
136 | $method = strtolower($method); | |
137 | ||
138 | if ($this->is_system($sender)) { | |
139 | unset($this->handlers[$handler][$method]); | |
140 | } | |
141 | } | |
142 | ||
143 | function lookup_handler($handler, $method) { | |
144 | $handler = str_replace("-", "_", strtolower($handler)); | |
145 | $method = strtolower($method); | |
146 | ||
147 | if (is_array($this->handlers[$handler])) { | |
148 | if (isset($this->handlers[$handler]["*"])) { | |
149 | return $this->handlers[$handler]["*"]; | |
150 | } else { | |
151 | return $this->handlers[$handler][$method]; | |
152 | } | |
153 | } | |
154 | ||
155 | return false; | |
156 | } | |
157 | ||
158 | function add_command($command, $description, $sender) { | |
159 | $command = "-" . str_replace("-", "_", strtolower($command)); | |
160 | ||
161 | $this->commands[$command] = array("description" => $description, | |
162 | "class" => $sender); | |
163 | } | |
164 | ||
165 | function del_command($command) { | |
166 | $command = "-" . strtolower($command); | |
167 | ||
168 | unset($this->commands[$command]); | |
169 | } | |
170 | ||
171 | function lookup_command($command) { | |
172 | $command = "-" . strtolower($command); | |
173 | ||
174 | if (is_array($this->commands[$command])) { | |
175 | return $this->commands[$command]["class"]; | |
176 | } else { | |
177 | return false; | |
178 | } | |
179 | ||
180 | return false; | |
181 | } | |
182 | ||
183 | function get_commands() { | |
184 | return $this->commands; | |
185 | } | |
186 | ||
187 | function run_commands($args) { | |
188 | foreach ($this->get_commands() as $command => $data) { | |
189 | if (in_array($command, $args)) { | |
190 | $command = str_replace("-", "", $command); | |
191 | $data["class"]->$command($args); | |
192 | } | |
193 | } | |
194 | } | |
195 | ||
196 | } | |
197 | ?> |