Skip to content

Commit

Permalink
Merge pull request #4 from uniondrug/1.x
Browse files Browse the repository at this point in the history
debug
  • Loading branch information
fuyibing authored May 14, 2018
2 parents b3e9d33 + d1ee3c9 commit 967a76e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
27 changes: 25 additions & 2 deletions src/Parsers/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Annotation extends Base
* @var Validator
*/
public $validator = null;
public $mock = '';
/**
* 是否为数组类型
* @var bool
Expand All @@ -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";
Expand Down Expand Up @@ -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
);
}
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Parsers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Parsers/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
18 changes: 14 additions & 4 deletions src/Parsers/Parameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -240,7 +249,8 @@ protected function withDesc($property)
* @param Property $property
* @return mixed
*/
protected function withValue($property){
protected function withValue($property)
{
return $property->value;
}
}
1 change: 1 addition & 0 deletions src/Parsers/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 967a76e

Please sign in to comment.