Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DoctrineBundle] Adding doctrine:import-mapping command to allow you …
…to easily import the mapping information from an existing database connection to a bundle
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
src/Symfony/Framework/DoctrineBundle/Command/ImportMappingDoctrineCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Symfony\Framework\DoctrineBundle\Command; | ||
|
||
use Symfony\Components\Console\Input\InputArgument; | ||
use Symfony\Components\Console\Input\InputOption; | ||
use Symfony\Components\Console\Input\InputInterface; | ||
use Symfony\Components\Console\Output\OutputInterface; | ||
use Symfony\Components\Console\Output\Output; | ||
|
||
/* | ||
* This file is part of the symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* 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 <fabien.potencier@symfony-project.com> | ||
* @author Jonathan H. Wage <jonwage@gmail.com> | ||
*/ | ||
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 | ||
) | ||
); | ||
} | ||
} |