Skip to content

Commit

Permalink
Merge pull request #25 from irontec/use-latest-ivoz-core-enhancements
Browse files Browse the repository at this point in the history
Use latest ivoz core enhancements
  • Loading branch information
mmadariaga authored Nov 29, 2023
2 parents 4d00e2f + 5f1b013 commit 88398e1
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 852 deletions.
25 changes: 22 additions & 3 deletions EntityGeneratorBundle/Doctrine/Dto/DtoManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PhpParser\Parser;
use Symfony\Bundle\MakerBundle\Doctrine\BaseCollectionRelation;
use Symfony\Bundle\MakerBundle\Doctrine\BaseSingleRelation;
use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper;
use Symfony\Bundle\MakerBundle\Doctrine\RelationManyToMany;
use Symfony\Bundle\MakerBundle\Doctrine\RelationManyToOne;
use Symfony\Bundle\MakerBundle\Doctrine\RelationOneToMany;
Expand Down Expand Up @@ -49,8 +50,10 @@ final class DtoManipulator implements ManipulatorInterface
/** @var UseStatement[] */
private $useStatements = [];

public function __construct(string $sourceCode)
{
public function __construct(
string $sourceCode,
private DoctrineHelper $doctrineHelper,
) {
$this->lexer = new Lexer\Emulative([
'usedAttributes' => [
'comments',
Expand Down Expand Up @@ -418,9 +421,25 @@ private function addSingularRelation(BaseSingleRelation $relation, $classMetadat
$classMetadata,
);

$targetClassName = substr(
$relation->getTargetClassName(),
0,
-3
);

/** @var \IvozDevTools\EntityGeneratorBundle\Doctrine\Metadata\ClassMetadata $targetClassMetadata */
$targetClassMetadata = $this->doctrineHelper->getMetadata($targetClassName);
$identifier = $targetClassMetadata->getIdentifier();
$targetPkField = $targetClassMetadata->getFieldMapping(
current($identifier)
);
$targetPkHint = $this->getEntityTypeHint(
$targetPkField["type"]
);

$this->addIdGetter(
$relation->getPropertyName(),
$relation->getCustomReturnType() ?: $typeHint,
$relation->getCustomReturnType() ?: $targetPkHint,
true,
[]
);
Expand Down
3 changes: 2 additions & 1 deletion EntityGeneratorBundle/Doctrine/Dto/DtoRegenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ private function createClassManipulator(
{
$classContent = $content ?? $this->fileManager->getFileContents($classPath);
return new DtoManipulator(
$classContent
$classContent,
$this->doctrineHelper
);
}

Expand Down
11 changes: 9 additions & 2 deletions EntityGeneratorBundle/Doctrine/Dto/FkGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ class FkGetter extends Getter
public function toString(string $nlLeftPad = ''): string
{
$methodName = 'get' . Str::asCamelCase($this->propertyName);
$returnHint = '';
if ($this->returnType && strpos($this->returnType, '|')) {
$returnHint = ': ' . $this->returnType . '|null';
} elseif ($this->returnType) {
$returnHint = ': ?' . $this->returnType;
}

$response = [];
$response[] = sprintf(
'public function %sId()',
$methodName
'public function %sId()%s',
$methodName,
$returnHint
);
$response[] = '{';
$response[] = ' if ($dto = $this->' . $methodName . '()) {';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ private function getAssociationMappings(ClassMetadata $classMetadata): array
$classMetadata->associationMappings,
function ($property) {
return in_array(
$property['type'] ?? null,
$property['type'],
[
ClassMetadata::ONE_TO_MANY,
ClassMetadata::MANY_TO_MANY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private function getMappedFieldsInEntity(ClassMetadata $classMetadata)
$classMetadata->associationMappings,
function ($mapping) {
return in_array(
$mapping['type'] ?? null,
$mapping['type'],
[
ClassMetadata::ONE_TO_ONE,
ClassMetadata::ONE_TO_MANY,
Expand Down
3 changes: 2 additions & 1 deletion EntityGeneratorBundle/Doctrine/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

class Property implements CodeGeneratorUnitInterface
{
/** @var mixed | null */
private $defaultValue;

public function __construct(
Expand Down Expand Up @@ -130,7 +131,7 @@ public function toString(string $nlLeftPad = ''): string
: 'false';
break;
case 'string':
if (!is_null($this->defaultValue)) {
if (!empty($this->defaultValue)) {
$defaultValue = '\'' . $this->defaultValue . '\'';
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,18 @@ public function makeEmptyInterface($classMetadata)
return;
}

$entityClassNameSegments = explode("\\", $classMetadata->name);
$entityClassName = end($entityClassNameSegments);

$classMetadata->name = $fqdn;
$classMetadata->rootEntityName = $fqdn;

[$classPath, $content] = $this->getDoctrineClassTemplate(
$classMetadata,
'doctrine/RepositoryInterface.tpl.php',
[]
[
'entity_classname' => $entityClassName
]
);

$manipulator = $this->createClassManipulator(
Expand Down Expand Up @@ -140,38 +145,6 @@ private function getDoctrineClassTemplate(
];
}

private function getClassTemplate(
ClassMetadata $metadata,
$templateName
): array
{
[$path, $variables] = $this->generator->generateClassContentVariables(
$metadata->name,
$templateName,
[]
);

if (file_exists($variables['relative_path'])) {
$variables['relative_path'] = realpath($variables['relative_path']);
} else {
$variables['relative_path'] = str_replace(
'vendor/composer/../../',
'',
$variables['relative_path']
);
}


return [
$variables['relative_path'],
$this->fileManager->parseTemplate(
$path,
$variables
)
];
}


private function createClassManipulator(
string $classPath,
?string $content
Expand Down
Loading

0 comments on commit 88398e1

Please sign in to comment.