-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from TomHAnderson/hotfix/globalEnable
Moved GlobalEnable logic to its own class
- Loading branch information
Showing
4 changed files
with
188 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiSkeletons\Doctrine\GraphQL\Metadata; | ||
|
||
use ApiSkeletons\Doctrine\GraphQL\Config; | ||
use ApiSkeletons\Doctrine\GraphQL\Hydrator\Strategy; | ||
|
||
use function str_replace; | ||
use function strlen; | ||
use function strpos; | ||
use function substr; | ||
|
||
/** | ||
* This ancestor class contains functions common to the MetadataFactory | ||
* and GlobalEnable | ||
*/ | ||
abstract class AbstractMetadataFactory | ||
{ | ||
protected Config $config; | ||
|
||
protected 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; | ||
} | ||
} | ||
|
||
/** | ||
* Compute the GraphQL type name | ||
*/ | ||
protected function getTypeName(string $entityClass): string | ||
{ | ||
return $this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiSkeletons\Doctrine\GraphQL\Metadata; | ||
|
||
use ApiSkeletons\Doctrine\GraphQL\Config; | ||
use ApiSkeletons\Doctrine\GraphQL\Hydrator\Strategy; | ||
use Doctrine\ORM\EntityManager; | ||
|
||
use function in_array; | ||
|
||
final class GlobalEnable extends AbstractMetadataFactory | ||
{ | ||
/** @var mixed[] */ | ||
private array $metadataConfig = []; | ||
|
||
public function __construct( | ||
private EntityManager $entityManager, | ||
protected Config $config, | ||
) { | ||
} | ||
|
||
/** | ||
* @param string[] $entityClasses | ||
* | ||
* @return mixed[] | ||
*/ | ||
public function __invoke(array $entityClasses): array | ||
{ | ||
$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; | ||
} | ||
} | ||
|
||
return $this->metadataConfig; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters