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\ApiPlatform\Metadata\Resource; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Metadata\ApiResource; |
17
|
|
|
use ApiPlatform\Metadata\HttpOperation; |
18
|
|
|
use ApiPlatform\Metadata\Operation\PathSegmentNameGeneratorInterface; |
19
|
|
|
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
20
|
|
|
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
21
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\ComponentInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This will add an endpoint for component resources to find out usage totals. |
25
|
|
|
* |
26
|
|
|
* @author Daniel West <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class ComponentResourceMetadataFactory implements ResourceMetadataCollectionFactoryInterface |
29
|
|
|
{ |
30
|
|
|
private ResourceMetadataCollectionFactoryInterface $decorated; |
31
|
|
|
private PathSegmentNameGeneratorInterface $pathSegmentNameGenerator; |
32
|
|
|
|
33
|
|
|
public function __construct(ResourceMetadataCollectionFactoryInterface $decorated, PathSegmentNameGeneratorInterface $pathSegmentNameGenerator) |
34
|
|
|
{ |
35
|
|
|
$this->decorated = $decorated; |
36
|
|
|
$this->pathSegmentNameGenerator = $pathSegmentNameGenerator; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function create(string $resourceClass): ResourceMetadataCollection |
40
|
|
|
{ |
41
|
|
|
$resourceMetadata = $this->decorated->create($resourceClass); |
42
|
|
|
$interfaces = class_implements($resourceClass); |
43
|
|
|
if (!\in_array(ComponentInterface::class, $interfaces, true)) { |
44
|
|
|
return $resourceMetadata; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** @var ApiResource $resourceMetadatum */ |
48
|
|
|
foreach ($resourceMetadata as $resourceMetadatum) { |
49
|
|
|
$resourceShortName = $resourceMetadatum->getShortName(); |
50
|
|
|
if (!$resourceShortName) { |
51
|
|
|
throw new \RuntimeException(sprintf('Could not find short name from resource metadata for %s', $resourceClass)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$pathSegmentName = $this->pathSegmentNameGenerator->getSegmentName($resourceShortName); |
55
|
|
|
$usagePath = sprintf('/%s/{id}/usage', $pathSegmentName); |
56
|
|
|
|
57
|
|
|
$operations = $resourceMetadatum->getOperations(); |
58
|
|
|
if ($operations) { |
59
|
|
|
$copyOperation = null; |
60
|
|
|
/** @var HttpOperation $operation */ |
61
|
|
|
foreach ($operations as $operation) { |
62
|
|
|
$uriVariables = $operation->getUriVariables(); |
63
|
|
|
if ($uriVariables) { |
64
|
|
|
$copyOperation = $operation; |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
if ($copyOperation) { |
69
|
|
|
$usageOperation = $copyOperation->withUriTemplate($usagePath); |
70
|
|
|
$operations->add('_api_' . $usagePath . '_get_usage', $usageOperation); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $resourceMetadata; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|