Skip to content

Commit

Permalink
Merge pull request #27 from irontec/PROVIDER-1420-orm-mapping-generat…
Browse files Browse the repository at this point in the history
…or-should-update

Orm mapping generator update entity feature
  • Loading branch information
mmadariaga authored Jan 23, 2024
2 parents dec35b7 + 84a6d3e commit a620b32
Show file tree
Hide file tree
Showing 11 changed files with 730 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace IvozDevTools\EntityGeneratorBundle\Doctrine\Mapping\Generator;

use IvozDevTools\EntityGeneratorBundle\Doctrine\Mapping\RequestedProperty;

class ConstraintsGenerator
{
public function __construct()
{
}

/**
* @param RequestedProperty[] $data
*/
public function execute(\SimpleXMLElement $xml, array $data): \SimpleXMLElement
{
/** @var \SimpleXMLElement $mappedSuperClass */
$mappedSuperClass = $xml->{'mapped-superclass'};
/** @var \SimpleXMLElement $uniqueConstraints */
$uniqueConstraints = $mappedSuperClass->{'unique-constraints'};
/** @var \SimpleXMLElement $uniqueConstraint */
$uniqueConstraint = $uniqueConstraints->{'unique-constraint'};

if (!$uniqueConstraint) {
$uniqueConstraints = $mappedSuperClass->addChild('unique-constraints');
}

foreach ($data as $constraint) {
$uniqueConstraint = $uniqueConstraints->addChild('unique-constraint');
$uniqueConstraint->addAttribute('name', $constraint->getFieldName());
$uniqueConstraint->addAttribute('columns', $constraint->getUniqueConstraintColumns());
}

return $xml;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace IvozDevTools\EntityGeneratorBundle\Doctrine\Mapping\Generator;

use IvozDevTools\EntityGeneratorBundle\Doctrine\Mapping\RequestedProperty;

class FieldsGenerator
{

public function __construct()
{
}

/**
* @param RequestedProperty[] $data
*/
public function execute(\SimpleXMLElement $xml, array $data): \SimpleXMLElement
{
$mappedSuperClass = $xml->{'mapped-superclass'};
$options = null;
foreach ($data as $field) {

/** @var \SimpleXMLElement $item */
$item = $mappedSuperClass->addChild('field');
$item->addAttribute('name', $field->getFieldName());
$item->addAttribute('type', $field->getType());
$item->addAttribute('column', $field->getFieldName());

if ($field->getType() === 'string') {
$item->addAttribute('length', (string)$field->getLength());
$options = $item->addChild('options');
$option = $options->addchild('option');
$option->addAttribute('name', 'fixed');

if ($field->getComment()) {
$option = $options->addchild(
'option',
sprintf(
'[enum:%s]',
$field->getComment()
)
);
$option->addAttribute('name', 'comment');
}
}

if ($field->getType() === 'integer') {
if ($field->isUnsigned()) {
$options = $item->addChild('options');
$option = $options->addchild('option', (string)$field->isUnsigned());
$option->addAttribute('name', 'unsigned');
$option = $options->addchild('option');
$option->addAttribute('name', 'fixed');
}
}

if ($field->getType() === 'decimal' || $field->getType() === 'float') {
$item->addAttribute('precision', (string)$field->getPrecision());
$item->addAttribute('scale', (string)$field->getScale());
}

$item->addAttribute('nullable', $field->getNullable());

if ($field->getDefault()) {
if (!$options) {
$options = $item->addChild('options');
}
$option = $options->addchild('option', $field->getDefault());
$option->addAttribute('name', 'default');
}
}

return $xml;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

namespace IvozDevTools\EntityGeneratorBundle\Doctrine\Mapping\Generator;

use IvozDevTools\EntityGeneratorBundle\Doctrine\Mapping\MappedEntityRelation;
use IvozDevTools\EntityGeneratorBundle\Doctrine\Mapping\MappingGenerator;
use IvozDevTools\EntityGeneratorBundle\Doctrine\Mapping\RequestedProperty;

class RelationsGenerator
{
public function __construct(
private MappingGenerator $generator,
private string $mappingName
) {
}

/**
* @param RequestedProperty[] $data
*/
public function execute(\SimpleXMLElement $xml, array $data): \SimpleXMLElement
{
$mappedSuperClass = $xml->{'mapped-superclass'};
foreach ($data as $field) {
$relation = $field->getRelation();

$inverseClass = $relation->getInverseClass();
$inverseClassSegments = explode("\\", $inverseClass);
$className = end($inverseClassSegments);

$inverseRelation = sprintf(
'%s\\%sInterface',
$inverseClass,
$className
);

switch ($relation->getType()) {
case MappedEntityRelation::MANY_TO_ONE:
/** @var \SimpleXMLElement $manyToOne */
$manyToOne = $mappedSuperClass->addChild('many-to-one');

$manyToOne->addAttribute('field', $field->getFieldName());
$manyToOne->addAttribute('target-entity', $inverseRelation);
$manyToOne->addAttribute('fetch', $field->getFetch());

$joinColumns = $manyToOne->addChild('join-columns');
$joinColumn = $joinColumns->addChild('join-column');
$entityId = sprintf('%sId', $relation->getOwningProperty());
$joinColumn->addAttribute('name', $entityId);
$joinColumn->addAttribute('referenced-column-name', 'id');
$joinColumn->addAttribute('on-delete', $field->getOnDelete());

if ($relation->isNullable()) {
$joinColumn->addAttribute('nullable', (string)$relation->isNullable());
}

if ($relation->getMapInverseRelation()) {
$this->generateInversedRelation(
$relation,
$field->getFetch()
);
}
break;
case MappedEntityRelation::ONE_TO_MANY:
/** @var \SimpleXMLElement $oneToMany */
$oneToMany = $mappedSuperClass->addChild('one-to-many');

$oneToMany->addAttribute('field', $field->getFieldName());
$oneToMany->addAttribute('target-entity', $inverseRelation);
$oneToMany->addAttribute('fetch', $field->getFetch());

$joinColumns = $oneToMany->addChild('join-columns');
$joinColumn = $joinColumns->addChild('join-column');
$entityId = sprintf('%sId', $relation->getOwningProperty());
$joinColumn->addAttribute('name', $entityId);
$joinColumn->addAttribute('referenced-column-name', 'id');
$joinColumn->addAttribute('on-delete', $field->getOnDelete());

if ($relation->isNullable()) {
$joinColumn->addAttribute('nullable', (string)$relation->isNullable());
}

if ($relation->getMapInverseRelation()) {
$this->generateInversedRelation(
$relation,
$field->getFetch()
);
}
break;
case MappedEntityRelation::ONE_TO_ONE:
/** @var \SimpleXMLElement $oneToOne */
$oneToOne = $mappedSuperClass->addChild('one-to-one');

$oneToOne->addAttribute('field', $field->getFieldName());
$oneToOne->addAttribute('target-entity', $inverseRelation);
$oneToOne->addAttribute('inversed-by', strtolower($relation->getOwningClass()));
$oneToOne->addAttribute('fetch', $field->getFetch());

$joinColumns = $oneToOne->addChild('join-columns');
$joinColumn = $joinColumns->addChild('join-column');
$entityId = sprintf('%sId', $relation->getOwningProperty());
$joinColumn->addAttribute('name', $entityId);
$joinColumn->addAttribute('referenced-column-name', 'id');
$joinColumn->addAttribute('on-delete', $field->getOnDelete());

if ($relation->isNullable()) {
$joinColumn->addAttribute('nullable', (string)$relation->isNullable());
}

if ($relation->getMapInverseRelation()) {
$this->generateInversedRelation(
$relation,
$field->getFetch(),
);
}
break;
}
}

return $xml;
}

private function generateInversedRelation(
MappedEntityRelation $relation,
string $fetch
) {
$mappingPaths = $this->generator->getMappingsPath();
$owningClass = $relation->getOwningClass();
$inverseClass = $relation->getInverseClass();
$inverseProperty = $relation->getInverseProperty();
$owningProperty = $relation->getOwningProperty();

$inverseClassSegments = explode(
"\\",
$inverseClass
);

$output = sprintf(
'%s/%s.%s.orm.xml',
$mappingPaths[$relation->getInversedProjectName()],
end($inverseClassSegments),
end($inverseClassSegments),
);

$xml = $this
->generator
->readXmlFile($output);

if (!$xml) {
throw new \Exception("File not found " . $output);
}

$aliasMappingPaths = $this->generator->getAliasMappingPaths();
$targetEntity = sprintf(
'%s\\%s\\%sInterface',
$aliasMappingPaths[$this->mappingName],
$owningClass,
$owningClass
);

$entity = $xml->{'entity'};
$oneToMany = $entity->addChild('one-to-many');
$oneToMany->addAttribute('field', $inverseProperty);
$oneToMany->addAttribute('target-entity', $targetEntity);
$oneToMany->addAttribute('mapped-by', $owningProperty);
$oneToMany->addAttribute('fetch', $fetch);

$this->generator->generateXml(
$xml,
$output
);
}

}
Loading

0 comments on commit a620b32

Please sign in to comment.