You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.7 KiB
PHP

<?php
namespace Bsr\Webservice;
use Bsr\Webservice\Exception\WebException;
use Bsr\Webservice\Formatter\Html;
class Help {
private static function func($ws, $func) {
try {
$rm = new \ReflectionMethod($ws, $func);
} catch(\ReflectionException $e) {
return '';
}
$doc = $rm->getDocComment();
$params = $rm->getParameters();
preg_match_all('/@param\s+(?P<type>[^\s]*?)\s*\$(?P<name>[^\s]+?)\s+(?P<doc>[\w\s]*)/', $doc, $parametersDoc, PREG_SET_ORDER);
preg_match('/@return\s+(?P<type>[^\s]*?)\s+(?P<doc>[\w\s]*)/', $doc, $returnDoc);
$doc = array_filter(array_map(function($l) {
$l = trim($l, " \t\n\r\0\x0B");
if(strpos($l, '/**') === 0 || strpos($l, '*/') === 0) {
$l = '';
}
$l = trim($l, "* ");
if(strpos($l, '@') === 0) {
$l = '';
}
return $l;
}, explode("\n", $doc)));
$doc = nl2br(implode("\n", $doc));
$paramsHtml = '';
foreach($params as $p) {
foreach($parametersDoc as $d) {
if(isset($d['name']) && $p->name == $d['name']) {
$paramsHtml .= Html::template(array(
'name' => $p->name,
'optional' => $p->isDefaultValueAvailable() ? '(optional)' : '',
'type' => isset($d['type']) ? $d['type'] : '',
'doc' => isset($d['doc']) ? $d['doc'] : '',
), 'param_help');
}
}
}
return Html::template(array(
'func' => $func,
'help' => $doc,
'parameters' => $paramsHtml,
'return' => Html::template(array(
'type' => isset($returnDoc['type']) ? $returnDoc['type'] : '',
'doc' => isset($returnDoc['doc']) ? $returnDoc['doc'] : '',
), 'return_help'),
), 'func_help');
}
public static function content(WebService $ws) {
$rc = new \ReflectionClass($ws);
$methods = array_filter(array_map(function(\ReflectionMethod $m) {
if($m->getName() == 'Run') {
// this is a method from WebService directly and is of not interests for the help
return '';
}
return $m->getName();
}, $rc->getMethods(\ReflectionMethod::IS_PUBLIC)));
$html = '';
foreach($methods as $m) {
$html .= static::func($ws, $m);
}
return $html;
}
public static function exception(WebException $e, WebService $ws, $func) {
return static::func($ws, $func);
}
}