]> git.wh0rd.org - tt-rss.git/commitdiff
add support for plugins adding API methods
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>
Fri, 12 Apr 2013 04:18:43 +0000 (08:18 +0400)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Fri, 12 Apr 2013 04:18:43 +0000 (08:18 +0400)
classes/api.php
classes/pluginhost.php
plugins/example_api/init.php [new file with mode: 0644]

index aaeb86dcfc8ed87b8ab3b7c507d4ef158334a908..0fcd65eb3bb9905d5473dff5c960b8acc6aa1b07 100644 (file)
@@ -464,8 +464,19 @@ class API extends Handler {
 
        }
 
-       function index() {
-               print $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD'));
+       function index($method) {
+               global $pluginhost;
+
+               $plugin = $pluginhost->get_api_method(strtolower($method));
+
+               if ($plugin && method_exists($plugin, $method)) {
+                       $reply = $plugin->$method();
+
+                       print $this->wrap($reply[0], $reply[1]);
+
+               } else {
+                       print $this->wrap(self::STATUS_ERR, array("error" => 'UNKNOWN_METHOD', "method" => $method));
+               }
        }
 
        function shareToPublished() {
index 9ae7b809e948935cfc98393c1bd76cd27b1a1937..7c6fab9a0ebcf8740d5a2380ecfa2a8cc4a8b292 100644 (file)
@@ -7,6 +7,7 @@ class PluginHost {
        private $commands = array();
        private $storage = array();
        private $feeds = array();
+       private $api_methods = array();
        private $owner_uid;
        private $debug;
 
@@ -347,5 +348,14 @@ class PluginHost {
                return PLUGIN_FEED_BASE_INDEX - 1 + abs($feed);
        }
 
+       function add_api_method($name, $sender) {
+               if ($this->is_system($sender)) {
+                       $this->api_methods[strtolower($name)] = $sender;
+               }
+       }
+
+       function get_api_method($name) {
+               return $this->api_methods[$name];
+       }
 }
 ?>
diff --git a/plugins/example_api/init.php b/plugins/example_api/init.php
new file mode 100644 (file)
index 0000000..a5f34b8
--- /dev/null
@@ -0,0 +1,31 @@
+<?php
+class Example_Api extends Plugin {
+
+       // Demonstrates adding a method to the API
+       // Plugin methods return an array containint
+       // 1. status (STATUS_OK or STATUS_ERR)
+       // 2. arbitrary payload
+
+       private $link;
+       private $host;
+
+       function about() {
+               return array(1.0,
+                       "Example plugin adding an API method",
+                       "fox",
+                       true,
+                       "http://tt-rss.org/");
+       }
+
+       function init($host) {
+               $this->link = $host->get_link();
+               $this->host = $host;
+
+               $host->add_api_method("example_testmethod", $this);
+       }
+
+       function example_testmethod() {
+               return array(API::STATUS_OK, array("current_time" => time()));
+       }
+}
+?>