prolab-api/vendor/zircote/swagger-php/src/Processors/BuildPaths.php

62 lines
2.2 KiB
PHP
Raw Normal View History

2025-09-24 06:24:52 +00:00
<?php declare(strict_types=1);
/**
* @license Apache 2.0
*/
namespace OpenApi\Processors;
use OpenApi\Analysis;
2025-10-04 09:16:19 +00:00
use OpenApi\Annotations\Operation;
use OpenApi\Annotations\PathItem;
2025-09-24 06:24:52 +00:00
use OpenApi\Context;
use OpenApi\Generator;
/**
2025-10-04 09:16:19 +00:00
* Build the openapi->paths using the detected @OA\PathItem and @OA\Operations (like @OA\Get, @OA\Post, etc).
2025-09-24 06:24:52 +00:00
*/
2025-10-04 09:16:19 +00:00
class BuildPaths
2025-09-24 06:24:52 +00:00
{
public function __invoke(Analysis $analysis)
{
$paths = [];
// Merge @OA\PathItems with the same path.
2025-10-04 09:16:19 +00:00
if ($analysis->openapi->paths !== Generator::UNDEFINED) {
2025-09-24 06:24:52 +00:00
foreach ($analysis->openapi->paths as $annotation) {
if (empty($annotation->path)) {
$annotation->_context->logger->warning($annotation->identity() . ' is missing required property "path" in ' . $annotation->_context);
} elseif (isset($paths[$annotation->path])) {
$paths[$annotation->path]->mergeProperties($annotation);
$analysis->annotations->detach($annotation);
} else {
$paths[$annotation->path] = $annotation;
}
}
}
2025-10-04 09:16:19 +00:00
/** @var Operation[] $operations */
$operations = $analysis->unmerged()->getAnnotationsOfType(Operation::class);
2025-09-24 06:24:52 +00:00
// Merge @OA\Operations into existing @OA\PathItems or create a new one.
foreach ($operations as $operation) {
if ($operation->path) {
if (empty($paths[$operation->path])) {
2025-10-04 09:16:19 +00:00
$paths[$operation->path] = new PathItem(
2025-09-24 06:24:52 +00:00
[
'path' => $operation->path,
'_context' => new Context(['generated' => true], $operation->_context),
]
);
2025-10-04 09:16:19 +00:00
$analysis->annotations->attach($paths[$operation->path]);
2025-09-24 06:24:52 +00:00
}
if ($paths[$operation->path]->merge([$operation])) {
$operation->_context->logger->warning('Unable to merge ' . $operation->identity() . ' in ' . $operation->_context);
}
}
}
2025-10-04 09:16:19 +00:00
if (count($paths)) {
2025-09-24 06:24:52 +00:00
$analysis->openapi->paths = array_values($paths);
}
}
}