diff --git a/src/Symfony/Framework/DoctrineBundle/Command/ImportMappingDoctrineCommand.php b/src/Symfony/Framework/DoctrineBundle/Command/ImportMappingDoctrineCommand.php new file mode 100644 index 0000000000..537758422a --- /dev/null +++ b/src/Symfony/Framework/DoctrineBundle/Command/ImportMappingDoctrineCommand.php @@ -0,0 +1,81 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +/** + * Import the initial mapping information for entities from an existing database + * into a bundle. + * + * @package symfony + * @subpackage console + * @author Fabien Potencier + * @author Jonathan H. Wage + */ +class ImportMappingDoctrineCommand extends DoctrineCommand +{ + /** + * @see Command + */ + protected function configure() + { + $this + ->setName('doctrine:import-mapping') + ->setDescription('Import the initial mapping information for entities from an existing database.') + ->addOption('connection', null, InputOption::PARAMETER_REQUIRED, 'The connection import from.') + ->addOption('bundle', null, InputOption::PARAMETER_REQUIRED, 'The bundle to import the mapping information to.') + ->addOption('type', null, InputOption::PARAMETER_OPTIONAL, 'The mapping format type to generate (defaults to xml).', 'xml') + ; + } + + /** + * @see Command + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + if (!preg_match('/Bundle$/', $bundle = $input->getOption('bundle'))) + { + throw new \InvalidArgumentException('The bundle must end with Bundle.'); + } + + $dirs = $this->container->getKernelService()->getBundleDirs(); + + $tmp = str_replace('\\', '/', $bundle); + $namespace = str_replace('/', '\\', dirname($tmp)); + $bundle = basename($tmp); + + if (!isset($dirs[$namespace])) + { + throw new \InvalidArgumentException(sprintf('Could not find namespace "%s" for bundle "%s".', $namespace, $bundle)); + } + + $path = $dirs[$namespace].'/'.$bundle.'/Resources/config/doctrine/metadata'; + + if (!is_dir($path)) + { + mkdir($path, 0777, true); + } + + $this->em = $this->container->getService(sprintf('doctrine.orm.%s_entity_manager', $input->getOption('connection'))); + $this->runCommand('doctrine:convert-mapping', array( + '--from' => 'database', + '--to' => $input->getOption('type'), + '--dest' => $path + ) + ); + } +} \ No newline at end of file