ImportMappingDoctrineCommand.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 from an existing database:
  36. <info>./app/console doctrine:mapping:import "MyCustomBundle" xml</info>
  37. You can also optionally specify which entity manager to import from with the <info>--em</info> option:
  38. <info>./app/console doctrine:mapping:import "MyCustomBundle" xml --em=default</info>
  39. EOT
  40. );
  41. }
  42. protected function execute(InputInterface $input, OutputInterface $output)
  43. {
  44. $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
  45. $destPath = $bundle->getPath();
  46. $type = $input->getArgument('mapping-type') ? $input->getArgument('mapping-type') : 'xml';
  47. if ('annotation' === $type) {
  48. $destPath .= '/Entity';
  49. } else {
  50. $destPath .= '/Resources/config/doctrine/metadata/orm';
  51. }
  52. if ('yaml' === $type) {
  53. $type = 'yml';
  54. }
  55. $cme = new ClassMetadataExporter();
  56. $exporter = $cme->getExporter($type);
  57. if ('annotation' === $type) {
  58. $entityGenerator = $this->getEntityGenerator();
  59. $exporter->setEntityGenerator($entityGenerator);
  60. }
  61. $em = $this->getEntityManager($input->getOption('em'));
  62. $databaseDriver = new DatabaseDriver($em->getConnection()->getSchemaManager());
  63. $em->getConfiguration()->setMetadataDriverImpl($databaseDriver);
  64. $emName = $input->getOption('em');
  65. $emName = $emName ? $emName : 'default';
  66. $cmf = new DisconnectedClassMetadataFactory();
  67. $cmf->setEntityManager($em);
  68. $metadata = $cmf->getAllMetadata();
  69. if ($metadata) {
  70. $output->writeln(sprintf('Importing mapping information from "<info>%s</info>" entity manager', $emName));
  71. foreach ($metadata as $class) {
  72. $className = $class->name;
  73. $class->name = $bundle->getNamespace().'\\Entity\\'.$className;
  74. if ('annotation' === $type) {
  75. $path = $destPath.'/'.$className.'.php';
  76. } else {
  77. $path = $destPath.'/'.str_replace('\\', '.', $class->name).'.dcm.'.$type;
  78. }
  79. $output->writeln(sprintf(' > writing <comment>%s</comment>', $path));
  80. $code = $exporter->exportClassMetadata($class);
  81. if (!is_dir($dir = dirname($path))) {
  82. mkdir($dir, 0777, true);
  83. }
  84. file_put_contents($path, $code);
  85. }
  86. } else {
  87. $output->writeln('Database does not have any mapping information.', 'ERROR');
  88. $output->writeln('', 'ERROR');
  89. }
  90. }
  91. }