PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Generator/DoctrineEntityGenerator.php

https://gitlab.com/mohamedchiheb.bida/workshopFOS
PHP | 119 lines | 93 code | 11 blank | 15 comment | 5 complexity | e317f26109b0703b2c96b9739d6e655a MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sensio\Bundle\GeneratorBundle\Generator;
  11. use Symfony\Component\Filesystem\Filesystem;
  12. use Symfony\Component\HttpKernel\Bundle\BundleInterface;
  13. use Symfony\Bridge\Doctrine\RegistryInterface;
  14. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  15. use Doctrine\ORM\Tools\EntityGenerator;
  16. use Doctrine\ORM\Tools\EntityRepositoryGenerator;
  17. use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
  18. /**
  19. * Generates a Doctrine entity class based on its name, fields and format.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. * @author Jonathan H. Wage <jonwage@gmail.com>
  23. */
  24. class DoctrineEntityGenerator extends Generator
  25. {
  26. private $filesystem;
  27. private $registry;
  28. public function __construct(Filesystem $filesystem, RegistryInterface $registry)
  29. {
  30. $this->filesystem = $filesystem;
  31. $this->registry = $registry;
  32. }
  33. public function generate(BundleInterface $bundle, $entity, $format, array $fields, $withRepository)
  34. {
  35. // configure the bundle (needed if the bundle does not contain any Entities yet)
  36. $config = $this->registry->getManager(null)->getConfiguration();
  37. $config->setEntityNamespaces(array_merge(
  38. array($bundle->getName() => $bundle->getNamespace().'\\Entity'),
  39. $config->getEntityNamespaces()
  40. ));
  41. $entityClass = $this->registry->getAliasNamespace($bundle->getName()).'\\'.$entity;
  42. $entityPath = $bundle->getPath().'/Entity/'.str_replace('\\', '/', $entity).'.php';
  43. if (file_exists($entityPath)) {
  44. throw new \RuntimeException(sprintf('Entity "%s" already exists.', $entityClass));
  45. }
  46. $class = new ClassMetadataInfo($entityClass);
  47. if ($withRepository) {
  48. $class->customRepositoryClassName = $entityClass.'Repository';
  49. }
  50. $class->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
  51. $class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
  52. foreach ($fields as $field) {
  53. $class->mapField($field);
  54. }
  55. $entityGenerator = $this->getEntityGenerator();
  56. if ('annotation' === $format) {
  57. $entityGenerator->setGenerateAnnotations(true);
  58. $entityCode = $entityGenerator->generateEntityClass($class);
  59. $mappingPath = $mappingCode = false;
  60. } else {
  61. $cme = new ClassMetadataExporter();
  62. $exporter = $cme->getExporter('yml' == $format ? 'yaml' : $format);
  63. $mappingPath = $bundle->getPath().'/Resources/config/doctrine/'.str_replace('\\', '.', $entity).'.orm.'.$format;
  64. if (file_exists($mappingPath)) {
  65. throw new \RuntimeException(sprintf('Cannot generate entity when mapping "%s" already exists.', $mappingPath));
  66. }
  67. $mappingCode = $exporter->exportClassMetadata($class);
  68. $entityGenerator->setGenerateAnnotations(false);
  69. $entityCode = $entityGenerator->generateEntityClass($class);
  70. }
  71. $this->filesystem->mkdir(dirname($entityPath));
  72. file_put_contents($entityPath, $entityCode);
  73. if ($mappingPath) {
  74. $this->filesystem->mkdir(dirname($mappingPath));
  75. file_put_contents($mappingPath, $mappingCode);
  76. }
  77. if ($withRepository) {
  78. $path = $bundle->getPath().str_repeat('/..', substr_count(get_class($bundle), '\\'));
  79. $this->getRepositoryGenerator()->writeEntityRepositoryClass($class->customRepositoryClassName, $path);
  80. }
  81. }
  82. public function isReservedKeyword($keyword)
  83. {
  84. return $this->registry->getConnection()->getDatabasePlatform()->getReservedKeywordsList()->isKeyword($keyword);
  85. }
  86. protected function getEntityGenerator()
  87. {
  88. $entityGenerator = new EntityGenerator();
  89. $entityGenerator->setGenerateAnnotations(false);
  90. $entityGenerator->setGenerateStubMethods(true);
  91. $entityGenerator->setRegenerateEntityIfExists(false);
  92. $entityGenerator->setUpdateEntityIfExists(true);
  93. $entityGenerator->setNumSpaces(4);
  94. $entityGenerator->setAnnotationPrefix('ORM\\');
  95. return $entityGenerator;
  96. }
  97. protected function getRepositoryGenerator()
  98. {
  99. return new EntityRepositoryGenerator();
  100. }
  101. }