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