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