|
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\Hydrator\Strategy; |
|
9
|
|
|
use Doctrine\ORM\EntityManager; |
|
10
|
|
|
|
|
11
|
|
|
use function in_array; |
|
12
|
|
|
|
|
13
|
|
|
final class GlobalEnable extends AbstractMetadataFactory |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var mixed[] */ |
|
16
|
|
|
private array $metadataConfig = []; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct( |
|
19
|
|
|
private EntityManager $entityManager, |
|
20
|
|
|
protected Config $config, |
|
21
|
|
|
) { |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param string[] $entityClasses |
|
26
|
|
|
* |
|
27
|
|
|
* @return mixed[] |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __invoke(array $entityClasses): array |
|
30
|
|
|
{ |
|
31
|
|
|
$globalIgnore = $this->config->getGlobalIgnore(); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
foreach ($entityClasses as $entityClass) { |
|
34
|
|
|
// Get extract by value or reference |
|
35
|
|
|
$byValue = $this->config->getGlobalByValue() ?? true; |
|
36
|
|
|
|
|
37
|
|
|
// Save entity-level metadata |
|
38
|
|
|
$this->metadataConfig[$entityClass] = [ |
|
39
|
|
|
'entityClass' => $entityClass, |
|
40
|
|
|
'byValue' => $byValue, |
|
41
|
|
|
'namingStrategy' => null, |
|
42
|
|
|
'fields' => [], |
|
43
|
|
|
'filters' => [], |
|
44
|
|
|
'excludeCriteria' => [], |
|
45
|
|
|
'description' => $entityClass, |
|
46
|
|
|
'typeName' => $this->getTypeName($entityClass), |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
$this->buildFieldMetadata($entityClass); |
|
50
|
|
|
$this->buildAssociationMetadata($entityClass); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $this->metadataConfig; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
private function buildFieldMetadata(string $entityClass): void |
|
57
|
|
|
{ |
|
58
|
|
|
$entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($entityClassMetadata->getFieldNames() as $fieldName) { |
|
61
|
|
|
if (in_array($fieldName, $this->config->getGlobalIgnore())) { |
|
62
|
|
|
continue; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->metadataConfig[$entityClass]['fields'][$fieldName]['description'] |
|
66
|
|
|
= $fieldName; |
|
67
|
|
|
|
|
68
|
|
|
$this->metadataConfig[$entityClass]['fields'][$fieldName]['type'] |
|
69
|
|
|
= $entityClassMetadata->getTypeOfField($fieldName); |
|
70
|
|
|
|
|
71
|
|
|
// Set default strategy based on field type |
|
72
|
|
|
$this->metadataConfig[$entityClass]['fields'][$fieldName]['strategy'] |
|
73
|
|
|
= $this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName)); |
|
74
|
|
|
|
|
75
|
|
|
$this->metadataConfig[$entityClass]['fields'][$fieldName]['excludeCriteria'] = []; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function buildAssociationMetadata(string $entityClass): void |
|
80
|
|
|
{ |
|
81
|
|
|
$entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass); |
|
82
|
|
|
|
|
83
|
|
|
foreach ($entityClassMetadata->getAssociationNames() as $associationName) { |
|
84
|
|
|
if (in_array($associationName, $this->config->getGlobalIgnore())) { |
|
85
|
|
|
continue; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->metadataConfig[$entityClass]['fields'][$associationName]['excludeCriteria'] = []; |
|
89
|
|
|
$this->metadataConfig[$entityClass]['fields'][$associationName]['description'] = $associationName; |
|
90
|
|
|
$this->metadataConfig[$entityClass]['fields'][$associationName]['filterCriteriaEventName'] |
|
91
|
|
|
= null; |
|
92
|
|
|
|
|
93
|
|
|
// NullifyOwningAssociation is not used for globalEnable |
|
94
|
|
|
$this->metadataConfig[$entityClass]['fields'][$associationName]['strategy'] = |
|
95
|
|
|
Strategy\AssociationDefault::class; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|