ImportMappingDoctrineCommand.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. use Doctrine\ORM\Tools\Console\MetadataFilter;
  19. /**
  20. * Import Doctrine ORM metadata mapping information from an existing database.
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. * @author Jonathan H. Wage <jonwage@gmail.com>
  24. */
  25. class ImportMappingDoctrineCommand extends DoctrineCommand
  26. {
  27. protected function configure()
  28. {
  29. $this
  30. ->setName('doctrine:mapping:import')
  31. ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to import the mapping information to')
  32. ->addArgument('mapping-type', InputArgument::OPTIONAL, 'The mapping type to export the imported mapping information to')
  33. ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command')
  34. ->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be mapped.')
  35. ->addOption('force', null, InputOption::VALUE_NONE, 'Force to overwrite existing mapping files.')
  36. ->setDescription('Imports mapping information from an existing database')
  37. ->setHelp(<<<EOT
  38. The <info>doctrine:mapping:import</info> command imports mapping information
  39. from an existing database:
  40. <info>php app/console doctrine:mapping:import "MyCustomBundle" xml</info>
  41. You can also optionally specify which entity manager to import from with the
  42. <info>--em</info> option:
  43. <info>php app/console doctrine:mapping:import "MyCustomBundle" xml --em=default</info>
  44. If you don't want to map every entity that can be found in the database, use the
  45. <info>--filter</info> option. It will try to match the targeted mapped entity with the
  46. provided pattern string.
  47. <info>php app/console doctrine:mapping:import "MyCustomBundle" xml --filter=MyMatchedEntity</info>
  48. Use the <info>--force</info> option, if you want to override existing mapping files:
  49. <info>php app/console doctrine:mapping:import "MyCustomBundle" xml --force</info>
  50. EOT
  51. );
  52. }
  53. protected function execute(InputInterface $input, OutputInterface $output)
  54. {
  55. $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
  56. $destPath = $bundle->getPath();
  57. $type = $input->getArgument('mapping-type') ? $input->getArgument('mapping-type') : 'xml';
  58. if ('annotation' === $type) {
  59. $destPath .= '/Entity';
  60. } else {
  61. $destPath .= '/Resources/config/doctrine';
  62. }
  63. if ('yaml' === $type) {
  64. $type = 'yml';
  65. }
  66. $cme = new ClassMetadataExporter();
  67. $exporter = $cme->getExporter($type);
  68. $exporter->setOverwriteExistingFiles($input->getOption('force'));
  69. if ('annotation' === $type) {
  70. $entityGenerator = $this->getEntityGenerator();
  71. $exporter->setEntityGenerator($entityGenerator);
  72. }
  73. $em = $this->getEntityManager($input->getOption('em'));
  74. $databaseDriver = new DatabaseDriver($em->getConnection()->getSchemaManager());
  75. $em->getConfiguration()->setMetadataDriverImpl($databaseDriver);
  76. $emName = $input->getOption('em');
  77. $emName = $emName ? $emName : 'default';
  78. $cmf = new DisconnectedClassMetadataFactory();
  79. $cmf->setEntityManager($em);
  80. $metadata = $cmf->getAllMetadata();
  81. $metadata = MetadataFilter::filter($metadata, $input->getOption('filter'));
  82. if ($metadata) {
  83. $output->writeln(sprintf('Importing mapping information from "<info>%s</info>" entity manager', $emName));
  84. foreach ($metadata as $class) {
  85. $className = $class->name;
  86. $class->name = $bundle->getNamespace().'\\Entity\\'.$className;
  87. if ('annotation' === $type) {
  88. $path = $destPath.'/'.$className.'.php';
  89. } else {
  90. $path = $destPath.'/'.$className.'.orm.'.$type;
  91. }
  92. $output->writeln(sprintf(' > writing <comment>%s</comment>', $path));
  93. $code = $exporter->exportClassMetadata($class);
  94. if (!is_dir($dir = dirname($path))) {
  95. mkdir($dir, 0777, true);
  96. }
  97. file_put_contents($path, $code);
  98. }
  99. } else {
  100. $output->writeln('Database does not have any mapping information.', 'ERROR');
  101. $output->writeln('', 'ERROR');
  102. }
  103. }
  104. }