1<?php
2
3namespace dokuwiki\Extension;
4
5use dokuwiki\Remote\Api;
6use dokuwiki\Remote\ApiCall;
7use ReflectionException;
8use ReflectionMethod;
9
10/**
11 * Remote Plugin prototype
12 *
13 * Add functionality to the remote API in a plugin
14 */
15abstract class RemotePlugin extends Plugin
16{
17    private Api $api;
18
19    /**
20     * Constructor
21     */
22    public function __construct()
23    {
24        $this->api = new Api();
25    }
26
27    /**
28     * Get all available methods with remote access.
29     *
30     * By default it exports all public methods of a remote plugin. Methods beginning
31     * with an underscore are skipped.
32     *
33     * @return ApiCall[] Information about all provided methods. ('methodname' => ApiCall)
34     * @throws ReflectionException
35     */
36    public function getMethods()
37    {
38        $result = [];
39
40        $reflection = new \ReflectionClass($this);
41        foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
42            // skip parent methods, only methods further down are exported
43            $declaredin = $method->getDeclaringClass()->name;
44            if ($declaredin === 'dokuwiki\Extension\Plugin' || $declaredin === 'dokuwiki\Extension\RemotePlugin') {
45                continue;
46            }
47            $method_name = $method->name;
48            if ($method_name[0] ===  '_') {
49                continue;
50            }
51            if ($method_name === 'getMethods') {
52                continue; // skip self, if overridden
53            }
54
55            // add to result
56            $result[$method_name] = new ApiCall([$this, $method_name], 'plugins');
57        }
58
59        return $result;
60    }
61
62    /**
63     * @deprecated 2023-11-30
64     */
65    public function _getMethods()
66    {
67        dbg_deprecated('getMethods()');
68    }
69
70
71
72    /**
73     * @return Api
74     */
75    protected function getApi()
76    {
77        return $this->api;
78    }
79}
80