PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/doctrine/doctrine-bundle/Command/ImportMappingDoctrineCommand.php

https://gitlab.com/matijabelec/bigpandadev
PHP | 137 lines | 107 code | 10 blank | 20 comment | 3 complexity | 5d0b98e121eca86b0aab9ca698761143 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Doctrine Bundle
  4. *
  5. * The code was originally distributed inside the Symfony framework.
  6. *
  7. * (c) Fabien Potencier <fabien@symfony.com>
  8. * (c) Doctrine Project, Benjamin Eberlei <kontakt@beberlei.de>
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. namespace Doctrine\Bundle\DoctrineBundle\Command;
  14. use Symfony\Component\Console\Input\InputArgument;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Input\InputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Doctrine\ORM\Mapping\Driver\DatabaseDriver;
  19. use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
  20. use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
  21. use Doctrine\ORM\Tools\Console\MetadataFilter;
  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. /**
  31. * {@inheritDoc}
  32. */
  33. protected function configure()
  34. {
  35. $this
  36. ->setName('doctrine:mapping:import')
  37. ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to import the mapping information to')
  38. ->addArgument('mapping-type', InputArgument::OPTIONAL, 'The mapping type to export the imported mapping information to')
  39. ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command')
  40. ->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be mapped.')
  41. ->addOption('force', null, InputOption::VALUE_NONE, 'Force to overwrite existing mapping files.')
  42. ->setDescription('Imports mapping information from an existing database')
  43. ->setHelp(<<<EOT
  44. The <info>doctrine:mapping:import</info> command imports mapping information
  45. from an existing database:
  46. <info>php app/console doctrine:mapping:import "MyCustomBundle" xml</info>
  47. You can also optionally specify which entity manager to import from with the
  48. <info>--em</info> option:
  49. <info>php app/console doctrine:mapping:import "MyCustomBundle" xml --em=default</info>
  50. If you don't want to map every entity that can be found in the database, use the
  51. <info>--filter</info> option. It will try to match the targeted mapped entity with the
  52. provided pattern string.
  53. <info>php app/console doctrine:mapping:import "MyCustomBundle" xml --filter=MyMatchedEntity</info>
  54. Use the <info>--force</info> option, if you want to override existing mapping files:
  55. <info>php app/console doctrine:mapping:import "MyCustomBundle" xml --force</info>
  56. EOT
  57. );
  58. }
  59. /**
  60. * {@inheritDoc}
  61. */
  62. protected function execute(InputInterface $input, OutputInterface $output)
  63. {
  64. $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
  65. $destPath = $bundle->getPath();
  66. $type = $input->getArgument('mapping-type') ? $input->getArgument('mapping-type') : 'xml';
  67. if ('annotation' === $type) {
  68. $destPath .= '/Entity';
  69. } else {
  70. $destPath .= '/Resources/config/doctrine';
  71. }
  72. if ('yaml' === $type) {
  73. $type = 'yml';
  74. }
  75. $cme = new ClassMetadataExporter();
  76. $exporter = $cme->getExporter($type);
  77. $exporter->setOverwriteExistingFiles($input->getOption('force'));
  78. if ('annotation' === $type) {
  79. $entityGenerator = $this->getEntityGenerator();
  80. $exporter->setEntityGenerator($entityGenerator);
  81. }
  82. $em = $this->getEntityManager($input->getOption('em'));
  83. $databaseDriver = new DatabaseDriver($em->getConnection()->getSchemaManager());
  84. $em->getConfiguration()->setMetadataDriverImpl($databaseDriver);
  85. $emName = $input->getOption('em');
  86. $emName = $emName ? $emName : 'default';
  87. $cmf = new DisconnectedClassMetadataFactory();
  88. $cmf->setEntityManager($em);
  89. $metadata = $cmf->getAllMetadata();
  90. $metadata = MetadataFilter::filter($metadata, $input->getOption('filter'));
  91. if ($metadata) {
  92. $output->writeln(sprintf('Importing mapping information from "<info>%s</info>" entity manager', $emName));
  93. foreach ($metadata as $class) {
  94. $className = $class->name;
  95. $class->name = $bundle->getNamespace().'\\Entity\\'.$className;
  96. if ('annotation' === $type) {
  97. $path = $destPath.'/'.str_replace('\\', '.', $className).'.php';
  98. } else {
  99. $path = $destPath.'/'.str_replace('\\', '.', $className).'.orm.'.$type;
  100. }
  101. $output->writeln(sprintf(' > writing <comment>%s</comment>', $path));
  102. $code = $exporter->exportClassMetadata($class);
  103. if (!is_dir($dir = dirname($path))) {
  104. mkdir($dir, 0775, true);
  105. }
  106. file_put_contents($path, $code);
  107. chmod($path, 0664);
  108. }
  109. return 0;
  110. } else {
  111. $output->writeln('Database does not have any mapping information.', 'ERROR');
  112. $output->writeln('', 'ERROR');
  113. return 1;
  114. }
  115. }
  116. }