我有config/packages/api_platform.yaml
:
api_platform:
mapping:
paths:
- '%kernel.project_dir%/src/Entity'
- '%kernel.project_dir%/old/Entity'
在这两个命名空间中,我都有同名的实体。我必须做什么才能将它们分组到不同映射/docs
中显示,而不仅仅是按实体类名显示。
如何为每个命名空间配置前缀"全局"?
API 平台现在不提供此配置 您将不得不像这样装饰OpenApiFactory
招摇的文档按tags
对它们进行分组
<?php
// src/OpenApi/OpenApiFactory.php
namespace AppOpenApi;
use ApiPlatformCoreOpenApiFactoryOpenApiFactoryInterface;
use ApiPlatformCoreOpenApiOpenApi;
use ApiPlatformCoreOpenApiModel;
final class OpenApiFactory implements OpenApiFactoryInterface
{
private $decorated;
public function __construct(OpenApiFactoryInterface $decorated)
{
$this->decorated = $decorated;
}
public function __invoke(array $context = []): OpenApi
{
$openApi = $this->decorated->__invoke($context);
$paths = $openApi->getPaths()->getPaths();
$filteredPaths = new ModelPaths();
foreach ($paths as $path => $pathItem) {
// Here you can check the tags for each Operations
// Add custom logic with the snippet below
}
return $openApi->withPaths($filteredPaths);
}
}
脏的部分来了,标签将是EntityName
而不是完整的命名空间名称 我会列出所有旧实体,过滤其上的当前标签并更改为旧或新 (下面是伪代码)
$OldEntityList = ['entity1', 'entity2', ...]
foreach ($Operations as $op) {
if (in_array($op->getTags(), $OldEntityList) {
$op->withTags('old');
} else {
$op->withTags('new');
}
}