| 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 |
* Build the openapi->paths using the detected <code>@OA\PathItem</code> and <code>@OA\Operation</code> (<code>@OA\Get</code>, etc). |
| 16 |
*/ |
| 17 |
class BuildPaths |
| 18 |
{ |
| 19 |
public function __invoke(Analysis $analysis): void |
| 20 |
{ |
| 21 |
$paths = []; |
| 22 |
// Merge @OA\PathItems with the same path. |
| 23 |
if (!Generator::isDefault($analysis->openapi->paths)) { |
| 24 |
foreach ($analysis->openapi->paths as $annotation) { |
| 25 |
if (empty($annotation->path)) { |
| 26 |
$annotation->_context->logger->warning($annotation->identity() . ' is missing required property "path" in ' . $annotation->_context); |
| 27 |
} elseif (isset($paths[$annotation->path])) { |
| 28 |
$paths[$annotation->path]->mergeProperties($annotation); |
| 29 |
$analysis->removeAnnotation($annotation); |
| 30 |
} else { |
| 31 |
$paths[$annotation->path] = $annotation; |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
$operations = $analysis->unmerged()->getAnnotationsOfType(OA\Operation::class); |
| 37 |
|
| 38 |
// Merge @OA\Operations into existing @OA\PathItems or create a new one. |
| 39 |
foreach ($operations as $operation) { |
| 40 |
if ($operation->path) { |
| 41 |
if (empty($paths[$operation->path])) { |
| 42 |
$paths[$operation->path] = $pathItem = new OA\PathItem([ |
| 43 |
'path' => $operation->path, |
| 44 |
'_context' => new Context(['generated' => true], $operation->_context), |
| 45 |
]); |
| 46 |
$analysis->addAnnotation($pathItem, $pathItem->_context); |
| 47 |
} |
| 48 |
if ($analysis->mergeAnnotations($paths[$operation->path], [$operation])) { |
| 49 |
$operation->_context->logger->warning('Unable to merge ' . $operation->identity() . ' in ' . $operation->_context); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
if ($paths) { |
| 54 |
$analysis->openapi->paths = array_values($paths); |
| 55 |
} |
| 56 |
} |
| 57 |
} |