Licitator 1.0
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

308 lines
15 KiB

<?php
/**
* Created by PhpStorm.
* User: mapescador
* Date: 24/02/20
* Time: 13:35
*/
namespace App\Command;
use Doctrine\Common\Annotations\AnnotationReader as Reader;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use ReflectionClass;
class ImportEntitiesCommand extends Command
{
protected static $defaultName = 'prometeo:importentities';
protected static $doctrine = 'Doctrine\ORM\Mapping';
protected function configure()
{
$this
// the short description shown while running "php bin/console list"
->setDescription('Import Entities to Btm.')
// the full command description shown when running the command with
// the "--help" option
->setHelp('This command allows you to convert Import existing structure.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln([
'Import converter',
'============================',
'',
]);
$btm = 'ErModel.btm';
$folderentity = 'src/Entity';
$btmdata = [];
$ermapping = [];
$width=100;
$height=100;
foreach (glob($folderentity . "/*.php") as $nombre_fichero) {
$tokenize = explode('/', $nombre_fichero);
$clase = preg_replace('/\\.[^.\\s]{3,4}$/', '', end($tokenize));
$reader = new Reader();
$ref = new ReflectionClass('App\Entity\\' . $clase);
$this->parseClass($reader, $ref, $ermapping, $btmdata, $width, $height);
}
foreach (glob($folderentity . "/*.php") as $nombre_fichero) {
$tokenize = explode('/', $nombre_fichero);
$clase = preg_replace('/\\.[^.\\s]{3,4}$/', '', end($tokenize));
$reader = new Reader();
$ref = new ReflectionClass('App\Entity\\' . $clase);
$this->parseRelations($reader, $ref, $ermapping, $btmdata);
}
file_put_contents($btm, json_encode($btmdata));
return 0;
}
protected function parseClass(Reader $reader, ReflectionClass $ref, array &$ermapping, array &$btmdata,&$width, &$height)
{
if ($reader->getClassAnnotation($ref, 'Doctrine\ORM\Mapping\Entity')) {
$entity = new \stdClass();
$entity->type = 'TableShape';
$entity->cssClass = 'TableShape';
$entity->stroke = 1;
$entity->radius = 3;
$entity->alpha = 1;
$entity->angle = 0;
$entity->id = md5($ref->getName());
$entity->name = $ref->getName();
$entity->userData = new \stdClass();
$ermapping[base64_encode($entity->name)] = [];
$ermapping[base64_encode($entity->name)]['uuid'] = md5($ref->getName());
foreach ($ref->getProperties() as $property) {
$ermapping[base64_encode($entity->name)][$property->getName()] = [];
}
$entity->entities = [];
$entity->ports = [];
foreach ($ref->getProperties() as $field) {
$id = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\Id');
$column = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\Column');
$autoincrement = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\GeneratedValue');
//$reader->getClassAnnotation($ref, 'Doctrine\ORM\Mapping\Entity');
$fieldid = md5($field->getName());
$fieldid =$field->getName();
$ermapping[base64_encode($entity->name)][$fieldid[0]] = [];
$inputport = new \stdClass();
$inputport->type = 'draw2d.InputPort';
$inputport->port = 'draw2d.InputPort';
$inputport->name = 'input_' . $fieldid;
$inputport->id = 'input_' . $fieldid;
$inputport->userData = new \stdClass();
$inputport->width = 10;
$inputport->height = 10;
$inputport->alpha = 1;
$inputport->angle = 0;
$inputport->cssClass = 'draw2d_InputPort';
$inputport->bgColor = "#4F6870";
$inputport->color = "#1B1B1B";
$inputport->locator = "draw2d.layout.locator.InputPortLocator";
$inputport->stroke = 1;
$inputport->dasharray = null;
$inputport->maxFanOut = 9007199254740991;
$ermapping[base64_encode($entity->name)][$fieldid]['inputport'] = $inputport->id;
$outputport = new \stdClass();
$outputport->type = 'draw2d.OutputPort';
$outputport->port = 'draw2d.OutputPort';
$outputport->name = 'output_' . $fieldid;
$outputport->id = 'output_' . $fieldid;
$outputport->userData = new \stdClass();
$outputport->width = 10;
$outputport->height = 10;
$outputport->alpha = 1;
$outputport->angle = 0;
$outputport->cssClass = 'draw2d_OutputPort';
$outputport->bgColor = "#4F6870";
$outputport->color = "#1B1B1B";
$outputport->locator = "draw2d.layout.locator.OutputPortLocator";
$outputport->stroke = 1;
$outputport->dasharray = null;
$outputport->maxFanOut = 9007199254740991;
echo "\nentityname==>$entity->name\n";
$ermapping[base64_encode($entity->name)][$fieldid]['outputport'] = $outputport->id;
if ($column) {
$entityfield = new \stdClass();
$entityfield->text = $field->getName();
$entityfield->id = $fieldid;
$entityfield->primary = ($id) ? ["true"] : ["false"];
$entityfield->type = ($column->type) ? $column->type : 'string';
$entityfield->required = ($column->nullable) ? ["false"] : ["true"];
$entityfield->unique = ["false"];
$entityfield->unsigned = ["false"];
$entityfield->auto = ($autoincrement) ? ["true"] : ["false"];
$entity->ports[] = $inputport;
$entity->ports[] = $outputport;
$entity->entities[] = $entityfield;
$entity->bgColor="#FFFFFF";
$entity->color="#D2D2D2";
$width=$width+100;
$entity->x=$width;
$entity->y=$height;
$entity->width=202;
$entity->height=115;
}else{
//relation
$entityfield = new \stdClass();
$entityfield->text = $field->getName();
$entityfield->id = $fieldid;
// $entityfield->primary = ($id) ? ["true"] : ["false"];
// $entityfield->type = ($column->type) ? $column->type : 'string';
// $entityfield->required = ($column->nullable) ? ["false"] : ["true"];
// $entityfield->unique = ["false"];
// $entityfield->unsigned = ["false"];
// $entityfield->auto = ($autoincrement) ? ["true"] : ["false"];
$entity->ports[] = $inputport;
$entity->ports[] = $outputport;
$entity->entities[] = $entityfield;
$entity->bgColor="#FFFFFF";
$entity->color="#000000";
$width=$width+100;
$entity->x=$width;
$entity->y=$height;
$entity->width=202;
$entity->height=115;
}
}
}
$btmdata[] = $entity;
}
protected function parseRelations(Reader $reader, ReflectionClass $ref, array &$ermapping, array &$btmdata)
{
echo 'inicia relaciones';
if ($reader->getClassAnnotation($ref, 'Doctrine\ORM\Mapping\Entity')) {
foreach ($ref->getProperties() as $field) {
echo 'inicia properties '.$ref->getName();
echo print_r($reader->getPropertyAnnotations($field));
$relation = false;
$targetEntity = null;
$relationtype = 0;
$onetoone = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\OneToOne');
$onetomany = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\OneToMany');
$manytoone = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\ManyToOne');
$manytomany = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\ManyToMany');
if ($onetoone) {
$relation = true;
$relationtype = 1;
$targetEntity = $onetoone->targetEntity;
} elseif ($onetomany) {
$relation = true;
$relationtype = 2;
$targetEntity = $onetomany->targetEntity;
} elseif ($manytoone) {
$relation = true;
$relationtype = 3;
$targetEntity = $manytoone->targetEntity;
} elseif ($manytomany) {
$relation = true;
$relationtype = 4;
$targetEntity = $manytomany->targetEntity;
}
// Metemos las relaciones
if ($relation) {
echo 'hay una relacion de tipo ->'.$relationtype.' con '.$targetEntity;
$connection = new \stdClass();
$connection->type = 'draw2d.Connection';
$connection->id = md5(md5($ref->getName()) . $targetEntity);
$connection->cssClass = 'draw2d_Connection';
$connection->policy = "draw2d.policy.line.OrthogonalSelectionFeedbackPolicy";
$connection->router = "draw2d.layout.connection.InteractiveManhattanConnectionRouter";
$connection->source = new \stdClass();
$connection->target = new \stdClass();
$join = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\JoinColumn');
$jointable = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\JoinTable');
if ($jointable) {
if (empty($jointable->name)) {
$jointable->name = strtolower($ref->getName() . '_' . $targetEntity);
}
$entity = new \stdClass();
$entity->type = 'TableShape';
$entity->cssClass = 'TableShape';
$entity->stroke = 1;
$entity->radius = 3;
$entity->alpha = 1;
$entity->angle = 0;
$entity->id = md5($jointable->name);
$entity->name = $jointable->name;
$entity->userData = new \stdClass();
$ermapping[base64_encode($entity->name)] = [];
$ermapping[base64_encode($entity->name)]['uuid'] = md5($jointable->name);
$entity->entities = [];
$entity->ports = [];
}
$fieldfrom = md5($field->getName());
$fieldfrom = $field->getName();
switch ($relationtype) {
case 1:
if($onetoone->mappedBy){
$fieldto = $onetoone->mappedBy;
}elseif($join->referencedColumnName){
$fieldto =$join->referencedColumnName;
}else{
$fieldto ='id';
}
$connection->source->decoration = "draw2d.decoration.connection.BarDecorator";
$connection->target->decoration = "draw2d.decoration.connection.BarDecorator";
$connection->color = '#00A8F0';
break;
case 2:
$fieldto = $onetomany->mappedBy;
$connection->source->decoration = "draw2d.decoration.connection.BarDecorator";
$connection->target->decoration = "draw2d.decoration.connection.DiamondDecorator";
$connection->color = '#b9dd69';
break;
case 3:
$fieldto = $manytoone->inversedBy;
$connection->source->decoration = "draw2d.decoration.connection.DiamondDecorator";
$connection->target->decoration = "draw2d.decoration.connection.BarDecorator";
$connection->color = '#b9dd69';
break;
case 4:
$fieldto = $jointable->referencedColumnName;
$connection->source->decoration = "draw2d.decoration.connection.DiamondDecorator";
$connection->target->decoration = "draw2d.decoration.connection.DiamondDecorator";
$connection->color = '#f3546a';
break;
default:
break;
}
$from = $ref->getName();
echo "\nentitynameconnection==>$targetEntity\n";
$to ='App\Entity\\'.$targetEntity;
if(empty($ermapping[base64_encode($to)]))continue;
$connection->source->node = $ermapping[base64_encode($from)]['uuid'];
// $connection->source->port=$ermapping[base64_encode($from)][$fieldfrom]=array();
$connection->source->port = $ermapping[base64_encode($from)][$fieldfrom]['inputport'];
$connection->target = new \stdClass();
echo "\n\n\n Fieldto ===>".$fieldto."\n\n";
$connection->target->node = $ermapping[base64_encode($to)]['uuid'];
//$connection->target->port = $ermapping[base64_encode($to)][md5($fieldto)]['outputport'];
$connection->target->port = $ermapping[base64_encode($to)][$fieldto]['outputport'];
echo 'añadimos relacion';
$btmdata[] = $connection;
}
}
}
}
}