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\OpenApi\Factory\OpenApiFactoryInterface; |
17
|
|
|
use ApiPlatform\Core\OpenApi\Model\PathItem; |
18
|
|
|
use ApiPlatform\Core\OpenApi\OpenApi; |
19
|
|
|
use PackageVersions\Versions; |
20
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent; |
21
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData; |
22
|
|
|
use Silverback\ApiComponentsBundle\Model\Uploadable\MediaObject; |
23
|
|
|
use Symfony\Component\Form\Forms; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Daniel West <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class OpenApiFactory implements OpenApiFactoryInterface |
29
|
|
|
{ |
30
|
|
|
private OpenApiFactoryInterface $decorated; |
31
|
|
|
|
32
|
|
|
public function __construct(OpenApiFactoryInterface $decorated) |
33
|
|
|
{ |
34
|
|
|
$this->decorated = $decorated; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function removeResources(OpenApi $openApi, array $resourceClassNames) |
38
|
|
|
{ |
39
|
|
|
$shortNames = []; |
40
|
|
|
foreach ($resourceClassNames as $resourceClassName) { |
41
|
|
|
$shortNames[] = (new \ReflectionClass($resourceClassName))->getShortName(); |
42
|
|
|
} |
43
|
|
|
$openApiPaths = $openApi->getPaths(); |
44
|
|
|
$paths = $openApiPaths->getPaths(); |
45
|
|
|
foreach ($paths as $path => $pathItem) { |
46
|
|
|
$operation = $pathItem->getGet() ?: $pathItem->getPost(); |
47
|
|
|
if (!$operation) { |
48
|
|
|
continue; |
49
|
|
|
} |
50
|
|
|
$tags = $operation->getTags(); |
51
|
|
|
if (!empty(array_intersect($tags, $shortNames))) { |
52
|
|
|
$openApiPaths->addPath($path, new PathItem()); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function __invoke(array $context = []): OpenApi |
58
|
|
|
{ |
59
|
|
|
$openApi = $this->decorated->__invoke($context); |
60
|
|
|
$version = sprintf('%s (%s)', $openApi->getInfo()->getVersion(), Versions::getVersion('silverbackis/api-components-bundle')); |
61
|
|
|
|
62
|
|
|
$this->removeResources($openApi, [ |
63
|
|
|
AbstractComponent::class, |
64
|
|
|
AbstractPageData::class, |
65
|
|
|
Forms::class, |
66
|
|
|
MediaObject::class, |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
|
return $openApi->withInfo($openApi->getInfo()->withVersion($version)); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|