| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
/** |
| 4 |
* @license Apache 2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace OpenApi\Processors; |
| 8 |
|
| 9 |
use OpenApi\Analysis; |
| 10 |
use OpenApi\Annotations as OA; |
| 11 |
use OpenApi\Context; |
| 12 |
use OpenApi\Generator; |
| 13 |
|
| 14 |
/** |
| 15 |
* Merge reusable annotation into <code>@OA\Schemas</code>. |
| 16 |
*/ |
| 17 |
class MergeIntoComponents |
| 18 |
{ |
| 19 |
public function __invoke(Analysis $analysis): void |
| 20 |
{ |
| 21 |
$components = $analysis->openapi->components; |
| 22 |
if (Generator::isDefault($components)) { |
| 23 |
$components = new OA\Components(['_context' => new Context(['generated' => true], $analysis->context)]); |
| 24 |
} |
| 25 |
|
| 26 |
/** @var OA\AbstractAnnotation $annotation */ |
| 27 |
foreach ($analysis->annotations as $annotation) { |
| 28 |
if ($annotation instanceof OA\AbstractAnnotation |
| 29 |
&& in_array(OA\Components::class, $annotation::$_parents) |
| 30 |
&& false === $annotation->_context->is('nested')) { |
| 31 |
// A top level annotation. |
| 32 |
$analysis->mergeAnnotations($components, [$annotation], true); |
| 33 |
$analysis->openapi->components = $components; |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
} |