1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mpociot\ApiDoc\Extracting\Strategies\Responses; |
4
|
|
|
|
5
|
|
|
use Illuminate\Routing\Route; |
6
|
|
|
use Mpociot\ApiDoc\Extracting\RouteDocBlocker; |
7
|
|
|
use Mpociot\ApiDoc\Extracting\Strategies\Strategy; |
8
|
|
|
use Mpociot\Reflection\DocBlock; |
9
|
|
|
use Mpociot\Reflection\DocBlock\Tag; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Get a response from the docblock ( @response ). |
13
|
|
|
*/ |
14
|
|
|
class UseResponseTag extends Strategy |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param Route $route |
18
|
|
|
* @param \ReflectionClass $controller |
19
|
|
|
* @param \ReflectionMethod $method |
20
|
|
|
* @param array $routeRules |
21
|
|
|
* @param array $context |
22
|
|
|
* |
23
|
|
|
* @throws \Exception |
24
|
|
|
* |
25
|
|
|
* @return array|null |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
public function __invoke(Route $route, \ReflectionClass $controller, \ReflectionMethod $method, array $routeRules, array $context = []) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$docBlocks = RouteDocBlocker::getDocBlocksFromRoute($route); |
30
|
|
|
/** @var DocBlock $methodDocBlock */ |
31
|
|
|
$methodDocBlock = $docBlocks['method']; |
32
|
|
|
|
33
|
|
|
return $this->getDocBlockResponses($methodDocBlock->getTags()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the response from the docblock if available. |
38
|
|
|
* |
39
|
|
|
* @param array $tags |
40
|
|
|
* |
41
|
|
|
* @return array|null |
42
|
|
|
*/ |
43
|
|
|
protected function getDocBlockResponses(array $tags) |
44
|
|
|
{ |
45
|
|
|
$responseTags = array_values( |
46
|
|
|
array_filter($tags, function ($tag) { |
47
|
|
|
return $tag instanceof Tag && strtolower($tag->getName()) === 'response'; |
|
|
|
|
48
|
|
|
}) |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
if (empty($responseTags)) { |
52
|
|
|
return null; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$responses = array_map(function (Tag $responseTag) { |
56
|
|
|
preg_match('/^(\d{3})?\s?([\s\S]*)$/', $responseTag->getContent(), $result); |
57
|
|
|
|
58
|
|
|
$status = $result[1] ?: 200; |
59
|
|
|
$content = $result[2] ?: '{}'; |
60
|
|
|
|
61
|
|
|
return ['content' => $content, 'status' => (int) $status]; |
62
|
|
|
}, $responseTags); |
63
|
|
|
|
64
|
|
|
return $responses; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.