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;
16 const HOOK_AUTH_USER = 8;
19 const KIND_SYSTEM = 2;
22 function __construct($link) {
26 private function register_plugin($name, $plugin) {
27 //array_push($this->plugins, $plugin);
28 $this->plugins[$name] = $plugin;
35 function get_plugins() {
36 return $this->plugins;
39 function get_plugin($name) {
40 return $this->plugins[$name];
43 function run_hooks($type, $method, $args) {
44 foreach ($this->get_hooks($type) as $hook) {
45 $hook->$method($args);
49 function add_hook($type, $sender) {
50 if (!is_array($this->hooks[$type])) {
51 $this->hooks[$type] = array();
54 array_push($this->hooks[$type], $sender);
57 function del_hook($type, $sender) {
58 if (is_array($this->hooks[$type])) {
59 $key = array_Search($this->hooks[$type], $sender);
61 unset($this->hooks[$type][$key]);
66 function get_hooks($type) {
67 if (isset($this->hooks[$type])) {
68 return $this->hooks[$type];
73 function load_all($kind) {
74 $plugins = array_map("basename", glob("plugins/*"));
75 $this->load(join(",", $plugins), $kind);
78 function load($classlist, $kind) {
79 $plugins = explode(",", $classlist);
81 foreach ($plugins as $class) {
82 $class = trim($class);
83 $class_file = strtolower(basename($class));
84 $file = dirname(__FILE__)."/../plugins/$class_file/$class_file.php";
86 if (!isset($this->plugins[$class])) {
87 if (file_exists($file)) require_once $file;
89 if (class_exists($class) && is_subclass_of($class, "Plugin")) {
90 $plugin = new $class($this);
93 case $this::KIND_SYSTEM:
94 if ($this->is_system($plugin)) {
96 $this->register_plugin($class, $plugin);
99 case $this::KIND_USER:
100 if (!$this->is_system($plugin)) {
101 $plugin->init($this);
102 $this->register_plugin($class, $plugin);
105 case $this::KIND_ALL:
106 $plugin->init($this);
107 $this->register_plugin($class, $plugin);
115 function is_system($plugin) {
116 $about = $plugin->about();
121 // only system plugins are allowed to modify routing
122 function add_handler($handler, $method, $sender) {
123 $handler = str_replace("-", "_", strtolower($handler));
124 $method = strtolower($method);
126 if ($this->is_system($sender)) {
127 if (!is_array($this->handlers[$handler])) {
128 $this->handlers[$handler] = array();
131 $this->handlers[$handler][$method] = $sender;
135 function del_handler($handler, $method) {
136 $handler = str_replace("-", "_", strtolower($handler));
137 $method = strtolower($method);
139 if ($this->is_system($sender)) {
140 unset($this->handlers[$handler][$method]);
144 function lookup_handler($handler, $method) {
145 $handler = str_replace("-", "_", strtolower($handler));
146 $method = strtolower($method);
148 if (is_array($this->handlers[$handler])) {
149 if (isset($this->handlers[$handler]["*"])) {
150 return $this->handlers[$handler]["*"];
152 return $this->handlers[$handler][$method];
159 function add_command($command, $description, $sender) {
160 $command = "-" . str_replace("-", "_", strtolower($command));
162 $this->commands[$command] = array("description" => $description,
166 function del_command($command) {
167 $command = "-" . strtolower($command);
169 unset($this->commands[$command]);
172 function lookup_command($command) {
173 $command = "-" . strtolower($command);
175 if (is_array($this->commands[$command])) {
176 return $this->commands[$command]["class"];
184 function get_commands() {
185 return $this->commands;
188 function run_commands($args) {
189 foreach ($this->get_commands() as $command => $data) {
190 if (in_array($command, $args)) {
191 $command = str_replace("-", "", $command);
192 $data["class"]->$command($args);