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
|
|
|
// Fetch fields |
50
|
|
|
$entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass); |
51
|
|
|
$fieldNames = $entityClassMetadata->getFieldNames(); |
52
|
|
|
|
53
|
|
|
foreach ($fieldNames as $fieldName) { |
54
|
|
|
if (in_array($fieldName, $globalIgnore)) { |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->metadataConfig[$entityClass]['fields'][$fieldName]['description'] = |
59
|
|
|
$fieldName; |
60
|
|
|
|
61
|
|
|
$this->metadataConfig[$entityClass]['fields'][$fieldName]['type'] = |
62
|
|
|
$entityClassMetadata->getTypeOfField($fieldName); |
63
|
|
|
|
64
|
|
|
// Set default strategy based on field type |
65
|
|
|
$this->metadataConfig[$entityClass]['fields'][$fieldName]['strategy'] = |
66
|
|
|
$this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName)); |
67
|
|
|
|
68
|
|
|
$this->metadataConfig[$entityClass]['fields'][$fieldName]['excludeCriteria'] = []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Fetch attributes for associations |
72
|
|
|
$associationNames = $this->entityManager->getMetadataFactory() |
73
|
|
|
->getMetadataFor($entityClass)->getAssociationNames(); |
74
|
|
|
|
75
|
|
|
foreach ($associationNames as $associationName) { |
76
|
|
|
if (in_array($associationName, $globalIgnore)) { |
77
|
|
|
continue; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->metadataConfig[$entityClass]['fields'][$associationName]['excludeCriteria'] = []; |
81
|
|
|
$this->metadataConfig[$entityClass]['fields'][$associationName]['description'] = $associationName; |
82
|
|
|
$this->metadataConfig[$entityClass]['fields'][$associationName]['filterCriteriaEventName'] |
83
|
|
|
= null; |
84
|
|
|
|
85
|
|
|
// NullifyOwningAssociation is not used for globalEnable |
86
|
|
|
$this->metadataConfig[$entityClass]['fields'][$associationName]['strategy'] = |
87
|
|
|
Strategy\AssociationDefault::class; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->metadataConfig; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|