|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Silverback API Component 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\ApiComponentBundle\Metadata; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
|
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
|
18
|
|
|
use Silverback\ApiComponentBundle\Entity\Core\AbstractComponent; |
|
19
|
|
|
use Silverback\ApiComponentBundle\Entity\Core\AbstractPageData; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Daniel West <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class AutoRoutePrefixMetadataFactory implements ResourceMetadataFactoryInterface |
|
25
|
|
|
{ |
|
26
|
|
|
private ResourceMetadataFactoryInterface $decorated; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct(ResourceMetadataFactoryInterface $decorated) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->decorated = $decorated; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function create(string $resourceClass): ResourceMetadata |
|
34
|
|
|
{ |
|
35
|
|
|
$resourceMetadata = $this->decorated->create($resourceClass); |
|
36
|
|
|
|
|
37
|
|
|
$reflection = new \ReflectionClass($resourceClass); |
|
38
|
|
|
$namespace = $reflection->getNamespaceName(); |
|
39
|
|
|
$acbNamespace = 'Silverback\ApiComponentBundle\\'; |
|
40
|
|
|
if (strpos($namespace, $acbNamespace) !== 0) { |
|
41
|
|
|
return $resourceMetadata; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$routePrefixParts = ['_']; |
|
45
|
|
|
if (is_subclass_of($resourceClass, AbstractComponent::class)) { |
|
46
|
|
|
$routePrefixParts[] = 'component'; |
|
47
|
|
|
}elseif (is_subclass_of($resourceClass, AbstractPageData::class)) { |
|
48
|
|
|
$routePrefixParts[] = 'page_data'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $this->prefixRoute($routePrefixParts, $resourceMetadata); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function prefixRoute(array $routePrefixParts, ResourceMetadata $resourceMetadata): ResourceMetadata |
|
55
|
|
|
{ |
|
56
|
|
|
if ($currentRoutePrefix = $resourceMetadata->getAttribute('route_prefix')) { |
|
57
|
|
|
$routePrefixParts[] = trim($currentRoutePrefix, '/'); |
|
58
|
|
|
} |
|
59
|
|
|
$newRoutePrefix = '/' . implode('/', $routePrefixParts); |
|
60
|
|
|
|
|
61
|
|
|
$attributes = $resourceMetadata->getAttributes() ?: []; |
|
62
|
|
|
|
|
63
|
|
|
return $resourceMetadata->withAttributes(array_merge($attributes, [ |
|
64
|
|
|
'route_prefix' => $newRoutePrefix, |
|
65
|
|
|
])); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|