Skip to content

Commit b20ddd4

Browse files
committed
Add support for multiple response/responsefile tags
1 parent b5ddfb0 commit b20ddd4

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

resources/views/partials/route.blade.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,19 @@
6969
```
7070

7171
@if(in_array('GET',$route['methods']) || (isset($route['showresponse']) && $route['showresponse']))
72+
@if(is_array($route['response']))
73+
@foreach($route['response'] as $response)
74+
> Example response ({{$response['status']}}):
75+
76+
```json
77+
@if(is_object($response['content']) || is_array($response['content']))
78+
{!! json_encode($response['content'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) !!}
79+
@else
80+
{!! json_encode(json_decode($response['content']), JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) !!}
81+
@endif
82+
```
83+
@endforeach
84+
@else
7285
> Example response:
7386

7487
```json
@@ -79,6 +92,7 @@
7992
@endif
8093
```
8194
@endif
95+
@endif
8296

8397
### HTTP Request
8498
@foreach($route['methods'] as $method)

src/Tools/ResponseResolver.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Mpociot\ApiDoc\Tools;
44

5+
use Illuminate\Http\JsonResponse;
56
use Illuminate\Routing\Route;
67
use Mpociot\ApiDoc\Tools\ResponseStrategies\ResponseTagStrategy;
78
use Mpociot\ApiDoc\Tools\ResponseStrategies\ResponseCallStrategy;
@@ -10,6 +11,9 @@
1011

1112
class ResponseResolver
1213
{
14+
/**
15+
* @var array
16+
*/
1317
public static $strategies = [
1418
ResponseTagStrategy::class,
1519
TransformerTagsStrategy::class,
@@ -22,23 +26,48 @@ class ResponseResolver
2226
*/
2327
private $route;
2428

29+
/**
30+
* @param Route $route
31+
*/
2532
public function __construct(Route $route)
2633
{
2734
$this->route = $route;
2835
}
2936

37+
/**
38+
* @param array $tags
39+
* @param array $routeProps
40+
*
41+
* @return array
42+
*/
3043
private function resolve(array $tags, array $routeProps)
3144
{
3245
$response = null;
46+
3347
foreach (static::$strategies as $strategy) {
3448
$strategy = new $strategy();
49+
50+
/** @var JsonResponse|array|null $response */
3551
$response = $strategy($this->route, $tags, $routeProps);
52+
3653
if (! is_null($response)) {
37-
return $this->getResponseContent($response);
54+
if (is_array($response)) {
55+
return array_map(function (JsonResponse $response) {
56+
return ['status' => $response->getStatusCode(), 'content' => $this->getResponseContent($response)];
57+
}, $response);
58+
}
59+
60+
return [['status' => $response->getStatusCode(), 'content' => $this->getResponseContent($response)]];
3861
}
3962
}
4063
}
4164

65+
/**
66+
* @param $route
67+
* @param $tags
68+
* @param $routeProps
69+
* @return array
70+
*/
4271
public static function getResponse($route, $tags, $routeProps)
4372
{
4473
return (new static($route))->resolve($tags, $routeProps);

src/Tools/ResponseStrategies/ResponseFileStrategy.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
*/
1111
class ResponseFileStrategy
1212
{
13+
/**
14+
* @param Route $route
15+
* @param array $tags
16+
* @param array $routeProps
17+
*
18+
* @return mixed
19+
*/
1320
public function __invoke(Route $route, array $tags, array $routeProps)
1421
{
1522
return $this->getFileResponse($tags);
@@ -25,15 +32,20 @@ public function __invoke(Route $route, array $tags, array $routeProps)
2532
protected function getFileResponse(array $tags)
2633
{
2734
$responseFileTags = array_filter($tags, function ($tag) {
28-
return $tag instanceof Tag && strtolower($tag->getName()) == 'responsefile';
35+
return $tag instanceof Tag && strtolower($tag->getName()) === 'responsefile';
2936
});
37+
3038
if (empty($responseFileTags)) {
3139
return;
3240
}
33-
$responseFileTag = array_first($responseFileTags);
3441

35-
$json = json_decode(file_get_contents(storage_path($responseFileTag->getContent()), true), true);
42+
return array_map(function ($responseFileTag) {
43+
preg_match('/^(\d{3})?\s?([\s\S]*)$/', $responseFileTag->getContent(), $result);
44+
45+
$status = $result[1] ?: 200;
46+
$content = $result[2] ? file_get_contents(storage_path($result[2]), true) : '{}';
3647

37-
return response()->json($json);
48+
return response()->json(json_decode($content, true), (int) $status);
49+
}, $responseFileTags);
3850
}
3951
}

src/Tools/ResponseStrategies/ResponseTagStrategy.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
*/
1111
class ResponseTagStrategy
1212
{
13+
/**
14+
* @param Route $route
15+
* @param array $tags
16+
* @param array $routeProps
17+
*
18+
* @return mixed
19+
*/
1320
public function __invoke(Route $route, array $tags, array $routeProps)
1421
{
1522
return $this->getDocBlockResponse($tags);
@@ -25,13 +32,20 @@ public function __invoke(Route $route, array $tags, array $routeProps)
2532
protected function getDocBlockResponse(array $tags)
2633
{
2734
$responseTags = array_filter($tags, function ($tag) {
28-
return $tag instanceof Tag && strtolower($tag->getName()) == 'response';
35+
return $tag instanceof Tag && strtolower($tag->getName()) === 'response';
2936
});
37+
3038
if (empty($responseTags)) {
3139
return;
3240
}
33-
$responseTag = array_first($responseTags);
3441

35-
return response()->json(json_decode($responseTag->getContent(), true));
42+
return array_map(function ($responseTag) {
43+
preg_match('/^(\d{3})?\s?([\s\S]*)$/', $responseTag->getContent(), $result);
44+
45+
$status = $result[1] ?: 200;
46+
$content = $result[2] ?: '{}';
47+
48+
return response()->json(json_decode($content, true), (int) $status);
49+
}, $responseTags);
3650
}
3751
}

0 commit comments

Comments
 (0)