forked from zircote/swagger-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeIntoOpenApi.php
59 lines (54 loc) · 1.88 KB
/
MergeIntoOpenApi.php
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
<?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 @OA\OpenApi annotations into one.
*/
class MergeIntoOpenApi implements ProcessorInterface
{
public function __invoke(Analysis $analysis)
{
// Auto-create the OpenApi annotation.
if (!$analysis->openapi) {
$context = new Context([], $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)
&& property_exists($annotation, '_context')
&& false === $annotation->_context->is('nested')) {
// A top level annotation.
$merge[] = $annotation;
}
}
$openapi->merge($merge, true);
}
}