private function createRequestDoc($api)
{
//TODO: 需要处理特殊情况: 输入被绑定在多个参数, 或者输入的不同重叠区域被绑定到不同参数时
$docs = '';
// 提取参数
$params = new JsonStore(array());
foreach ($api['params'] as $name => $param) {
$ori = $params->get($param['value']);
if (count($ori) !== 0) {
// 现在不支持同一个变量多个地方引用
continue;
}
$info = new \ArrayObject(array($name, $param));
$params->set($param['value'], $info);
}
$params = $params->toArray();
// 路由中指定的路径
$route_path = HttpRouterEntries::stringToPath($api['uri'][1]);
// 这是绝对路径
$path = $api['uri'][0];
// 路径拼到示例中
if (isset($params['path'])) {
$req_path = $params['path'];
// 请求中使用的路径, 这是相对路径
$offest = count(HttpRouterEntries::stringToPath($api['root']));
// 相对于绝对路径的偏移
if (is_array($req_path)) {
// 参数只是路径的一部分
if (count($req_path) > 0) {
$end = max(array_keys($req_path));
Verify::isTrue($end < 128, "too long path with length {$end}");
for ($i = 0; $i <= $end; $i++) {
if (isset($req_path[$i])) {
list($arg_name, $arg_info) = $req_path[$i];
if (isset($route_path[$i + $offest]) && $route_path[$i + $offest] !== '*') {
//忽略固定的路径
} else {
$route_path[$i + $offest] = "[{$arg_name}]";
$docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n";
}
} else {
if (!isset($route_path[$i + $offest])) {
$route_path[$i + $offest] = '*';
}
}
}
}
} else {
// 参数整个路径
list($arg_name, $arg_info) = $req_path;
$route_path[$offest] = "[{$arg_name}]";
$docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n";
}
unset($params['path']);
}
$path .= ' /';
$path .= implode('/', $route_path);
// querystring
if (isset($params['_GET'])) {
$get = $params['_GET'];
if (is_array($get)) {
$first = true;
foreach ($get as $name => $value) {
list($arg_name, $arg_info) = $value;
if ($first) {
$path = $path . '?';
$first = false;
} else {
$path = $path . '&';
}
$path = "{$path}{$name}=[{$arg_name}]";
$docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n";
}
} else {
// 参数整个_GET
list($arg_name, $arg_info) = $get;
$path = "{$path}?[{$arg_name}]";
$docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n";
}
unset($params['_GET']);
}
$path .= " HTTP/1.1\r\n";
// header
$header = '';
if (isset($params['header'])) {
$headers = $params['header'];
$first = true;
foreach ($headers as $header_name => $value) {
//if (substr_compare($name, 'HTTP_X_', 0, 7) !== 0) {
// continue;
//}
//$words = explode('_', substr($name, 7));
//$header_name = '';
//foreach ($words as $k => $word) {
// $words[$k] = ucwords(strtolower($word));
//}
//$header_name = implode('-', $words);
list($arg_name, $arg_info) = $value;
$header = "{$header}{$header_name}: [{$arg_name}]\r\n";
$docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n";
unset($params['_SERVER'][$name]);
}
}
// cookie
$header = '';
if (isset($params['_COOKIE'])) {
$cookies = $params['_COOKIE'];
$first = true;
$header = $header . "Cookie: ";
foreach ($cookies as $cookie_name => $value) {
list($arg_name, $arg_info) = $value;
$header = "{$header}{$cookie_name}=[{$arg_name}];";
$docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n";
}
$header .= "\r\n";
}
// body
$body = '';
if (isset($params['_POST'])) {
$post = $params['_POST'];
$first = true;
if (is_array($post)) {
foreach ($post as $name => $value) {
list($arg_name, $arg_info) = $value;
if ($first) {
$first = false;
} else {
$body = $body . '&';
}
$body = "{$body}{$name}=[{$arg_name}]";
$docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n";
}
} else {
// 参数整个_POST
list($arg_name, $arg_info) = $post;
$body = "{$body}[{$arg_name}]";
$docs = "{$docs}{$arg_name}:\r\n {$arg_info['doc']}\r\n\r\n";
}
unset($params['_POST']);
}
if (isset($params['_FILES'])) {
$files = $params['_FILES'];
if (is_array($files)) {
foreach ($files as $name => $value) {
//TODO: 这里假设只有一个文件上传
list($arg_name, $arg_info) = $this->searchArgInfo($value);
$docs = "{$docs}{$name}:\r\n {$arg_info['doc']}\r\n\r\n";
}
}
unset($params['_POST']);
}
$sample = $path . $header . "\r\n" . $body;
return array($sample, $docs);
}