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.

307 lines
15 KiB

5 years ago
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: mapescador
  5. * Date: 24/02/20
  6. * Time: 13:35
  7. */
  8. namespace App\Command;
  9. use Doctrine\Common\Annotations\AnnotationReader as Reader;
  10. use Symfony\Component\Console\Command\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Input\InputOption;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Symfony\Component\DependencyInjection\ContainerInterface;
  17. use ReflectionClass;
  18. class ImportEntitiesCommand extends Command
  19. {
  20. protected static $defaultName = 'prometeo:importentities';
  21. protected static $doctrine = 'Doctrine\ORM\Mapping';
  22. protected function configure()
  23. {
  24. $this
  25. // the short description shown while running "php bin/console list"
  26. ->setDescription('Import Entities to Btm.')
  27. // the full command description shown when running the command with
  28. // the "--help" option
  29. ->setHelp('This command allows you to convert Import existing structure.');
  30. }
  31. protected function execute(InputInterface $input, OutputInterface $output)
  32. {
  33. $output->writeln([
  34. 'Import converter',
  35. '============================',
  36. '',
  37. ]);
  38. $btm = 'ErModel.btm';
  39. $folderentity = 'src/Entity';
  40. $btmdata = [];
  41. $ermapping = [];
  42. $width=100;
  43. $height=100;
  44. foreach (glob($folderentity . "/*.php") as $nombre_fichero) {
  45. $tokenize = explode('/', $nombre_fichero);
  46. $clase = preg_replace('/\\.[^.\\s]{3,4}$/', '', end($tokenize));
  47. $reader = new Reader();
  48. $ref = new ReflectionClass('App\Entity\\' . $clase);
  49. $this->parseClass($reader, $ref, $ermapping, $btmdata, $width, $height);
  50. }
  51. foreach (glob($folderentity . "/*.php") as $nombre_fichero) {
  52. $tokenize = explode('/', $nombre_fichero);
  53. $clase = preg_replace('/\\.[^.\\s]{3,4}$/', '', end($tokenize));
  54. $reader = new Reader();
  55. $ref = new ReflectionClass('App\Entity\\' . $clase);
  56. $this->parseRelations($reader, $ref, $ermapping, $btmdata);
  57. }
  58. file_put_contents($btm, json_encode($btmdata));
  59. return 0;
  60. }
  61. protected function parseClass(Reader $reader, ReflectionClass $ref, array &$ermapping, array &$btmdata,&$width, &$height)
  62. {
  63. if ($reader->getClassAnnotation($ref, 'Doctrine\ORM\Mapping\Entity')) {
  64. $entity = new \stdClass();
  65. $entity->type = 'TableShape';
  66. $entity->cssClass = 'TableShape';
  67. $entity->stroke = 1;
  68. $entity->radius = 3;
  69. $entity->alpha = 1;
  70. $entity->angle = 0;
  71. $entity->id = md5($ref->getName());
  72. $entity->name = $ref->getName();
  73. $entity->userData = new \stdClass();
  74. $ermapping[base64_encode($entity->name)] = [];
  75. $ermapping[base64_encode($entity->name)]['uuid'] = md5($ref->getName());
  76. foreach ($ref->getProperties() as $property) {
  77. $ermapping[base64_encode($entity->name)][$property->getName()] = [];
  78. }
  79. $entity->entities = [];
  80. $entity->ports = [];
  81. foreach ($ref->getProperties() as $field) {
  82. $id = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\Id');
  83. $column = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\Column');
  84. $autoincrement = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\GeneratedValue');
  85. //$reader->getClassAnnotation($ref, 'Doctrine\ORM\Mapping\Entity');
  86. $fieldid = md5($field->getName());
  87. $fieldid =$field->getName();
  88. $ermapping[base64_encode($entity->name)][$fieldid[0]] = [];
  89. $inputport = new \stdClass();
  90. $inputport->type = 'draw2d.InputPort';
  91. $inputport->port = 'draw2d.InputPort';
  92. $inputport->name = 'input_' . $fieldid;
  93. $inputport->id = 'input_' . $fieldid;
  94. $inputport->userData = new \stdClass();
  95. $inputport->width = 10;
  96. $inputport->height = 10;
  97. $inputport->alpha = 1;
  98. $inputport->angle = 0;
  99. $inputport->cssClass = 'draw2d_InputPort';
  100. $inputport->bgColor = "#4F6870";
  101. $inputport->color = "#1B1B1B";
  102. $inputport->locator = "draw2d.layout.locator.InputPortLocator";
  103. $inputport->stroke = 1;
  104. $inputport->dasharray = null;
  105. $inputport->maxFanOut = 9007199254740991;
  106. $ermapping[base64_encode($entity->name)][$fieldid]['inputport'] = $inputport->id;
  107. $outputport = new \stdClass();
  108. $outputport->type = 'draw2d.OutputPort';
  109. $outputport->port = 'draw2d.OutputPort';
  110. $outputport->name = 'output_' . $fieldid;
  111. $outputport->id = 'output_' . $fieldid;
  112. $outputport->userData = new \stdClass();
  113. $outputport->width = 10;
  114. $outputport->height = 10;
  115. $outputport->alpha = 1;
  116. $outputport->angle = 0;
  117. $outputport->cssClass = 'draw2d_OutputPort';
  118. $outputport->bgColor = "#4F6870";
  119. $outputport->color = "#1B1B1B";
  120. $outputport->locator = "draw2d.layout.locator.OutputPortLocator";
  121. $outputport->stroke = 1;
  122. $outputport->dasharray = null;
  123. $outputport->maxFanOut = 9007199254740991;
  124. echo "\nentityname==>$entity->name\n";
  125. $ermapping[base64_encode($entity->name)][$fieldid]['outputport'] = $outputport->id;
  126. if ($column) {
  127. $entityfield = new \stdClass();
  128. $entityfield->text = $field->getName();
  129. $entityfield->id = $fieldid;
  130. $entityfield->primary = ($id) ? ["true"] : ["false"];
  131. $entityfield->type = ($column->type) ? $column->type : 'string';
  132. $entityfield->required = ($column->nullable) ? ["false"] : ["true"];
  133. $entityfield->unique = ["false"];
  134. $entityfield->unsigned = ["false"];
  135. $entityfield->auto = ($autoincrement) ? ["true"] : ["false"];
  136. $entity->ports[] = $inputport;
  137. $entity->ports[] = $outputport;
  138. $entity->entities[] = $entityfield;
  139. $entity->bgColor="#FFFFFF";
  140. $entity->color="#D2D2D2";
  141. $width=$width+100;
  142. $entity->x=$width;
  143. $entity->y=$height;
  144. $entity->width=202;
  145. $entity->height=115;
  146. }else{
  147. //relation
  148. $entityfield = new \stdClass();
  149. $entityfield->text = $field->getName();
  150. $entityfield->id = $fieldid;
  151. // $entityfield->primary = ($id) ? ["true"] : ["false"];
  152. // $entityfield->type = ($column->type) ? $column->type : 'string';
  153. // $entityfield->required = ($column->nullable) ? ["false"] : ["true"];
  154. // $entityfield->unique = ["false"];
  155. // $entityfield->unsigned = ["false"];
  156. // $entityfield->auto = ($autoincrement) ? ["true"] : ["false"];
  157. $entity->ports[] = $inputport;
  158. $entity->ports[] = $outputport;
  159. $entity->entities[] = $entityfield;
  160. $entity->bgColor="#FFFFFF";
  161. $entity->color="#000000";
  162. $width=$width+100;
  163. $entity->x=$width;
  164. $entity->y=$height;
  165. $entity->width=202;
  166. $entity->height=115;
  167. }
  168. }
  169. }
  170. $btmdata[] = $entity;
  171. }
  172. protected function parseRelations(Reader $reader, ReflectionClass $ref, array &$ermapping, array &$btmdata)
  173. {
  174. echo 'inicia relaciones';
  175. if ($reader->getClassAnnotation($ref, 'Doctrine\ORM\Mapping\Entity')) {
  176. foreach ($ref->getProperties() as $field) {
  177. echo 'inicia properties '.$ref->getName();
  178. echo print_r($reader->getPropertyAnnotations($field));
  179. $relation = false;
  180. $targetEntity = null;
  181. $relationtype = 0;
  182. $onetoone = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\OneToOne');
  183. $onetomany = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\OneToMany');
  184. $manytoone = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\ManyToOne');
  185. $manytomany = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\ManyToMany');
  186. if ($onetoone) {
  187. $relation = true;
  188. $relationtype = 1;
  189. $targetEntity = $onetoone->targetEntity;
  190. } elseif ($onetomany) {
  191. $relation = true;
  192. $relationtype = 2;
  193. $targetEntity = $onetomany->targetEntity;
  194. } elseif ($manytoone) {
  195. $relation = true;
  196. $relationtype = 3;
  197. $targetEntity = $manytoone->targetEntity;
  198. } elseif ($manytomany) {
  199. $relation = true;
  200. $relationtype = 4;
  201. $targetEntity = $manytomany->targetEntity;
  202. }
  203. // Metemos las relaciones
  204. if ($relation) {
  205. echo 'hay una relacion de tipo ->'.$relationtype.' con '.$targetEntity;
  206. $connection = new \stdClass();
  207. $connection->type = 'draw2d.Connection';
  208. $connection->id = md5(md5($ref->getName()) . $targetEntity);
  209. $connection->cssClass = 'draw2d_Connection';
  210. $connection->policy = "draw2d.policy.line.OrthogonalSelectionFeedbackPolicy";
  211. $connection->router = "draw2d.layout.connection.InteractiveManhattanConnectionRouter";
  212. $connection->source = new \stdClass();
  213. $connection->target = new \stdClass();
  214. $join = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\JoinColumn');
  215. $jointable = $reader->getPropertyAnnotation($field, 'Doctrine\ORM\Mapping\JoinTable');
  216. if ($jointable) {
  217. if (empty($jointable->name)) {
  218. $jointable->name = strtolower($ref->getName() . '_' . $targetEntity);
  219. }
  220. $entity = new \stdClass();
  221. $entity->type = 'TableShape';
  222. $entity->cssClass = 'TableShape';
  223. $entity->stroke = 1;
  224. $entity->radius = 3;
  225. $entity->alpha = 1;
  226. $entity->angle = 0;
  227. $entity->id = md5($jointable->name);
  228. $entity->name = $jointable->name;
  229. $entity->userData = new \stdClass();
  230. $ermapping[base64_encode($entity->name)] = [];
  231. $ermapping[base64_encode($entity->name)]['uuid'] = md5($jointable->name);
  232. $entity->entities = [];
  233. $entity->ports = [];
  234. }
  235. $fieldfrom = md5($field->getName());
  236. $fieldfrom = $field->getName();
  237. switch ($relationtype) {
  238. case 1:
  239. if($onetoone->mappedBy){
  240. $fieldto = $onetoone->mappedBy;
  241. }elseif($join->referencedColumnName){
  242. $fieldto =$join->referencedColumnName;
  243. }else{
  244. $fieldto ='id';
  245. }
  246. $connection->source->decoration = "draw2d.decoration.connection.BarDecorator";
  247. $connection->target->decoration = "draw2d.decoration.connection.BarDecorator";
  248. $connection->color = '#00A8F0';
  249. break;
  250. case 2:
  251. $fieldto = $onetomany->mappedBy;
  252. $connection->source->decoration = "draw2d.decoration.connection.BarDecorator";
  253. $connection->target->decoration = "draw2d.decoration.connection.DiamondDecorator";
  254. $connection->color = '#b9dd69';
  255. break;
  256. case 3:
  257. $fieldto = $manytoone->inversedBy;
  258. $connection->source->decoration = "draw2d.decoration.connection.DiamondDecorator";
  259. $connection->target->decoration = "draw2d.decoration.connection.BarDecorator";
  260. $connection->color = '#b9dd69';
  261. break;
  262. case 4:
  263. $fieldto = $jointable->referencedColumnName;
  264. $connection->source->decoration = "draw2d.decoration.connection.DiamondDecorator";
  265. $connection->target->decoration = "draw2d.decoration.connection.DiamondDecorator";
  266. $connection->color = '#f3546a';
  267. break;
  268. default:
  269. break;
  270. }
  271. $from = $ref->getName();
  272. echo "\nentitynameconnection==>$targetEntity\n";
  273. $to ='App\Entity\\'.$targetEntity;
  274. if(empty($ermapping[base64_encode($to)]))continue;
  275. $connection->source->node = $ermapping[base64_encode($from)]['uuid'];
  276. // $connection->source->port=$ermapping[base64_encode($from)][$fieldfrom]=array();
  277. $connection->source->port = $ermapping[base64_encode($from)][$fieldfrom]['inputport'];
  278. $connection->target = new \stdClass();
  279. echo "\n\n\n Fieldto ===>".$fieldto."\n\n";
  280. $connection->target->node = $ermapping[base64_encode($to)]['uuid'];
  281. //$connection->target->port = $ermapping[base64_encode($to)][md5($fieldto)]['outputport'];
  282. $connection->target->port = $ermapping[base64_encode($to)][$fieldto]['outputport'];
  283. echo 'añadimos relacion';
  284. $btmdata[] = $connection;
  285. }
  286. }
  287. }
  288. }
  289. }