-
Notifications
You must be signed in to change notification settings - Fork 939
Expand file tree
/
Copy pathMergeIntoOpenApi.php
More file actions
111 lines (92 loc) · 3.59 KB
/
Copy pathMergeIntoOpenApi.php
File metadata and controls
111 lines (92 loc) · 3.59 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php declare(strict_types=1);
/**
* @license Apache 2.0
*/
namespace OpenApi\Processors;
use OpenApi\Analysis;
use OpenApi\Annotations as OA;
use OpenApi\Context;
use OpenApi\Generator;
/**
* Merge all <code>@OA\OpenApi</code> annotations into one.
*/
class MergeIntoOpenApi
{
protected bool $mergeComponents;
public function __construct(bool $mergeComponents = false)
{
$this->mergeComponents = $mergeComponents;
}
public function isMergeComponents(): bool
{
return $this->mergeComponents;
}
/**
* If set to <code>true</code> allow multiple `@OA\Components` annotations to be merged.
*/
public function setMergeComponents(bool $mergeComponents): MergeIntoOpenApi
{
$this->mergeComponents = $mergeComponents;
return $this;
}
public function __invoke(Analysis $analysis): void
{
// Auto-create the OpenApi annotation.
if (!$analysis->openapi) {
$context = new Context(['generated' => true], $analysis->context);
$analysis->addAnnotation(new OA\OpenApi(['_context' => $context]), $context);
}
$openapi = $analysis->openapi;
$openapi->_analysis = $analysis;
// Merge annotations into the target openapi
$merge = [];
/** @var OA\AbstractAnnotation $annotation */
foreach ($analysis->annotations as $annotation) {
if ($annotation === $openapi) {
continue;
}
if ($annotation instanceof OA\OpenApi) {
$paths = $annotation->paths;
unset($annotation->paths);
$openapi->mergeProperties($annotation);
if (!Generator::isDefault($paths)) {
foreach ($paths as $path) {
if (Generator::isDefault($openapi->paths)) {
$openapi->paths = [];
}
$openapi->paths[] = $path;
}
}
} elseif ($annotation instanceof OA\AbstractAnnotation
&& in_array(OA\OpenApi::class, $annotation::$_parents)
&& false === $annotation->_context->is('nested')) {
// A top-level annotation.
$merge[] = $annotation;
}
}
if ($this->isMergeComponents()) {
// merge Components
$componentsList = array_filter($merge, fn (OA\AbstractAnnotation $annotation): bool => $annotation instanceof OA\Components);
$firstComponents = $openapi->components;
if ((!Generator::isDefault($firstComponents) && $componentsList !== []) || count($merge) > 1) {
if (Generator::isDefault($firstComponents)) {
$firstComponents = array_shift($componentsList);
}
foreach ($componentsList as $components) {
foreach (OA\Components::$_nested as $nested) {
if (2 == count($nested)) {
$property = $nested[0];
if (!Generator::isDefault($components->{$property})) {
$firstComponents->merge($components->{$property});
}
}
}
$analysis->removeAnnotation($components);
}
$merge = array_filter($merge, fn (OA\AbstractAnnotation $annotation): bool => !$annotation instanceof OA\Components);
$merge[] = $firstComponents;
}
}
$openapi->merge($merge, true);
}
}