Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use latest ivoz core enhancements #25

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading