|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ApiSkeletons\Doctrine\GraphQL\Metadata; |
|
6
|
|
|
|
|
7
|
|
|
use ApiSkeletons\Doctrine\GraphQL\Config; |
|
8
|
|
|
use ApiSkeletons\Doctrine\GraphQL\Event\BuildMetadata; |
|
9
|
|
|
use ApiSkeletons\Doctrine\GraphQL\Hydrator\Strategy; |
|
10
|
|
|
use ArrayObject; |
|
11
|
|
|
use Doctrine\ORM\EntityManager; |
|
12
|
|
|
use League\Event\EventDispatcher; |
|
13
|
|
|
|
|
14
|
|
|
use function in_array; |
|
15
|
|
|
|
|
16
|
|
|
final class GlobalEnable extends AbstractMetadataFactory |
|
17
|
|
|
{ |
|
18
|
|
|
private ArrayObject $metadata; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct( |
|
21
|
|
|
private EntityManager $entityManager, |
|
22
|
|
|
protected Config $config, |
|
23
|
|
|
protected EventDispatcher $eventDispatcher, |
|
24
|
|
|
) { |
|
25
|
|
|
$this->metadata = new ArrayObject(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** @param string[] $entityClasses */ |
|
29
|
|
|
public function __invoke(array $entityClasses): ArrayObject |
|
30
|
|
|
{ |
|
31
|
|
|
foreach ($entityClasses as $entityClass) { |
|
32
|
|
|
// Get extract by value or reference |
|
33
|
|
|
$byValue = $this->config->getGlobalByValue() ?? true; |
|
34
|
|
|
|
|
35
|
|
|
// Save entity-level metadata |
|
36
|
|
|
$this->metadata[$entityClass] = [ |
|
37
|
|
|
'entityClass' => $entityClass, |
|
38
|
|
|
'byValue' => $byValue, |
|
39
|
|
|
'limit' => 0, |
|
40
|
|
|
'namingStrategy' => null, |
|
41
|
|
|
'fields' => [], |
|
42
|
|
|
'filters' => [], |
|
43
|
|
|
'excludeCriteria' => [], |
|
44
|
|
|
'description' => $entityClass, |
|
45
|
|
|
'typeName' => $this->getTypeName($entityClass), |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
$this->buildFieldMetadata($entityClass); |
|
49
|
|
|
$this->buildAssociationMetadata($entityClass); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->eventDispatcher->dispatch( |
|
53
|
|
|
new BuildMetadata($this->metadata, 'metadata.build'), |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
return $this->metadata; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
private function buildFieldMetadata(string $entityClass): void |
|
60
|
|
|
{ |
|
61
|
|
|
$entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass); |
|
62
|
|
|
|
|
63
|
|
|
foreach ($entityClassMetadata->getFieldNames() as $fieldName) { |
|
64
|
|
|
if (in_array($fieldName, $this->config->getGlobalIgnore())) { |
|
65
|
|
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->metadata[$entityClass]['fields'][$fieldName] = [ |
|
69
|
|
|
'description' => $fieldName, |
|
70
|
|
|
'type' => $entityClassMetadata->getTypeOfField($fieldName), |
|
71
|
|
|
'strategy' => $this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName)), |
|
72
|
|
|
'excludeCriteria' => [], |
|
73
|
|
|
]; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function buildAssociationMetadata(string $entityClass): void |
|
78
|
|
|
{ |
|
79
|
|
|
$entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass); |
|
80
|
|
|
|
|
81
|
|
|
foreach ($entityClassMetadata->getAssociationNames() as $associationName) { |
|
82
|
|
|
if (in_array($associationName, $this->config->getGlobalIgnore())) { |
|
83
|
|
|
continue; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->metadata[$entityClass]['fields'][$associationName] = [ |
|
87
|
|
|
'excludeCriteria' => [], |
|
88
|
|
|
'description' => $associationName, |
|
89
|
|
|
'filterCriteriaEventName' => null, |
|
90
|
|
|
'strategy' => Strategy\AssociationDefault::class, |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|