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