forked from zircote/swagger-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocBlockAnnotationFactory.php
More file actions
77 lines (61 loc) · 2.22 KB
/
DocBlockAnnotationFactory.php
File metadata and controls
77 lines (61 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php declare(strict_types=1);
/**
* @license Apache 2.0
*/
namespace OpenApi\Analysers;
use OpenApi\Annotations as OA;
use OpenApi\Context;
use OpenApi\Generator;
use OpenApi\GeneratorAwareTrait;
class DocBlockAnnotationFactory implements AnnotationFactoryInterface
{
use GeneratorAwareTrait;
protected ?DocBlockParser $docBlockParser = null;
public function __construct(?DocBlockParser $docBlockParser = null)
{
$this->docBlockParser = $docBlockParser ?: new DocBlockParser();
}
public function isSupported(): bool
{
return DocBlockParser::isEnabled();
}
public function setGenerator(Generator $generator)
{
$this->generator = $generator;
$this->docBlockParser->setAliases($generator->getAliases());
return $this;
}
public function build(\Reflector $reflector, Context $context): array
{
$aliases = $this->generator ? $this->generator->getAliases() : [];
if (method_exists($reflector, 'getShortName') && method_exists($reflector, 'getName')) {
$aliases[strtolower($reflector->getShortName())] = $reflector->getName();
}
if ($context->with('scanned')) {
$details = $context->scanned;
foreach ($details['uses'] as $alias => $name) {
$aliasKey = strtolower($alias);
if ($name != $alias && !array_key_exists($aliasKey, $aliases)) {
// real aliases only
$aliases[strtolower($alias)] = $name;
}
}
}
$this->docBlockParser->setAliases($aliases);
if (method_exists($reflector, 'getDocComment') && ($comment = $reflector->getDocComment())) {
$annotations = [];
foreach ($this->docBlockParser->fromComment($comment, $context) as $instance) {
if ($instance instanceof OA\AbstractAnnotation) {
$annotations[] = $instance;
} else {
if ($context->is('other') === false) {
$context->other = [];
}
$context->other[] = $instance;
}
}
return $annotations;
}
return [];
}
}