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