ImportMappingDoctrineCommand.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 Symfony\Bundle\DoctrineBundle\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Doctrine\ORM\Mapping\Driver\DatabaseDriver;
  16. use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
  17. use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
  18. /**
  19. * Import Doctrine ORM metadata mapping information from an existing database.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. * @author Jonathan H. Wage <jonwage@gmail.com>
  23. */
  24. class ImportMappingDoctrineCommand extends DoctrineCommand
  25. {
  26. protected function configure()
  27. {
  28. $this
  29. ->setName('doctrine:mapping:import')
  30. ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to import the mapping information to')
  31. ->addArgument('mapping-type', InputArgument::OPTIONAL, 'The mapping type to export the imported mapping information to')
  32. ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command')
  33. ->setDescription('Import mapping information from an existing database')
  34. ->setHelp(<<<EOT
  35. The <info>doctrine:mapping:import</info> command imports mapping information
  36. from an existing database:
  37. <info>./app/console doctrine:mapping:import "MyCustomBundle" xml</info>
  38. You can also optionally specify which entity manager to import from with the
  39. <info>--em</info> option:
  40. <info>./app/console doctrine:mapping:import "MyCustomBundle" xml --em=default</info>
  41. EOT
  42. );
  43. }
  44. protected function execute(InputInterface $input, OutputInterface $output)
  45. {
  46. $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
  47. $destPath = $bundle->getPath();
  48. $type = $input->getArgument('mapping-type') ? $input->getArgument('mapping-type') : 'xml';
  49. if ('annotation' === $type) {
  50. $destPath .= '/Entity';
  51. } else {
  52. $destPath .= '/Resources/config/doctrine';
  53. }
  54. if ('yaml' === $type) {
  55. $type = 'yml';
  56. }
  57. $cme = new ClassMetadataExporter();
  58. $exporter = $cme->getExporter($type);
  59. if ('annotation' === $type) {
  60. $entityGenerator = $this->getEntityGenerator();
  61. $exporter->setEntityGenerator($entityGenerator);
  62. }
  63. $em = $this->getEntityManager($input->getOption('em'));
  64. $databaseDriver = new DatabaseDriver($em->getConnection()->getSchemaManager());
  65. $em->getConfiguration()->setMetadataDriverImpl($databaseDriver);
  66. $emName = $input->getOption('em');
  67. $emName = $emName ? $emName : 'default';
  68. $cmf = new DisconnectedClassMetadataFactory();
  69. $cmf->setEntityManager($em);
  70. $metadata = $cmf->getAllMetadata();
  71. if ($metadata) {
  72. $output->writeln(sprintf('Importing mapping information from "<info>%s</info>" entity manager', $emName));
  73. foreach ($metadata as $class) {
  74. $className = $class->name;
  75. $class->name = $bundle->getNamespace().'\\Entity\\'.$className;
  76. if ('annotation' === $type) {
  77. $path = $destPath.'/'.$className.'.orm.php';
  78. } else {
  79. $path = $destPath.'/'.str_replace('\\', '.', $class->name).'.orm.'.$type;
  80. }
  81. $output->writeln(sprintf(' > writing <comment>%s</comment>', $path));
  82. $code = $exporter->exportClassMetadata($class);
  83. if (!is_dir($dir = dirname($path))) {
  84. mkdir($dir, 0777, true);
  85. }
  86. file_put_contents($path, $code);
  87. }
  88. } else {
  89. $output->writeln('Database does not have any mapping information.', 'ERROR');
  90. $output->writeln('', 'ERROR');
  91. }
  92. }
  93. }