vendor/symfony/src/Symfony/Bundle/DoctrineBundle/Command/ImportMappingDoctrineCommand.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Bundle\DoctrineBundle\Command;
       
    13 
       
    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 /**
       
    24  * Import Doctrine ORM metadata mapping information from an existing database.
       
    25  *
       
    26  * @author Fabien Potencier <fabien@symfony.com>
       
    27  * @author Jonathan H. Wage <jonwage@gmail.com>
       
    28  */
       
    29 class ImportMappingDoctrineCommand extends DoctrineCommand
       
    30 {
       
    31     protected function configure()
       
    32     {
       
    33         $this
       
    34             ->setName('doctrine:mapping:import')
       
    35             ->addArgument('bundle', InputArgument::REQUIRED, 'The bundle to import the mapping information to')
       
    36             ->addArgument('mapping-type', InputArgument::OPTIONAL, 'The mapping type to export the imported mapping information to')
       
    37             ->addOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command')
       
    38             ->addOption('filter', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'A string pattern used to match entities that should be mapped.')
       
    39             ->addOption('force', null, InputOption::VALUE_NONE, 'Force to overwrite existing mapping files.')
       
    40             ->setDescription('Import mapping information from an existing database')
       
    41             ->setHelp(<<<EOT
       
    42 The <info>doctrine:mapping:import</info> command imports mapping information
       
    43 from an existing database:
       
    44 
       
    45 <info>php app/console doctrine:mapping:import "MyCustomBundle" xml</info>
       
    46 
       
    47 You can also optionally specify which entity manager to import from with the
       
    48 <info>--em</info> option:
       
    49 
       
    50 <info>php app/console doctrine:mapping:import "MyCustomBundle" xml --em=default</info>
       
    51 
       
    52 If you don't want to map every entity that can be found in the database, use the
       
    53 <info>--filter</info> option. It will try to match the targeted mapped entity with the
       
    54 provided pattern string.
       
    55 
       
    56 <info>php app/console doctrine:mapping:import "MyCustomBundle" xml --filter=MyMatchedEntity</info>
       
    57 
       
    58 Use the <info>--force</info> option, if you want to override existing mapping files:
       
    59 
       
    60 <info>php app/console doctrine:mapping:import "MyCustomBundle" xml --force</info>
       
    61 EOT
       
    62         );
       
    63     }
       
    64 
       
    65     protected function execute(InputInterface $input, OutputInterface $output)
       
    66     {
       
    67         $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle'));
       
    68 
       
    69         $destPath = $bundle->getPath();
       
    70         $type = $input->getArgument('mapping-type') ? $input->getArgument('mapping-type') : 'xml';
       
    71         if ('annotation' === $type) {
       
    72             $destPath .= '/Entity';
       
    73         } else {
       
    74             $destPath .= '/Resources/config/doctrine';
       
    75         }
       
    76         if ('yaml' === $type) {
       
    77             $type = 'yml';
       
    78         }
       
    79 
       
    80         $cme = new ClassMetadataExporter();
       
    81         $exporter = $cme->getExporter($type);
       
    82         $exporter->setOverwriteExistingFiles($input->getOption('force'));
       
    83 
       
    84         if ('annotation' === $type) {
       
    85             $entityGenerator = $this->getEntityGenerator();
       
    86             $exporter->setEntityGenerator($entityGenerator);
       
    87         }
       
    88 
       
    89         $em = $this->getEntityManager($input->getOption('em'));
       
    90 
       
    91         $databaseDriver = new DatabaseDriver($em->getConnection()->getSchemaManager());
       
    92         $em->getConfiguration()->setMetadataDriverImpl($databaseDriver);
       
    93 
       
    94         $emName = $input->getOption('em');
       
    95         $emName = $emName ? $emName : 'default';
       
    96 
       
    97         $cmf = new DisconnectedClassMetadataFactory();
       
    98         $cmf->setEntityManager($em);
       
    99         $metadata = $cmf->getAllMetadata();
       
   100         $metadata = MetadataFilter::filter($metadata, $input->getOption('filter'));
       
   101         if ($metadata) {
       
   102             $output->writeln(sprintf('Importing mapping information from "<info>%s</info>" entity manager', $emName));
       
   103             foreach ($metadata as $class) {
       
   104                 $className = $class->name;
       
   105                 $class->name = $bundle->getNamespace().'\\Entity\\'.$className;
       
   106                 if ('annotation' === $type) {
       
   107                     $path = $destPath.'/'.$className.'.php';
       
   108                 } else {
       
   109                     $path = $destPath.'/'.$className.'.orm.'.$type;
       
   110                 }
       
   111                 $output->writeln(sprintf('  > writing <comment>%s</comment>', $path));
       
   112                 $code = $exporter->exportClassMetadata($class);
       
   113                 if (!is_dir($dir = dirname($path))) {
       
   114                     mkdir($dir, 0777, true);
       
   115                 }
       
   116                 file_put_contents($path, $code);
       
   117             }
       
   118         } else {
       
   119             $output->writeln('Database does not have any mapping information.', 'ERROR');
       
   120             $output->writeln('', 'ERROR');
       
   121         }
       
   122     }
       
   123 }