|
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\AbstractComponent; |
|
22
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Daniel West <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class RoutingPrefixResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface |
|
28
|
|
|
{ |
|
29
|
|
|
private ResourceMetadataCollectionFactoryInterface $decorated; |
|
30
|
|
|
|
|
31
|
5 |
|
public function __construct(ResourceMetadataCollectionFactoryInterface $decorated) |
|
32
|
|
|
{ |
|
33
|
5 |
|
$this->decorated = $decorated; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
5 |
|
public function create(string $resourceClass): ResourceMetadataCollection |
|
37
|
|
|
{ |
|
38
|
5 |
|
$resourceMetadata = $this->decorated->create($resourceClass); |
|
39
|
|
|
|
|
40
|
5 |
|
$routePrefixParts = []; |
|
41
|
5 |
|
if (is_subclass_of($resourceClass, AbstractComponent::class)) { |
|
42
|
2 |
|
$routePrefixParts[] = 'component'; |
|
43
|
3 |
|
} elseif (is_subclass_of($resourceClass, AbstractPageData::class)) { |
|
44
|
1 |
|
$routePrefixParts[] = 'page_data'; |
|
45
|
|
|
} else { |
|
46
|
|
|
// underscores for core resources |
|
47
|
2 |
|
$reflection = new \ReflectionClass($resourceClass); |
|
48
|
2 |
|
$namespace = $reflection->getNamespaceName(); |
|
49
|
2 |
|
if (preg_match("/Silverback\\\\ApiComponentsBundle\\\\(?!Test)[\w]+/", $namespace)) { |
|
50
|
1 |
|
$routePrefixParts[] = '_'; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
5 |
|
if (!\count($routePrefixParts)) { |
|
55
|
1 |
|
return $resourceMetadata; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
return $this->prefixRoute($resourceClass, $routePrefixParts, $resourceMetadata); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
4 |
|
private function prefixRoute(string $resourceClass, array $routePrefixParts, ResourceMetadataCollection $resourceMetadata): ResourceMetadataCollection |
|
62
|
|
|
{ |
|
63
|
4 |
|
$resources = []; |
|
64
|
|
|
/** @var ApiResource $resourceMetadatum */ |
|
65
|
4 |
|
foreach ($resourceMetadata as $i => $resourceMetadatum) { |
|
66
|
1 |
|
$finalRoutePrefixParts = [...$routePrefixParts]; |
|
67
|
1 |
|
if ($currentRoutePrefix = $resourceMetadatum->getRoutePrefix()) { |
|
68
|
1 |
|
$finalRoutePrefixParts[] = trim($currentRoutePrefix, '/'); |
|
69
|
|
|
} |
|
70
|
1 |
|
$newRoutePrefix = '/' . implode('/', $finalRoutePrefixParts); |
|
71
|
1 |
|
$resources[$i] = $resourceMetadatum->withRoutePrefix($newRoutePrefix); |
|
72
|
1 |
|
$newOperations = []; |
|
73
|
1 |
|
$oldOperations = $resourceMetadatum->getOperations(); |
|
74
|
|
|
/** |
|
75
|
|
|
* @var string $key |
|
76
|
|
|
* @var Operation $oldOperation |
|
77
|
|
|
*/ |
|
78
|
1 |
|
foreach ($oldOperations as $key => $oldOperation) { |
|
79
|
1 |
|
$subRoutePrefixParts = [...$routePrefixParts]; |
|
80
|
1 |
|
if ($currentRoutePrefix = $oldOperation->getRoutePrefix()) { |
|
81
|
1 |
|
$subRoutePrefixParts[] = trim($currentRoutePrefix, '/'); |
|
82
|
1 |
|
$subRoutePrefix = '/' . implode('/', $subRoutePrefixParts); |
|
83
|
|
|
} else { |
|
84
|
1 |
|
$subRoutePrefix = $newRoutePrefix; |
|
85
|
|
|
} |
|
86
|
1 |
|
$newOperations[$key] = $oldOperation->withRoutePrefix($subRoutePrefix); |
|
87
|
|
|
} |
|
88
|
1 |
|
$resources[$i] = $resources[$i]->withOperations(new Operations($newOperations)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
4 |
|
return new ResourceMetadataCollection($resourceClass, $resources); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|