ConvertDoctrine1SchemaDoctrineCommand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Symfony\Bundle\DoctrineBundle\Command;
  3. use Symfony\Component\Console\Input\InputArgument;
  4. use Symfony\Component\Console\Input\InputOption;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. use Symfony\Component\Console\Output\Output;
  8. use Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand;
  9. use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
  10. use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
  11. use Doctrine\ORM\Mapping\Driver\DatabaseDriver;
  12. use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
  13. use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
  14. use Doctrine\ORM\Tools\ConvertDoctrine1Schema;
  15. /*
  16. * This file is part of the Symfony framework.
  17. *
  18. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  19. *
  20. * This source file is subject to the MIT license that is bundled
  21. * with this source code in the file LICENSE.
  22. */
  23. /**
  24. * Convert a Doctrine 1 schema to Doctrine 2 mapping files
  25. *
  26. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  27. * @author Jonathan H. Wage <jonwage@gmail.com>
  28. */
  29. class ConvertDoctrine1SchemaDoctrineCommand extends DoctrineCommand
  30. {
  31. protected function configure()
  32. {
  33. $this
  34. ->setName('doctrine:mapping:convert-d1-schema')
  35. ->setDescription('Convert a Doctrine 1 schema to Doctrine 2 mapping files.')
  36. ->addArgument('d1-schema', InputArgument::REQUIRED, 'Path to the Doctrine 1 schema files.')
  37. ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to write the converted mapping information to.')
  38. ->addArgument('mapping-type', InputArgument::OPTIONAL, 'The mapping type to export the converted mapping information to.')
  39. ->setHelp(<<<EOT
  40. The <info>doctrine:mapping:convert-d1-schema</info> command converts a Doctrine 1 schema to Doctrine 2 mapping files:
  41. <info>./symfony doctrine:mapping:convert-d1-schema /path/to/doctrine1schema "Bundle\MyBundle" xml</info>
  42. Each Doctrine 1 model will have its own XML mapping file located in <info>Bundle/MyBundle/config/doctrine/metadata</info>.
  43. EOT
  44. );
  45. }
  46. /**
  47. * @see Command
  48. */
  49. protected function execute(InputInterface $input, OutputInterface $output)
  50. {
  51. $bundleClass = null;
  52. $bundleDirs = $this->container->getKernelService()->getBundleDirs();
  53. foreach ($this->container->getKernelService()->getBundles() as $bundle) {
  54. if (strpos(get_class($bundle), $input->getArgument('bundle')) !== false) {
  55. $tmp = dirname(str_replace('\\', '/', get_class($bundle)));
  56. $namespace = str_replace('/', '\\', dirname($tmp));
  57. $class = basename($tmp);
  58. if (isset($bundleDirs[$namespace])) {
  59. $destPath = realpath($bundleDirs[$namespace]).'/'.$class;
  60. $bundleClass = $class;
  61. break;
  62. }
  63. }
  64. }
  65. $type = $input->getArgument('mapping-type') ? $input->getArgument('mapping-type') : 'xml';
  66. if ($type === 'annotation') {
  67. $destPath .= '/Entity';
  68. } else {
  69. $destPath .= '/Resources/config/doctrine/metadata/orm';
  70. }
  71. // adjust so file naming works
  72. if ($type === 'yaml') {
  73. $type = 'yml';
  74. }
  75. $cme = new ClassMetadataExporter();
  76. $exporter = $cme->getExporter($type);
  77. if ($type === 'annotation') {
  78. $entityGenerator = $this->getEntityGenerator();
  79. $exporter->setEntityGenerator($entityGenerator);
  80. }
  81. $converter = new ConvertDoctrine1Schema($input->getArgument('d1-schema'));
  82. $metadata = $converter->getMetadata();
  83. if ($metadata) {
  84. $output->writeln(sprintf('Converting Doctrine 1 schema "<info>%s</info>"', $input->getArgument('d1-schema')));
  85. foreach ($metadata as $class) {
  86. $className = $class->name;
  87. $class->name = $namespace.'\\'.$bundleClass.'\\Entity\\'.$className;
  88. if ($type === 'annotation') {
  89. $path = $destPath.'/'.$className.'.php';
  90. } else {
  91. $path = $destPath.'/'.str_replace('\\', '.', $class->name).'.dcm.'.$type;
  92. }
  93. $output->writeln(sprintf(' > writing <comment>%s</comment>', $path));
  94. $code = $exporter->exportClassMetadata($class);
  95. if (!is_dir($dir = dirname($path))) {
  96. mkdir($dir, 0777, true);
  97. }
  98. file_put_contents($path, $code);
  99. }
  100. } else {
  101. $output->writeln('Database does not have any mapping information.'.PHP_EOL, 'ERROR');
  102. }
  103. }
  104. }