| 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 all <code>@OA\OpenApi</code> annotations into one. |
| 16 |
*/ |
| 17 |
class MergeIntoOpenApi |
| 18 |
{ |
| 19 |
protected bool $mergeComponents; |
| 20 |
|
| 21 |
public function __construct(bool $mergeComponents = false) |
| 22 |
{ |
| 23 |
$this->mergeComponents = $mergeComponents; |
| 24 |
} |
| 25 |
|
| 26 |
public function isMergeComponents(): bool |
| 27 |
{ |
| 28 |
return $this->mergeComponents; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* If set to <code>true</code> allow multiple `@OA\Components` annotations to be merged. |
| 33 |
*/ |
| 34 |
public function setMergeComponents(bool $mergeComponents): MergeIntoOpenApi |
| 35 |
{ |
| 36 |
$this->mergeComponents = $mergeComponents; |
| 37 |
|
| 38 |
return $this; |
| 39 |
} |
| 40 |
|
| 41 |
public function __invoke(Analysis $analysis): void |
| 42 |
{ |
| 43 |
// Auto-create the OpenApi annotation. |
| 44 |
if (!$analysis->openapi) { |
| 45 |
$context = new Context(['generated' => true], $analysis->context); |
| 46 |
$analysis->addAnnotation(new OA\OpenApi(['_context' => $context]), $context); |
| 47 |
} |
| 48 |
|
| 49 |
$openapi = $analysis->openapi; |
| 50 |
$openapi->_analysis = $analysis; |
| 51 |
|
| 52 |
// Merge annotations into the target openapi |
| 53 |
$merge = []; |
| 54 |
/** @var OA\AbstractAnnotation $annotation */ |
| 55 |
foreach ($analysis->annotations as $annotation) { |
| 56 |
if ($annotation === $openapi) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
|
| 60 |
if ($annotation instanceof OA\OpenApi) { |
| 61 |
$paths = $annotation->paths; |
| 62 |
unset($annotation->paths); |
| 63 |
$openapi->mergeProperties($annotation); |
| 64 |
if (!Generator::isDefault($paths)) { |
| 65 |
foreach ($paths as $path) { |
| 66 |
if (Generator::isDefault($openapi->paths)) { |
| 67 |
$openapi->paths = []; |
| 68 |
} |
| 69 |
$openapi->paths[] = $path; |
| 70 |
} |
| 71 |
} |
| 72 |
} elseif ($annotation instanceof OA\AbstractAnnotation |
| 73 |
&& in_array(OA\OpenApi::class, $annotation::$_parents) |
| 74 |
&& false === $annotation->_context->is('nested')) { |
| 75 |
// A top-level annotation. |
| 76 |
$merge[] = $annotation; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
if ($this->isMergeComponents()) { |
| 81 |
|
| 82 |
// merge Components |
| 83 |
$componentsList = array_filter($merge, static fn (OA\AbstractAnnotation $annotation): bool => $annotation instanceof OA\Components); |
| 84 |
$firstComponents = $openapi->components; |
| 85 |
|
| 86 |
if ((!Generator::isDefault($firstComponents) && $componentsList !== []) || count($merge) > 1) { |
| 87 |
if (Generator::isDefault($firstComponents)) { |
| 88 |
$firstComponents = array_shift($componentsList); |
| 89 |
} |
| 90 |
|
| 91 |
foreach ($componentsList as $components) { |
| 92 |
foreach (OA\Components::$_nested as $nested) { |
| 93 |
if (2 == count($nested)) { |
| 94 |
$property = $nested[0]; |
| 95 |
if (!Generator::isDefault($components->{$property})) { |
| 96 |
$firstComponents->merge($components->{$property}); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
$analysis->removeAnnotation($components); |
| 102 |
} |
| 103 |
|
| 104 |
$merge = array_filter($merge, static fn (OA\AbstractAnnotation $annotation): bool => !$annotation instanceof OA\Components); |
| 105 |
$merge[] = $firstComponents; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
$analysis->mergeAnnotations($openapi, $merge, true); |
| 110 |
} |
| 111 |
} |