xref: /dokuwiki/lib/plugins/remote.php (revision 836f6efbf31a2a263102aea61ef0cc5d577aa9bb)
1<?php
2
3/**
4 * Class DokuWiki_Remote_Plugin
5 */
6abstract class DokuWiki_Remote_Plugin extends DokuWiki_Plugin {
7
8    private  $api;
9
10    /**
11     * Constructor
12     */
13    public function __construct() {
14        $this->api = new RemoteAPI();
15    }
16
17    /**
18     * Get all available methods with remote access.
19     *
20     * By default it exports all public methods of a remote plugin. Methods beginning
21     * with an underscore are skipped.
22     *
23     * @return array Information about all provided methods. {@see RemoteAPI}.
24     * @throws ReflectionException
25     */
26    public function _getMethods() {
27        $result = array();
28
29        $reflection = new \ReflectionClass($this);
30        foreach($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
31            // skip parent methods, only methods further down are exported
32            $declaredin = $method->getDeclaringClass()->name;
33            if($declaredin == 'DokuWiki_Plugin' || $declaredin == 'DokuWiki_Remote_Plugin') continue;
34            $method_name = $method->name;
35            if(substr($method_name, 0, 1) == '_') continue;
36
37            // strip asterisks
38            $doc = $method->getDocComment();
39            $doc = preg_replace(
40                array('/^[ \t]*\/\*+[ \t]*/m', '/[ \t]*\*+[ \t]*/m', '/\*+\/\s*$/m','/\s*\/\s*$/m'),
41                array('', '', '', ''),
42                $doc
43            );
44
45            // prepare data
46            $data = array();
47            $data['name'] = $method_name;
48            $data['public'] = 0;
49            $data['doc'] = $doc;
50            $data['args'] = array();
51
52            // get parameter type from doc block type hint
53            foreach($method->getParameters() as $parameter) {
54                $name = $parameter->name;
55                $type = 'string'; // we default to string
56                if(preg_match('/^@param[ \t]+([\w|\[\]]+)[ \t]\$'.$name.'/m', $doc, $m)){
57                    $type = $this->cleanTypeHint($m[1]);
58                }
59                $data['args'][] = $type;
60            }
61
62            // get return type from doc block type hint
63            if(preg_match('/^@return[ \t]+([\w|\[\]]+)/m', $doc, $m)){
64                $data['return'] = $this->cleanTypeHint($m[1]);
65            } else {
66                $data['return'] = 'string';
67            }
68
69            // add to result
70            $result[$method_name] = $data;
71        }
72
73        return $result;
74    }
75
76    /**
77     * Matches the given type hint against the valid options for the remote API
78     *
79     * @param string $hint
80     * @return string
81     */
82    protected function cleanTypeHint($hint) {
83        $types = explode('|', $hint);
84        foreach($types as $t) {
85            if(substr($t, -2) == '[]') {
86                return 'array';
87            }
88            if($t == 'boolean') {
89                return 'bool';
90            }
91            if(in_array($t, array('array', 'string', 'int', 'double', 'bool', 'null', 'date', 'file'))) {
92                return $t;
93            }
94        }
95        return 'string';
96    }
97
98    /**
99     * @return RemoteAPI
100     */
101    protected function getApi() {
102        return $this->api;
103    }
104
105}
106