diff --git a/src/Metadata/AbstractMetadataFactory.php b/src/Metadata/AbstractMetadataFactory.php new file mode 100644 index 0000000..1dd71bc --- /dev/null +++ b/src/Metadata/AbstractMetadataFactory.php @@ -0,0 +1,82 @@ +appendGroupSuffix($this->stripEntityPrefix($entityClass)); + } + + /** + * Strip the configured entityPrefix from the type name + */ + protected function stripEntityPrefix(string $entityClass): string + { + if ($this->config->getEntityPrefix() !== null) { + if (strpos($entityClass, $this->config->getEntityPrefix()) === 0) { + $entityClass = substr($entityClass, strlen($this->config->getEntityPrefix())); + } + } + + return str_replace('\\', '_', $entityClass); + } + + /** + * Append the configured groupSuffix from the type name + */ + protected function appendGroupSuffix(string $entityClass): string + { + if ($this->config->getGroupSuffix() !== null) { + if ($this->config->getGroupSuffix()) { + $entityClass .= '_' . $this->config->getGroupSuffix(); + } + } else { + $entityClass .= '_' . $this->config->getGroup(); + } + + return $entityClass; + } +} diff --git a/src/Metadata/GlobalEnable.php b/src/Metadata/GlobalEnable.php new file mode 100644 index 0000000..d4a000a --- /dev/null +++ b/src/Metadata/GlobalEnable.php @@ -0,0 +1,93 @@ +config->getGlobalIgnore(); + + foreach ($entityClasses as $entityClass) { + // Get extract by value or reference + $byValue = $this->config->getGlobalByValue() ?? true; + + // Save entity-level metadata + $this->metadataConfig[$entityClass] = [ + 'entityClass' => $entityClass, + 'byValue' => $byValue, + 'namingStrategy' => null, + 'fields' => [], + 'filters' => [], + 'excludeCriteria' => [], + 'description' => $entityClass, + 'typeName' => $this->getTypeName($entityClass), + ]; + + // Fetch fields + $entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass); + $fieldNames = $entityClassMetadata->getFieldNames(); + + foreach ($fieldNames as $fieldName) { + if (in_array($fieldName, $globalIgnore)) { + continue; + } + + $this->metadataConfig[$entityClass]['fields'][$fieldName]['description'] = + $fieldName; + + $this->metadataConfig[$entityClass]['fields'][$fieldName]['type'] = + $entityClassMetadata->getTypeOfField($fieldName); + + // Set default strategy based on field type + $this->metadataConfig[$entityClass]['fields'][$fieldName]['strategy'] = + $this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName)); + + $this->metadataConfig[$entityClass]['fields'][$fieldName]['excludeCriteria'] = []; + } + + // Fetch attributes for associations + $associationNames = $this->entityManager->getMetadataFactory() + ->getMetadataFor($entityClass)->getAssociationNames(); + + foreach ($associationNames as $associationName) { + if (in_array($associationName, $globalIgnore)) { + continue; + } + + $this->metadataConfig[$entityClass]['fields'][$associationName]['excludeCriteria'] = []; + $this->metadataConfig[$entityClass]['fields'][$associationName]['description'] = $associationName; + $this->metadataConfig[$entityClass]['fields'][$associationName]['filterCriteriaEventName'] + = null; + + // NullifyOwningAssociation is not used for globalEnable + $this->metadataConfig[$entityClass]['fields'][$associationName]['strategy'] = + Strategy\AssociationDefault::class; + } + } + + return $this->metadataConfig; + } +} diff --git a/src/Metadata/MetadataFactory.php b/src/Metadata/MetadataFactory.php index b826714..cb545c7 100644 --- a/src/Metadata/MetadataFactory.php +++ b/src/Metadata/MetadataFactory.php @@ -14,13 +14,8 @@ use ReflectionClass; use function assert; -use function in_array; -use function str_replace; -use function strlen; -use function strpos; -use function substr; -class MetadataFactory +class MetadataFactory extends AbstractMetadataFactory { protected Metadata|null $metadata = null; protected EntityManager $entityManager; @@ -48,74 +43,6 @@ public function getMetadata(): Metadata return $this->buildMetadata(); } - /** @param string[] $entityClasses */ - private function globalEnable(array $entityClasses): Metadata - { - $globalIgnore = $this->config->getGlobalIgnore(); - - foreach ($entityClasses as $entityClass) { - // Get extract by value or reference - $byValue = $this->config->getGlobalByValue() ?? true; - - // Save entity-level metadata - $this->metadataConfig[$entityClass] = [ - 'entityClass' => $entityClass, - 'byValue' => $byValue, - 'namingStrategy' => null, - 'fields' => [], - 'filters' => [], - 'excludeCriteria' => [], - 'description' => $entityClass, - 'typeName' => $this->getTypeName($entityClass), - ]; - - // Fetch fields - $entityClassMetadata = $this->entityManager->getMetadataFactory()->getMetadataFor($entityClass); - $fieldNames = $entityClassMetadata->getFieldNames(); - - foreach ($fieldNames as $fieldName) { - if (in_array($fieldName, $globalIgnore)) { - continue; - } - - $this->metadataConfig[$entityClass]['fields'][$fieldName]['description'] = - $fieldName; - - $this->metadataConfig[$entityClass]['fields'][$fieldName]['type'] = - $entityClassMetadata->getTypeOfField($fieldName); - - // Set default strategy based on field type - $this->metadataConfig[$entityClass]['fields'][$fieldName]['strategy'] = - $this->getDefaultStrategy($entityClassMetadata->getTypeOfField($fieldName)); - - $this->metadataConfig[$entityClass]['fields'][$fieldName]['excludeCriteria'] = []; - } - - // Fetch attributes for associations - $associationNames = $this->entityManager->getMetadataFactory() - ->getMetadataFor($entityClass)->getAssociationNames(); - - foreach ($associationNames as $associationName) { - if (in_array($associationName, $globalIgnore)) { - continue; - } - - $this->metadataConfig[$entityClass]['fields'][$associationName]['excludeCriteria'] = []; - $this->metadataConfig[$entityClass]['fields'][$associationName]['description'] = $associationName; - $this->metadataConfig[$entityClass]['fields'][$associationName]['filterCriteriaEventName'] - = null; - - // NullifyOwningAssociation is not used for globalEnable - $this->metadataConfig[$entityClass]['fields'][$associationName]['strategy'] = - Strategy\AssociationDefault::class; - } - } - - $this->metadata = new Metadata($this->container, $this->metadataConfig); - - return $this->metadata; - } - protected function buildMetadata(): Metadata { $entityClasses = []; @@ -124,7 +51,9 @@ protected function buildMetadata(): Metadata } if ($this->config->getGlobalEnable()) { - return $this->globalEnable($entityClasses); + $globalEnable = $this->container->get(GlobalEnable::class); + + return new Metadata($this->container, $globalEnable($entityClasses)); } foreach ($entityClasses as $entityClass) { @@ -286,64 +215,4 @@ private function buildMetadataConfigForAssociations( } } } - - /** - * Strip the configured entityPrefix from the type name - */ - private function stripEntityPrefix(string $entityClass): string - { - if ($this->config->getEntityPrefix() !== null) { - if (strpos($entityClass, $this->config->getEntityPrefix()) === 0) { - $entityClass = substr($entityClass, strlen($this->config->getEntityPrefix())); - } - } - - return str_replace('\\', '_', $entityClass); - } - - /** - * Append the configured groupSuffix from the type name - */ - private function appendGroupSuffix(string $entityClass): string - { - if ($this->config->getGroupSuffix() !== null) { - if ($this->config->getGroupSuffix()) { - $entityClass .= '_' . $this->config->getGroupSuffix(); - } - } else { - $entityClass .= '_' . $this->config->getGroup(); - } - - return $entityClass; - } - - /** - * Compute the GraphQL type name - */ - private function getTypeName(string $entityClass): string - { - return $this->appendGroupSuffix($this->stripEntityPrefix($entityClass)); - } - - private function getDefaultStrategy(string|null $fieldType): string - { - // Set default strategy based on field type - switch ($fieldType) { - case 'tinyint': - case 'smallint': - case 'integer': - case 'int': - return Strategy\ToInteger::class; - - case 'boolean': - return Strategy\ToBoolean::class; - - case 'decimal': - case 'float': - return Strategy\ToFloat::class; - - default: - return Strategy\FieldDefault::class; - } - } } diff --git a/src/Services.php b/src/Services.php index 9c1164f..b0b44ce 100644 --- a/src/Services.php +++ b/src/Services.php @@ -52,6 +52,15 @@ static function (ContainerInterface $container) use ($metadataConfig) { return (new Metadata\MetadataFactory($container, $metadataConfig))->getMetadata(); }, ) + ->set( + Metadata\GlobalEnable::class, + static function (ContainerInterface $container) { + return new Metadata\GlobalEnable( + $container->get(EntityManager::class), + $container->get(Config::class), + ); + }, + ) ->set( Resolve\FieldResolver::class, static function (ContainerInterface $container) {