diff -r 000000000000 -r 7f95f8617b0b vendor/symfony/src/Symfony/Bundle/DoctrineBundle/Command/GenerateEntitiesDoctrineCommand.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/symfony/src/Symfony/Bundle/DoctrineBundle/Command/GenerateEntitiesDoctrineCommand.php Sat Sep 24 15:40:41 2011 +0200 @@ -0,0 +1,123 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\DoctrineBundle\Command; + +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputOption; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; +use Doctrine\ORM\Tools\EntityRepositoryGenerator; +use Symfony\Bundle\DoctrineBundle\Mapping\DisconnectedMetadataFactory; + +/** + * Generate entity classes from mapping information + * + * @author Fabien Potencier + * @author Jonathan H. Wage + */ +class GenerateEntitiesDoctrineCommand extends DoctrineCommand +{ + protected function configure() + { + $this + ->setName('doctrine:generate:entities') + ->setAliases(array('generate:doctrine:entities')) + ->setDescription('Generate entity classes and method stubs from your mapping information') + ->addArgument('name', InputArgument::REQUIRED, 'A bundle name, a namespace, or a class name') + ->addOption('path', null, InputOption::VALUE_REQUIRED, 'The path where to generate entities when it cannot be guessed') + ->addOption('no-backup', null, InputOption::VALUE_NONE, 'Do not backup existing entities files.') + ->setHelp(<<doctrine:generate:entities command generates entity classes +and method stubs from your mapping information: + +You have to limit generation of entities: + +* To a bundle: + + php app/console doctrine:generate:entities MyCustomBundle + +* To a single entity: + + php app/console doctrine:generate:entities MyCustomBundle:User + php app/console doctrine:generate:entities MyCustomBundle/Entity/User + +* To a namespace + + php app/console doctrine:generate:entities MyCustomBundle/Entity + +If the entities are not stored in a bundle, and if the classes do not exist, +the command has no way to guess where they should be generated. In this case, +you must provide the --path option: + + php app/console doctrine:generate:entities Blog/Entity --path=src/ + +By default, the unmodified version of each entity is backed up and saved +(e.g. Product.php~). To prevent this task from creating the backup file, +pass the --no-backup option: + + php app/console doctrine:generate:entities Blog/Entity --no-backup + +Important: Even if you specified Inheritance options in your +XML or YAML Mapping files the generator cannot generate the base and +child classes for you correctly, because it doesn't know which +class is supposed to extend which. You have to adjust the entity +code manually for inheritance to work! + +EOT + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine')); + + try { + $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name')); + + $output->writeln(sprintf('Generating entities for bundle "%s"', $bundle->getName())); + $metadata = $manager->getBundleMetadata($bundle); + } catch (\InvalidArgumentException $e) { + $name = strtr($input->getArgument('name'), '/', '\\'); + + if (false !== $pos = strpos($name, ':')) { + $name = $this->getContainer()->get('doctrine')->getEntityNamespace(substr($name, 0, $pos)).'\\'.substr($name, $pos + 1); + } + + if (class_exists($name)) { + $output->writeln(sprintf('Generating entity "%s"', $name)); + $metadata = $manager->getClassMetadata($name, $input->getOption('path')); + } else { + $output->writeln(sprintf('Generating entities for namespace "%s"', $name)); + $metadata = $manager->getNamespaceMetadata($name, $input->getOption('path')); + } + } + + $generator = $this->getEntityGenerator(); + + $backupExisting = !$input->getOption('no-backup'); + $generator->setBackupExisting($backupExisting); + + $repoGenerator = new EntityRepositoryGenerator(); + foreach ($metadata->getMetadata() as $m) { + if ($backupExisting) { + $basename = substr($m->name, strrpos($m->name, '\\') + 1); + $output->writeln(sprintf(' > backing up %s.php to %s.php~', $basename, $basename)); + } + $output->writeln(sprintf(' > generating %s', $m->name)); + $generator->generate(array($m), $metadata->getPath()); + + if ($m->customRepositoryClassName && false !== strpos($m->customRepositoryClassName, $metadata->getNamespace())) { + $repoGenerator->writeEntityRepositoryClass($m->customRepositoryClassName, $metadata->getPath()); + } + } + } +}