作者 lackoxygen

feat

@@ -3,19 +3,20 @@ @@ -3,19 +3,20 @@
3 namespace Lackoxygen\ShowDocGeneration\Annotations; 3 namespace Lackoxygen\ShowDocGeneration\Annotations;
4 4
5 use Doctrine\Common\Annotations\Annotation\Required; 5 use Doctrine\Common\Annotations\Annotation\Required;
  6 +use Lackoxygen\ShowDocGeneration\Contracts\JsonPasterInterface;
6 7
7 /** 8 /**
8 * @Annotation 9 * @Annotation
9 * @Target({"METHOD"}) 10 * @Target({"METHOD"})
10 * @Attributes({ 11 * @Attributes({
11 -@Attribute("value", type = "string"), 12 +@Attribute("resource", type = "string"),
12 * }) 13 * })
13 */ 14 */
14 -final class Resp extends Annotation 15 +final class RespResource extends Annotation
15 { 16 {
16 /** 17 /**
17 * @Required() 18 * @Required()
18 * @var string 19 * @var string
19 */ 20 */
20 - protected string $value = '{}'; 21 + protected string $resource = '';
21 } 22 }
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace Lackoxygen\ShowDocGeneration\Writer; 3 namespace Lackoxygen\ShowDocGeneration\Writer;
4 4
  5 +use Illuminate\Filesystem\Filesystem;
5 use Illuminate\Support\Arr; 6 use Illuminate\Support\Arr;
6 use Lackoxygen\ShowDocGeneration\Annotations\Method; 7 use Lackoxygen\ShowDocGeneration\Annotations\Method;
7 use Lackoxygen\ShowDocGeneration\Parser\Doc; 8 use Lackoxygen\ShowDocGeneration\Parser\Doc;
@@ -51,7 +52,7 @@ t; @@ -51,7 +52,7 @@ t;
51 52
52 protected string $output = ''; 53 protected string $output = '';
53 54
54 - public function __construct(Doc $doc) 55 + public function __construct(Doc $doc, string $resourcePath)
55 { 56 {
56 $editTemplate = $this->template; 57 $editTemplate = $this->template;
57 58
@@ -61,12 +62,30 @@ t; @@ -61,12 +62,30 @@ t;
61 'method' => Arr::get($doc->method, 'method', Method::METHOD_GET), 62 'method' => Arr::get($doc->method, 'method', Method::METHOD_GET),
62 'headers' => join("\n", $this->tableTbody($doc->headerLine)), 63 'headers' => join("\n", $this->tableTbody($doc->headerLine)),
63 'params' => join("\n", $this->tableTbody($doc->paramLine)), 64 'params' => join("\n", $this->tableTbody($doc->paramLine)),
64 - 'resp' => Arr::get($doc->resp, 'value', '{}'), 65 + 'resp' => '{}',
65 'respRemark' => join("\n", $this->tableTbody($doc->respLine)), 66 'respRemark' => join("\n", $this->tableTbody($doc->respLine)),
66 'middles' => join("\n", $this->unorderedList($doc->middles)), 67 'middles' => join("\n", $this->unorderedList($doc->middles)),
67 'updateTime' => now()->toString() 68 'updateTime' => now()->toString()
68 ]; 69 ];
69 70
  71 + $respResource = Arr::get($doc->respResource, 'resource');
  72 +
  73 + if ($respResource) {
  74 + $resourceInfo = pathinfo($respResource);
  75 +
  76 + if (!isset($resourceInfo['extension'])) {
  77 + $respResource .= '.json';
  78 + }
  79 +
  80 + $resourceFile = rtrim($resourcePath, '/\\') . DIRECTORY_SEPARATOR . $respResource;
  81 +
  82 + $fs = new Filesystem();
  83 +
  84 + if ($fs->exists($resourceFile) && $respData = $fs->sharedGet($resourceFile)) {
  85 + Arr::set($replaces, 'resp', $respData);
  86 + }
  87 + }
  88 +
70 $this->output = \str_replace( 89 $this->output = \str_replace(
71 \array_map(function (string $key) { 90 \array_map(function (string $key) {
72 return '{{' . $key . '}}'; 91 return '{{' . $key . '}}';