PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/doctrine-bundle/Command/GenerateEntitiesDoctrineCommand.php

https://bitbucket.org/MicHaeLan64/symfony
PHP | 135 lines | 107 code | 22 blank | 6 comment | 6 complexity | 47450c567ae7b10cf0f37d51b2bea7e9 MD5 | raw file
Possible License(s): LGPL-2.1, Unlicense, BSD-3-Clause
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle\Command;
  3. use Doctrine\Bundle\DoctrineBundle\Mapping\DisconnectedMetadataFactory;
  4. use Doctrine\ORM\Tools\EntityRepositoryGenerator;
  5. use Symfony\Component\Console\Input\InputArgument;
  6. use Symfony\Component\Console\Input\InputInterface;
  7. use Symfony\Component\Console\Input\InputOption;
  8. use Symfony\Component\Console\Output\OutputInterface;
  9. /**
  10. * Generate entity classes from mapping information
  11. */
  12. class GenerateEntitiesDoctrineCommand extends DoctrineCommand
  13. {
  14. /**
  15. * {@inheritDoc}
  16. */
  17. protected function configure()
  18. {
  19. $this
  20. ->setName('doctrine:generate:entities')
  21. ->setAliases(['generate:doctrine:entities'])
  22. ->setDescription('Generates entity classes and method stubs from your mapping information')
  23. ->addArgument('name', InputArgument::REQUIRED, 'A bundle name, a namespace, or a class name')
  24. ->addOption('path', null, InputOption::VALUE_REQUIRED, 'The path where to generate entities when it cannot be guessed')
  25. ->addOption('no-backup', null, InputOption::VALUE_NONE, 'Do not backup existing entities files.')
  26. ->setHelp(<<<EOT
  27. The <info>%command.name%</info> command generates entity classes
  28. and method stubs from your mapping information:
  29. You have to limit generation of entities:
  30. * To a bundle:
  31. <info>php %command.full_name% MyCustomBundle</info>
  32. * To a single entity:
  33. <info>php %command.full_name% MyCustomBundle:User</info>
  34. <info>php %command.full_name% MyCustomBundle/Entity/User</info>
  35. * To a namespace
  36. <info>php %command.full_name% MyCustomBundle/Entity</info>
  37. If the entities are not stored in a bundle, and if the classes do not exist,
  38. the command has no way to guess where they should be generated. In this case,
  39. you must provide the <comment>--path</comment> option:
  40. <info>php %command.full_name% Blog/Entity --path=src/</info>
  41. By default, the unmodified version of each entity is backed up and saved
  42. (e.g. Product.php~). To prevent this task from creating the backup file,
  43. pass the <comment>--no-backup</comment> option:
  44. <info>php %command.full_name% Blog/Entity --no-backup</info>
  45. <error>Important:</error> Even if you specified Inheritance options in your
  46. XML or YAML Mapping files the generator cannot generate the base and
  47. child classes for you correctly, because it doesn't know which
  48. class is supposed to extend which. You have to adjust the entity
  49. code manually for inheritance to work!
  50. EOT
  51. );
  52. }
  53. /**
  54. * {@inheritDoc}
  55. */
  56. protected function execute(InputInterface $input, OutputInterface $output)
  57. {
  58. trigger_error('The doctrine:generate:entity command has been deprecated.', E_USER_DEPRECATED);
  59. $output->writeln([
  60. ' <comment>NOTE:</comment> The <info>doctrine:generate:entities</info> command has been deprecated.',
  61. ' To read more about the differences between anemic and rich models go here <info>http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/getting-started.html#adding-behavior-to-entities</info>.',
  62. ' If you wish to generate your entities, use <info>make:entity --regenerate</info> from MakerBundle instead.',
  63. ]);
  64. $manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine'));
  65. try {
  66. $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
  67. $output->writeln(sprintf('Generating entities for bundle "<info>%s</info>"', $bundle->getName()));
  68. $metadata = $manager->getBundleMetadata($bundle);
  69. } catch (\InvalidArgumentException $e) {
  70. $name = strtr($input->getArgument('name'), '/', '\\');
  71. $pos = strpos($name, ':');
  72. if ($pos !== false) {
  73. $name = $this->getContainer()->get('doctrine')->getAliasNamespace(substr($name, 0, $pos)) . '\\' . substr($name, $pos + 1);
  74. }
  75. if (class_exists($name)) {
  76. $output->writeln(sprintf('Generating entity "<info>%s</info>"', $name));
  77. $metadata = $manager->getClassMetadata($name, $input->getOption('path'));
  78. } else {
  79. $output->writeln(sprintf('Generating entities for namespace "<info>%s</info>"', $name));
  80. $metadata = $manager->getNamespaceMetadata($name, $input->getOption('path'));
  81. }
  82. }
  83. $generator = $this->getEntityGenerator();
  84. $backupExisting = ! $input->getOption('no-backup');
  85. $generator->setBackupExisting($backupExisting);
  86. $repoGenerator = new EntityRepositoryGenerator();
  87. foreach ($metadata->getMetadata() as $m) {
  88. if ($backupExisting) {
  89. $basename = substr($m->name, strrpos($m->name, '\\') + 1);
  90. $output->writeln(sprintf(' > backing up <comment>%s.php</comment> to <comment>%s.php~</comment>', $basename, $basename));
  91. }
  92. // Getting the metadata for the entity class once more to get the correct path if the namespace has multiple occurrences
  93. try {
  94. $entityMetadata = $manager->getClassMetadata($m->getName(), $input->getOption('path'));
  95. } catch (\RuntimeException $e) {
  96. // fall back to the bundle metadata when no entity class could be found
  97. $entityMetadata = $metadata;
  98. }
  99. $output->writeln(sprintf(' > generating <comment>%s</comment>', $m->name));
  100. $generator->generate([$m], $entityMetadata->getPath());
  101. if (! $m->customRepositoryClassName || strpos($m->customRepositoryClassName, $metadata->getNamespace()) === false) {
  102. continue;
  103. }
  104. $repoGenerator->writeEntityRepositoryClass($m->customRepositoryClassName, $metadata->getPath());
  105. }
  106. }
  107. }