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 */ 25 public function _getMethods() { 26 $result = array(); 27 28 $reflection = new \ReflectionClass($this); 29 foreach($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { 30 // skip parent methods, only methods further down are exported 31 $declaredin = $method->getDeclaringClass()->name; 32 if($declaredin == 'DokuWiki_Plugin' || $declaredin == 'DokuWiki_Remote_Plugin') continue; 33 $method_name = $method->name; 34 if(substr($method_name, 0, 1) == '_') continue; 35 36 // strip asterisks 37 $doc = $method->getDocComment(); 38 $doc = preg_replace( 39 array('/^[ \t]*\/\*+[ \t]*/m', '/[ \t]*\*+[ \t]*/m', '/\*+\/\s*$/m','/\s*\/\s*$/m'), 40 array('', '', '', ''), 41 $doc 42 ); 43 44 // prepare data 45 $data = array(); 46 $data['name'] = $method_name; 47 $data['public'] = 0; 48 $data['doc'] = $doc; 49 $data['args'] = array(); 50 51 // get parameter type from doc block type hint 52 foreach($method->getParameters() as $parameter) { 53 $name = $parameter->name; 54 $type = 'string'; // we default to string 55 if(preg_match('/^@param[ \t]+([\w|\[\]]+)[ \t]\$'.$name.'/m', $doc, $m)){ 56 $type = $this->cleanTypeHint($m[1]); 57 } 58 $data['args'][] = $type; 59 } 60 61 // get return type from doc block type hint 62 if(preg_match('/^@return[ \t]+([\w|\[\]]+)/m', $doc, $m)){ 63 $data['return'] = $this->cleanTypeHint($m[1]); 64 } else { 65 $data['return'] = 'string'; 66 } 67 68 // add to result 69 $result[$method_name] = $data; 70 } 71 72 return $result; 73 } 74 75 /** 76 * Matches the given type hint against the valid options for the remote API 77 * 78 * @param string $hint 79 * @return string 80 */ 81 protected function cleanTypeHint($hint) { 82 $types = explode('|', $hint); 83 foreach($types as $t) { 84 if(substr($t, -2) == '[]') { 85 return 'array'; 86 } 87 if($t == 'boolean') { 88 return 'bool'; 89 } 90 if(in_array($t, array('array', 'string', 'int', 'double', 'bool', 'null', 'date', 'file'))) { 91 return $t; 92 } 93 } 94 return 'string'; 95 } 96 97 /** 98 * @return RemoteAPI 99 */ 100 protected function getApi() { 101 return $this->api; 102 } 103 104} 105