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
|
|
|
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->metadataConfig[$entityClass] = [ |
37
|
|
|
'entityClass' => $entityClass, |
38
|
|
|
'byValue' => $byValue, |
39
|
|
|
'namingStrategy' => null, |
40
|
|
|
'fields' => [], |
41
|
|
|
'filters' => [], |
42
|
|
|
'excludeCriteria' => [], |
43
|
|
|
'description' => $entityClass, |
44
|
|
|
'typeName' => $this->getTypeName($entityClass), |
45
|
|
|
]; |
46
|
|
|
|
47
|
|
|
$this->buildFieldMetadata($entityClass); |
48
|
|
|
$this->buildAssociationMetadata($entityClass); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->metadataConfig; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
private function buildFieldMetadata(string $entityClass): void |
55
|
|
|
{ |
56
|
|
|
$entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass); |
57
|
|
|
|
58
|
|
|
foreach ($entityClassMetadata->getFieldNames() as $fieldName) { |
59
|
|
|
if (in_array($fieldName, $this->config->getGlobalIgnore())) { |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$this->metadataConfig[$entityClass]['fields'][$fieldName]['description'] |
64
|
|
|
= $fieldName; |
65
|
|
|
|
66
|
|
|
$this->metadataConfig[$entityClass]['fields'][$fieldName]['type'] |
67
|
|
|
= $entityClassMetadata->getTypeOfField($fieldName); |
68
|
|
|
|
69
|
|
|
// Set default strategy based on field type |
70
|
|
|
$this->metadataConfig[$entityClass]['fields'][$fieldName]['strategy'] |
71
|
|
|
= $this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName)); |
72
|
|
|
|
73
|
|
|
$this->metadataConfig[$entityClass]['fields'][$fieldName]['excludeCriteria'] = []; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public 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->metadataConfig[$entityClass]['fields'][$associationName]['excludeCriteria'] = []; |
87
|
|
|
$this->metadataConfig[$entityClass]['fields'][$associationName]['description'] = $associationName; |
88
|
|
|
$this->metadataConfig[$entityClass]['fields'][$associationName]['filterCriteriaEventName'] |
89
|
|
|
= null; |
90
|
|
|
|
91
|
|
|
// NullifyOwningAssociation is not used for globalEnable |
92
|
|
|
$this->metadataConfig[$entityClass]['fields'][$associationName]['strategy'] = |
93
|
|
|
Strategy\AssociationDefault::class; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|