/src/Symfony/Bundle/DoctrineBundle/Command/ConvertDoctrine1SchemaDoctrineCommand.php

https://github.com/sebio/symfony · PHP · 105 lines · 74 code · 13 blank · 18 comment · 9 complexity · 6b45b66df6815fc9e2f8b409c0e6e282 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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. use Doctrine\ORM\Tools\ConvertDoctrine1Schema;
  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>./app/console doctrine:mapping:convert-d1-schema /path/to/doctrine1schema "BundleMyBundle" 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. $bundle = $this->application->getKernel()->getBundle($input->getArgument('bundle'));
  52. $destPath = $bundle->getPath();
  53. $type = $input->getArgument('mapping-type') ? $input->getArgument('mapping-type') : 'xml';
  54. if ('annotation' === $type) {
  55. $destPath .= '/Entity';
  56. } else {
  57. $destPath .= '/Resources/config/doctrine/metadata/orm';
  58. }
  59. // adjust so file naming works
  60. if ('yaml' === $type) {
  61. $type = 'yml';
  62. }
  63. $cme = new ClassMetadataExporter();
  64. $exporter = $cme->getExporter($type);
  65. if ('annotation' === $type) {
  66. $entityGenerator = $this->getEntityGenerator();
  67. $exporter->setEntityGenerator($entityGenerator);
  68. }
  69. $converter = new ConvertDoctrine1Schema($input->getArgument('d1-schema'));
  70. $metadata = $converter->getMetadata();
  71. if ($metadata) {
  72. $output->writeln(sprintf('Converting Doctrine 1 schema "<info>%s</info>"', $input->getArgument('d1-schema')));
  73. foreach ($metadata as $class) {
  74. $className = $class->name;
  75. $class->name = $bundle->getNamespace().'\\Entity\\'.$className;
  76. if ('annotation' === $type) {
  77. $path = $destPath.'/'.$className.'.php';
  78. } else {
  79. $path = $destPath.'/'.str_replace('\\', '.', $class->name).'.dcm.'.$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.'.PHP_EOL, 'ERROR');
  90. }
  91. }
  92. }