Apply rector fixes to inc/Extension and inc/Debug
[dokuwiki.git] / inc / Extension / RemotePlugin.php
blobfd67110443e97f3fcf64ecafeb9a573948157c0b
1 <?php
3 namespace dokuwiki\Extension;
5 use dokuwiki\Remote\Api;
6 use ReflectionException;
7 use ReflectionMethod;
9 /**
10 * Remote Plugin prototype
12 * Add functionality to the remote API in a plugin
14 abstract class RemotePlugin extends Plugin
17 private Api $api;
19 /**
20 * Constructor
22 public function __construct()
24 $this->api = new Api();
27 /**
28 * Get all available methods with remote access.
30 * By default it exports all public methods of a remote plugin. Methods beginning
31 * with an underscore are skipped.
33 * @return array Information about all provided methods. {@see dokuwiki\Remote\RemoteAPI}.
34 * @throws ReflectionException
36 public function _getMethods()
38 $result = [];
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;
47 $method_name = $method->name;
48 if (strpos($method_name, '_') === 0) {
49 continue;
52 // strip asterisks
53 $doc = $method->getDocComment();
54 $doc = preg_replace(
55 ['/^[ \t]*\/\*+[ \t]*/m', '/[ \t]*\*+[ \t]*/m', '/\*+\/\s*$/m', '/\s*\/\s*$/m'],
56 ['', '', '', ''],
57 $doc
60 // prepare data
61 $data = [];
62 $data['name'] = $method_name;
63 $data['public'] = 0;
64 $data['doc'] = $doc;
65 $data['args'] = [];
67 // get parameter type from doc block type hint
68 foreach ($method->getParameters() as $parameter) {
69 $name = $parameter->name;
70 $type = 'string'; // we default to string
71 if (preg_match('/^@param[ \t]+([\w|\[\]]+)[ \t]\$' . $name . '/m', $doc, $m)) {
72 $type = $this->cleanTypeHint($m[1]);
74 $data['args'][] = $type;
77 // get return type from doc block type hint
78 if (preg_match('/^@return[ \t]+([\w|\[\]]+)/m', $doc, $m)) {
79 $data['return'] = $this->cleanTypeHint($m[1]);
80 } else {
81 $data['return'] = 'string';
84 // add to result
85 $result[$method_name] = $data;
88 return $result;
91 /**
92 * Matches the given type hint against the valid options for the remote API
94 * @param string $hint
95 * @return string
97 protected function cleanTypeHint($hint)
99 $types = explode('|', $hint);
100 foreach ($types as $t) {
101 if (substr($t, -2) === '[]') {
102 return 'array';
104 if ($t === 'boolean') {
105 return 'bool';
107 if (in_array($t, ['array', 'string', 'int', 'double', 'bool', 'null', 'date', 'file'])) {
108 return $t;
111 return 'string';
115 * @return Api
117 protected function getApi()
119 return $this->api;