Skip to content

Commit

Permalink
增加 @ignore & @SDK 特性, 同时控制台提示
Browse files Browse the repository at this point in the history
  • Loading branch information
fuyibing committed Oct 25, 2018
1 parent 6de7764 commit 55e24a3
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 35 deletions.
7 changes: 5 additions & 2 deletions src/Parsers/Abstracts/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ abstract class Base
public $schema = '{{protocol}}';
public $domain = '{{domain}}';
public $token = '{{token}}';
public $console;

public function __construct()
{
$this->console = new Console();
}

/**
Expand All @@ -39,13 +41,14 @@ public function saveMarkdown($path, $name, $contents)
$file = $path.'/'.$name;
try {
if (!is_dir($path)) {
mkdir($path, 0777,true);
mkdir($path, 0777, true);
}
$fp = fopen($file, 'wb+');
fwrite($fp, $contents);
fclose($fp);
$this->console->debug("导出到%s文件", $file);
} catch(\Exception $e) {
echo $e->getMessage();
$this->console->error($e->getMessage());
exit;
}
}
Expand Down
40 changes: 40 additions & 0 deletions src/Parsers/Abstracts/Console.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* @author wsfuyibing <websearch@163.com>
* @date 2018-05-09
*/
namespace Uniondrug\Postman\Parsers\Abstracts;

/**
* 控制台消息
* @package Uniondrug\Postman\Parsers\Abstracts
*/
class Console
{
public function debug($format, ... $args)
{
$this->printer("DEBUG", 30, 47, $format, ... $args);
}

public function error($format, ... $args)
{
$this->printer("ERROR", 33, 41, $format, ... $args);
}

public function info($format, ... $args)
{
$this->printer("INFO", 0, 0, $format, ... $args);
}

public function warning($format, ... $args)
{
$this->printer("WARNING", 31, 43, $format, ... $args);
}

private function printer($level, $bc, $fc, $format, ... $args)
{
$args = is_array($args) ? $args : [];
$message = call_user_func_array('sprintf', array_merge([$format], $args));
echo sprintf("\033[%d;%dm[%s] %s\033[0m\n", $bc, $fc, sprintf("%5s", $level), $message);
}
}
53 changes: 53 additions & 0 deletions src/Parsers/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ class Annotation extends Base
*/
public $validator = null;
public $mock = '';
public $ver = '';
/**
* 是否为数组类型
* @var bool
*/
public $isArrayType = false;
public $isStructType = false;
public $isIgnored = false;
public $isSdk = false;
/**
* @var bool|string
*/
Expand All @@ -86,8 +89,11 @@ class Annotation extends Base
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 $regexpSdk = "/@(sdk)[^a-z]*/i";
private static $regexpIgnore = "/@(ignore)[^a-z]*/i";
private static $regexpType = "/@var[ ]+([_a-z0-9\\\]+)[ ]*([\[|\]| ]*)([^\n]*)\n/i";
private static $regexpValidator = "/@validator[ ]*\(([^\)]*)\)/i";
private static $regexpVersion = "/@version[ ]+([^\n]+)\n/i";

/**
* Annotation constructor.
Expand Down Expand Up @@ -124,6 +130,21 @@ public function alias()
}
}

public function ignored()
{
// 1. no comment
if ($this->comment === false) {
return;
}
// 2. not defined
preg_match(self::$regexpIgnore, $this->comment, $m);
if (count($m) === 0) {
return;
}
// 3. export
$this->isIgnored = true;
}

/**
* 解析name/description
*/
Expand Down Expand Up @@ -231,6 +252,23 @@ public function requeset()
]);
}

/**
* 测试单元
*/
public function sdk()
{
if ($this->comment === false) {
return;
}
// 2. not defined
preg_match(self::$regexpSdk, $this->comment, $m);
if (count($m) === 0) {
return;
}
// 3. export
$this->isSdk = true;
}

/**
* 测试单元
*/
Expand Down Expand Up @@ -305,6 +343,21 @@ public function mock()
$this->mock = trim($m[1]);
}

public function version()
{
// 1. not comment
if ($this->comment === false) {
return;
}
// 2. not defined
preg_match(self::$regexpVersion, $this->comment, $m);
if (count($m) === 0) {
return;
}
// 3. parse
$this->ver = trim($m[1]);
}

