]>
git.wh0rd.org - tt-rss.git/blob - classes/pluginhost.php
4 private $hooks = array();
5 private $plugins = array();
6 private $handlers = array();
7 private $commands = array();
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;
18 const KIND_SYSTEM
= 2;
21 function __construct($link) {
25 private function register_plugin($name, $plugin) {
26 //array_push($this->plugins, $plugin);
27 $this->plugins
[$name] = $plugin;
34 function get_plugins() {
35 return $this->plugins
;
38 function get_plugin($name) {
39 return $this->plugins
[$name];
42 function run_hooks($type, $method, $args) {
43 foreach ($this->get_hooks($type) as $hook) {
44 $hook->$method($args);
48 function add_hook($type, $sender) {
49 if (!is_array($this->hooks
[$type])) {
50 $this->hooks
[$type] = array();
53 array_push($this->hooks
[$type], $sender);
56 function del_hook($type, $sender) {
57 if (is_array($this->hooks
[$type])) {
58 $key = array_Search($this->hooks
[$type], $sender);
60 unset($this->hooks
[$type][$key]);
65 function get_hooks($type) {
66 if (isset($this->hooks
[$type])) {
67 return $this->hooks
[$type];
72 function load_all($kind) {
73 $plugins = array_map("basename", glob("plugins/*"));
74 $this->load(join(",", $plugins), $kind);
77 function load($classlist, $kind) {
78 $plugins = explode(",", $classlist);
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";
85 if (!isset($this->plugins
[$class])) {
86 if (file_exists($file)) require_once $file;
88 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
89 $plugin = new $class($this);
92 case $this::KIND_SYSTEM
:
93 if ($this->is_system($plugin)) {
95 $this->register_plugin($class, $plugin);
98 case $this::KIND_USER
:
99 if (!$this->is_system($plugin)) {
100 $plugin->init($this);
101 $this->register_plugin($class, $plugin);
104 case $this::KIND_ALL
:
105 $plugin->init($this);
106 $this->register_plugin($class, $plugin);
114 function is_system($plugin) {
115 $about = $plugin->about();
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);
125 if ($this->is_system($sender)) {
126 if (!is_array($this->handlers
[$handler])) {
127 $this->handlers
[$handler] = array();
130 $this->handlers
[$handler][$method] = $sender;
134 function del_handler($handler, $method) {
135 $handler = str_replace("-", "_", strtolower($handler));
136 $method = strtolower($method);
138 if ($this->is_system($sender)) {
139 unset($this->handlers
[$handler][$method]);
143 function lookup_handler($handler, $method) {
144 $handler = str_replace("-", "_", strtolower($handler));
145 $method = strtolower($method);
147 if (is_array($this->handlers
[$handler])) {
148 if (isset($this->handlers
[$handler]["*"])) {
149 return $this->handlers
[$handler]["*"];
151 return $this->handlers
[$handler][$method];
158 function add_command($command, $description, $sender) {
159 $command = "-" . str_replace("-", "_", strtolower($command));
161 $this->commands
[$command] = array("description" => $description,
165 function del_command($command) {
166 $command = "-" . strtolower($command);
168 unset($this->commands
[$command]);
171 function lookup_command($command) {
172 $command = "-" . strtolower($command);
174 if (is_array($this->commands
[$command])) {
175 return $this->commands
[$command]["class"];
183 function get_commands() {
184 return $this->commands
;
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);