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\CollectionOperationInterface; |
18
|
|
|
use ApiPlatform\Metadata\HttpOperation; |
19
|
|
|
use ApiPlatform\Metadata\Operation; |
20
|
|
|
use ApiPlatform\Metadata\Operations; |
21
|
|
|
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
22
|
|
|
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
23
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\RoutableInterface; |
24
|
|
|
use Silverback\ApiComponentsBundle\Security\Voter\AbstractRoutableVoter; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author Daniel West <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class RoutableResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface |
30
|
|
|
{ |
31
|
|
|
private ResourceMetadataCollectionFactoryInterface $decorated; |
32
|
|
|
|
33
|
|
|
public function __construct(ResourceMetadataCollectionFactoryInterface $decorated) |
34
|
|
|
{ |
35
|
|
|
$this->decorated = $decorated; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function create(string $resourceClass): ResourceMetadataCollection |
39
|
|
|
{ |
40
|
|
|
$resourceMetadata = $this->decorated->create($resourceClass); |
41
|
|
|
|
42
|
|
|
$refl = new \ReflectionClass($resourceClass); |
43
|
|
|
if (!$refl->implementsInterface(RoutableInterface::class)) { |
44
|
|
|
return $resourceMetadata; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$newResources = []; |
48
|
|
|
/** @var ApiResource $resourceMetadatum */ |
49
|
|
|
foreach ($resourceMetadata as $resourceMetadatum) { |
50
|
|
|
$newOperations = []; |
51
|
|
|
$operations = $resourceMetadatum->getOperations(); |
52
|
|
|
if ($operations) { |
53
|
|
|
/** @var Operation $operation */ |
54
|
|
|
foreach ($operations as $i => $operation) { |
55
|
|
|
if ( |
56
|
|
|
HttpOperation::METHOD_POST !== $operation->getMethod() && |
57
|
|
|
!$operation instanceof CollectionOperationInterface && |
58
|
|
|
!$operation->getSecurity()) { |
59
|
|
|
$operation = $operation->withSecurity(sprintf("is_granted('%s', object)", AbstractRoutableVoter::READ_ROUTABLE)); |
60
|
|
|
} |
61
|
|
|
$newOperations[$i] = $operation; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
$newResources[] = $resourceMetadatum->withOperations(new Operations($newOperations)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return new ResourceMetadataCollection($resourceClass, $newResources); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|