|
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\Metadata\Factory; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
|
17
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
18
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData; |
|
19
|
|
|
use Silverback\ApiComponentsBundle\Entity\Core\PageDataInterface; |
|
20
|
|
|
use Silverback\ApiComponentsBundle\Exception\PageDataNotFoundException; |
|
21
|
|
|
use Silverback\ApiComponentsBundle\Metadata\PageDataMetadata; |
|
22
|
|
|
use Silverback\ApiComponentsBundle\Metadata\PageDataPropertyMetadata; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @author Daniel West <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
class PageDataMetadataFactory implements PageDataMetadataFactoryInterface |
|
28
|
|
|
{ |
|
29
|
|
|
private ManagerRegistry $registry; |
|
30
|
|
|
private ResourceMetadataFactoryInterface $resourceMetadataFactory; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct( |
|
33
|
|
|
ManagerRegistry $registry, |
|
34
|
|
|
ResourceMetadataFactoryInterface $resourceMetadataFactory |
|
35
|
|
|
) { |
|
36
|
|
|
$this->registry = $registry; |
|
37
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function create(string $resourceClass): PageDataMetadata |
|
44
|
|
|
{ |
|
45
|
|
|
// needs to be a class |
|
46
|
|
|
if (!class_exists($resourceClass)) { |
|
47
|
|
|
throw new PageDataNotFoundException(sprintf('`%s` was not found', $resourceClass)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// Check it is page data |
|
51
|
|
|
$reflection = new \ReflectionClass($resourceClass); |
|
52
|
|
|
if (!$reflection->implementsInterface(PageDataInterface::class)) { |
|
53
|
|
|
throw new PageDataNotFoundException(sprintf('Resource class `%s` is not a valid page data resource', $resourceClass)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// FInd the doctrine manager |
|
57
|
|
|
$manager = $this->registry->getManagerForClass($resourceClass); |
|
58
|
|
|
if (!$manager) { |
|
59
|
|
|
throw new PageDataNotFoundException(sprintf('Cannot find manager for page data resource `%s`', $resourceClass)); |
|
60
|
|
|
} |
|
61
|
|
|
$classMetadata = $manager->getClassMetadata($resourceClass); |
|
62
|
|
|
|
|
63
|
|
|
// Get abstract prop names to exclude |
|
64
|
|
|
$abstractReflection = new \ReflectionClass(AbstractPageData::class); |
|
65
|
|
|
$reflProps = $abstractReflection->getProperties(); |
|
66
|
|
|
$abstractProps = array_map(static function (\ReflectionProperty $prop) { |
|
67
|
|
|
return $prop->name; |
|
68
|
|
|
}, $reflProps); |
|
69
|
|
|
|
|
70
|
|
|
// Get all fields with association |
|
71
|
|
|
$assocFields = array_filter($classMetadata->getAssociationNames(), static function ($name) use ($abstractProps) { |
|
72
|
|
|
return !\in_array($name, $abstractProps, true); |
|
73
|
|
|
}); |
|
74
|
|
|
|
|
75
|
|
|
// Create metadata with relations |
|
76
|
|
|
$metadata = new PageDataMetadata($resourceClass); |
|
77
|
|
|
foreach ($assocFields as $assocField) { |
|
78
|
|
|
$targetClass = $classMetadata->getAssociationTargetClass($assocField); |
|
79
|
|
|
$relationName = $this->resourceMetadataFactory->create($targetClass)->getShortName(); |
|
|
|
|
|
|
80
|
|
|
$metadata->addProperty(new PageDataPropertyMetadata($assocField, $relationName)); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $metadata; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|