RemotePlugin.php 3.3 KB

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