/**
* @param string $s
* @return array
Expand Down
19 changes: 13 additions & 6 deletions src/Parsers/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Collection extends Base
* @var string
*/
public $name = '';
public $prefix = '';
/**
* 描述
* @var string
Expand Down Expand Up @@ -55,14 +56,12 @@ class Collection extends Base

/**
* Controller constructor.
*
* @param string $path 项目路径
*/
public function __construct(string $path)
{
parent::__construct();
$this->basePath = $path;
$this->name = $path;
// 1. load config
$file = $path.'/postman.json';
if (file_exists($file)) {
Expand All @@ -72,10 +71,15 @@ public function __construct(string $path)
isset($json->name) && $this->name = $json->name;
isset($json->description) && $this->description = $json->description;
isset($json->host) && $this->host = $json->host;
isset($json->name) && $this->auth = strtoupper($json->auth) === 'YES';
isset($json->auth) && $this->auth = strtoupper($json->auth) === 'YES';
}
}
// 2. 遍历目录
// 2. console
$this->console->info("{$json->name}, {$json->description}");
$this->console->info("需要鉴权: {$json->auth}");
$this->console->info("域名前缀: {$json->host}");
$this->console->info("扫描目录: %s", $this->controllerPath);
// 3. 遍历目录
$this->scanner($path.'/'.$this->controllerPath);
}

Expand All @@ -91,6 +95,7 @@ public function parser()
$controller->parser();
$this->controllers[$class] = $controller;
} catch(\Exception $e) {
$this->console->error($e->getMessage());
}
}
}
Expand Down Expand Up @@ -125,7 +130,10 @@ public function toMarkdown()
$text .= $this->eol;
$text .= '### 接口目录'.$this->eol;
foreach ($this->controllers as $controller) {
$name = $controller->annotation->name;
if (count($controller->methods) === 0) {
continue;
}
$name = trim($controller->annotation->name);
$desc = preg_replace("/\n/", "", trim($controller->annotation->description));
$url = str_replace('\\', '/', substr($controller->reflect->getName(), 16));
$text .= '* ['.$name.'](./'.$url.'/README.md) : '.$desc.$this->crlf;
Expand Down Expand Up @@ -169,7 +177,6 @@ public function toPostman()

/**
* 扫描Controller目录
*
* @param string $path
*/
private function scanner($path)
Expand Down
30 changes: 23 additions & 7 deletions src/Parsers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function __construct(Collection $collection, string $class)
$this->collection = $collection;
$this->reflect = new \ReflectionClass($class);
$this->filename = substr($this->reflect->getFileName(), strlen($collection->basePath) + 1);
$this->console->info("发现%s控制器", $this->reflect->name);
}

/**
Expand Down Expand Up @@ -68,9 +69,13 @@ public function parser()
continue;
}
// 2.3 execute
$method = new Method($this->collection, $this, $reflect);
$method->parser();
$this->methods[] = $method;
try {
$method = new Method($this->collection, $this, $reflect);
$method->parser();
$this->methods[] = $method;
} catch(\Exception $e) {
$this->console->error($e->getMessage());
}
}
}

Expand All @@ -86,9 +91,15 @@ public function getIndex($curr = false)
$url = str_replace('\\', '/', substr($this->reflect->getName(), 16));
$prefix = './'.($curr ? '' : $url.'/');
foreach ($this->methods as $method) {
$name = $method->annotation->name;
$desc = preg_replace("/\n/", " ", trim($method->annotation->description));
$text .= $comma.$space.'* ['.$name.']('.$prefix.$method->reflect->getShortName().'.md) : '.$desc;
$name = trim($method->annotation->name);
$desc = trim(preg_replace("/\n/", " ", trim($method->annotation->description)));
$text .= $comma.$space.'* ['.$name.']('.$prefix.$method->reflect->getShortName().'.md)';
if ($method->annotation->ver !== '') {
$text .= " `{$method->annotation->ver}` ";
}
if ($desc !== '') {
$text .= ' : '.$desc;
}
$comma = $this->crlf;
}
return $text;
Expand All @@ -102,6 +113,11 @@ public function toMarkdown()
{
$name = str_replace('\\', '/', substr($this->reflect->getName(), 16));
$path = $this->collection->basePath.'/'.$this->collection->publishTo.'/'.$name;
$count = count($this->methods);
if ($count === 0) {
$this->console->warning("控制器{$this->reflect->getName()}无可导出动作, 忽略导出");
return;
}
// 1. title
$text = '# '.$this->annotation->name;
// 2. description
Expand All @@ -111,7 +127,7 @@ public function toMarkdown()
}
// 3. information
$text .= $this->eol;
$text .= "* **接口** : `".count($this->methods)."` 个".$this->crlf;
$text .= "* **接口** : `".$count."` 个".$this->crlf;
$text .= "* **前缀** : `{$this->annotation->prefix}`".$this->crlf;
$text .= "* **类名** : `{$this->reflect->name}`".$this->crlf;
$text .= "* **文件** : `{$this->filename}`";
Expand Down
Loading

0 comments on commit 55e24a3

Please sign in to comment.