1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Silverback API Components Bundle Project |
5
|
|
|
* |
6
|
|
|
* (c) Daniel West <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Silverback\ApiComponentsBundle\OpenApi; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Exception\ResourceClassNotFoundException; |
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
18
|
|
|
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface; |
19
|
|
|
use ApiPlatform\Core\OpenApi\Model\PathItem; |
20
|
|
|
use ApiPlatform\Core\OpenApi\OpenApi; |
21
|
|
|
use PackageVersions\Versions; |
22
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent; |
23
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData; |
24
|
|
|
use Silverback\ApiComponentsBundle\Model\Uploadable\MediaObject; |
25
|
|
|
use Symfony\Component\Form\Forms; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @author Daniel West <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class OpenApiFactory implements OpenApiFactoryInterface |
31
|
|
|
{ |
32
|
|
|
private OpenApiFactoryInterface $decorated; |
33
|
|
|
private ResourceMetadataFactoryInterface $resourceMetadataFactory; |
34
|
|
|
|
35
|
|
|
public function __construct(OpenApiFactoryInterface $decorated, ResourceMetadataFactoryInterface $resourceMetadataFactory) |
36
|
|
|
{ |
37
|
|
|
$this->decorated = $decorated; |
38
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function getResourceClassShortNames(array $resourceClassNames): array |
42
|
|
|
{ |
43
|
|
|
$shortNames = []; |
44
|
|
|
foreach ($resourceClassNames as $resourceClassName) { |
45
|
|
|
try { |
46
|
|
|
$metadata = $this->resourceMetadataFactory->create($resourceClassName); |
47
|
|
|
$shortNames[] = $metadata->getShortName(); |
48
|
|
|
} catch (ResourceClassNotFoundException $exception) { |
49
|
|
|
// the component may not be enabled |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $shortNames; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function removeResources(OpenApi $openApi, array $resourceClassNames): void |
57
|
|
|
{ |
58
|
|
|
$shortNames = $this->getResourceClassShortNames($resourceClassNames); |
59
|
|
|
$openApiPaths = $openApi->getPaths(); |
60
|
|
|
$paths = $openApiPaths->getPaths(); |
61
|
|
|
foreach ($paths as $path => $pathItem) { |
62
|
|
|
$operation = $pathItem->getGet() ?: $pathItem->getPost(); |
63
|
|
|
if (!$operation) { |
64
|
|
|
continue; |
65
|
|
|
} |
66
|
|
|
$tags = $operation->getTags(); |
67
|
|
|
if (!empty(array_intersect($tags, $shortNames))) { |
68
|
|
|
$openApiPaths->addPath($path, new PathItem()); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function __invoke(array $context = []): OpenApi |
74
|
|
|
{ |
75
|
|
|
$openApi = $this->decorated->__invoke($context); |
76
|
|
|
$version = sprintf('%s (%s)', $openApi->getInfo()->getVersion(), Versions::getVersion('silverbackis/api-components-bundle')); |
77
|
|
|
|
78
|
|
|
$this->removeResources($openApi, [ |
79
|
|
|
AbstractComponent::class, |
80
|
|
|
AbstractPageData::class, |
81
|
|
|
Forms::class, |
82
|
|
|
MediaObject::class, |
83
|
|
|
]); |
84
|
|
|
|
85
|
|
|
return $openApi->withInfo($openApi->getInfo()->withVersion($version)); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|