diff --git a/src/Parsers/Annotation.php b/src/Parsers/Annotation.php index c58dfb6..1648f5a 100644 --- a/src/Parsers/Annotation.php +++ b/src/Parsers/Annotation.php @@ -59,6 +59,7 @@ class Annotation extends Base * @var Validator */ public $validator = null; + public $mock = ''; /** * 是否为数组类型 * @var bool @@ -78,6 +79,7 @@ class Annotation extends Base ]; private static $regexpInput = "/@Input[ ]+(\S+)/i"; private static $regexpOutput = "/@Output[ ]+(\S+)/i"; + private static $regexpMock = "/@Mock[ ]+([^\n]+)\n/i"; private static $regexpPrefix = "/@RoutePrefix[ ]*\(([^\)]*)\)/i"; private static $regexpRequest = "/@(Route|Get|Post|Put|Patch|Delete|Options|head)[ ]*\(([^\)]*)\)/i"; private static $regexpType = "/@var[ ]+([_a-z0-9\\\]+)[ ]*([\[|\]| ]*)([^\n]*)\n/i"; @@ -118,6 +120,10 @@ public function info() $this->typeText = $this->name; if (count($info) > 0) { $this->description = implode($this->crlf, $info); + $this->description = preg_replace("/\<[\/]?code\>/i", + '```', + $this->description + ); } } } @@ -256,6 +262,24 @@ public function validator() $this->validator = new Validator($m[1]); } + /** + * 解析Mock数据 + */ + public function mock() + { + // 1. not comment + if ($this->comment === false) { + return; + } + // 2. not defined + preg_match(self::$regexpMock, $this->comment, $m); + if (count($m) === 0) { + return; + } + // 3. parse + $this->mock = trim($m[1]); + } + /** * @param string $s * @return array @@ -267,8 +291,7 @@ private function formatInfo($s) } $a = []; foreach (explode("\n", $s) as $x) { - $x = trim($x); - if ($x === '') { + if (trim($x) === '') { continue; } $a[] = $x; diff --git a/src/Parsers/Controller.php b/src/Parsers/Controller.php index f6ca235..c123110 100644 --- a/src/Parsers/Controller.php +++ b/src/Parsers/Controller.php @@ -105,7 +105,7 @@ public function toMarkdown() // 2. description $desc = $this->annotation->description; if ($desc !== '') { - $text .= $this->eol.$desc; + $text .= $this->eol.'> '.$desc; } // 3. information $text .= $this->eol; @@ -126,7 +126,7 @@ public function toMarkdown() // 6. save README.md $this->saveMarkdown($path, 'README.md', $text); // 7. 触发API - foreach ($this->methods as $method){ + foreach ($this->methods as $method) { $method->toMarkdown(); } } diff --git a/src/Parsers/Method.php b/src/Parsers/Method.php index 5799bea..2a55d4c 100644 --- a/src/Parsers/Method.php +++ b/src/Parsers/Method.php @@ -151,10 +151,10 @@ public function toPostmanRequest() } // url $data['url'] = [ - 'raw' => $this->schema.'://'.$this->collection->host.'.'.$this->domain.$this->annotation->path, + 'raw' => $this->schema.'://'.$this->collection->host.'.'.$this->domain.$this->controller->annotation->prefix.$this->annotation->path, 'protocol' => $this->schema, 'host' => explode('.', $this->collection->host.'.'.$this->domain), - 'path' => explode('/', substr($this->annotation->path, 1)) + 'path' => explode('/', substr($this->controller->annotation->prefix.$this->annotation->path, 1)) ]; // post body if ($this->annotation->isPostMethod) { diff --git a/src/Parsers/Parameters.php b/src/Parsers/Parameters.php index 91b619a..91ef971 100644 --- a/src/Parsers/Parameters.php +++ b/src/Parsers/Parameters.php @@ -85,17 +85,26 @@ public function __construct(Method $method, $sn, $ns) } /** + * @param boolean $mock 是否填充MOCK数据 * @return array */ - public function toArray() + public function toArray($mock = true) { $data = []; foreach ($this->properties as $property) { if ($property->annotation->isStructType) { $p = $this->children[$property->name]; - $data[$property->name] = $p->toArray(); + if ($property->annotation->isArrayType) { + $data[$property->name] = [$p->toArray($mock)]; + } else { + $data[$property->name] = $p->toArray($mock); + } } else { - $data[$property->name] = $property->annotation->isArrayType ? [$property->defaultValue()] : $property->defaultValue(); + $value = $property->annotation->mock; + if ($value === '') { + $value = $property->defaultValue(); + } + $data[$property->name] = $property->annotation->isArrayType ? [$value] : $value; } } return $this->isArray ? [$data] : $data; @@ -240,7 +249,8 @@ protected function withDesc($property) * @param Property $property * @return mixed */ - protected function withValue($property){ + protected function withValue($property) + { return $property->value; } } diff --git a/src/Parsers/Property.php b/src/Parsers/Property.php index 51804c6..3a75635 100644 --- a/src/Parsers/Property.php +++ b/src/Parsers/Property.php @@ -30,6 +30,7 @@ public function __construct(Method $method, StructInterface $struct, \Reflection $this->annotation->info(); $this->annotation->type(); $this->annotation->validator(); + $this->annotation->mock(); // 2. value if (!$this->annotation->isStructType) { if ($this->annotation->isArrayType) {