|
0
|
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\Tools\EntityRepositoryGenerator; |
|
|
19 |
use Symfony\Bundle\DoctrineBundle\Mapping\DisconnectedMetadataFactory; |
|
|
20 |
|
|
|
21 |
/** |
|
|
22 |
* Generate entity classes from mapping information |
|
|
23 |
* |
|
|
24 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
25 |
* @author Jonathan H. Wage <jonwage@gmail.com> |
|
|
26 |
*/ |
|
|
27 |
class GenerateEntitiesDoctrineCommand extends DoctrineCommand |
|
|
28 |
{ |
|
|
29 |
protected function configure() |
|
|
30 |
{ |
|
|
31 |
$this |
|
|
32 |
->setName('doctrine:generate:entities') |
|
|
33 |
->setAliases(array('generate:doctrine:entities')) |
|
|
34 |
->setDescription('Generate entity classes and method stubs from your mapping information') |
|
|
35 |
->addArgument('name', InputArgument::REQUIRED, 'A bundle name, a namespace, or a class name') |
|
|
36 |
->addOption('path', null, InputOption::VALUE_REQUIRED, 'The path where to generate entities when it cannot be guessed') |
|
|
37 |
->addOption('no-backup', null, InputOption::VALUE_NONE, 'Do not backup existing entities files.') |
|
|
38 |
->setHelp(<<<EOT |
|
|
39 |
The <info>doctrine:generate:entities</info> command generates entity classes |
|
|
40 |
and method stubs from your mapping information: |
|
|
41 |
|
|
|
42 |
You have to limit generation of entities: |
|
|
43 |
|
|
|
44 |
* To a bundle: |
|
|
45 |
|
|
|
46 |
<info>php app/console doctrine:generate:entities MyCustomBundle</info> |
|
|
47 |
|
|
|
48 |
* To a single entity: |
|
|
49 |
|
|
|
50 |
<info>php app/console doctrine:generate:entities MyCustomBundle:User</info> |
|
|
51 |
<info>php app/console doctrine:generate:entities MyCustomBundle/Entity/User</info> |
|
|
52 |
|
|
|
53 |
* To a namespace |
|
|
54 |
|
|
|
55 |
<info>php app/console doctrine:generate:entities MyCustomBundle/Entity</info> |
|
|
56 |
|
|
|
57 |
If the entities are not stored in a bundle, and if the classes do not exist, |
|
|
58 |
the command has no way to guess where they should be generated. In this case, |
|
|
59 |
you must provide the <comment>--path</comment> option: |
|
|
60 |
|
|
|
61 |
<info>php app/console doctrine:generate:entities Blog/Entity --path=src/</info> |
|
|
62 |
|
|
|
63 |
By default, the unmodified version of each entity is backed up and saved |
|
|
64 |
(e.g. Product.php~). To prevent this task from creating the backup file, |
|
|
65 |
pass the <comment>--no-backup</comment> option: |
|
|
66 |
|
|
|
67 |
<info>php app/console doctrine:generate:entities Blog/Entity --no-backup</info> |
|
|
68 |
|
|
|
69 |
<error>Important:</error> Even if you specified Inheritance options in your |
|
|
70 |
XML or YAML Mapping files the generator cannot generate the base and |
|
|
71 |
child classes for you correctly, because it doesn't know which |
|
|
72 |
class is supposed to extend which. You have to adjust the entity |
|
|
73 |
code manually for inheritance to work! |
|
|
74 |
|
|
|
75 |
EOT |
|
|
76 |
); |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
80 |
{ |
|
|
81 |
$manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine')); |
|
|
82 |
|
|
|
83 |
try { |
|
|
84 |
$bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name')); |
|
|
85 |
|
|
|
86 |
$output->writeln(sprintf('Generating entities for bundle "<info>%s</info>"', $bundle->getName())); |
|
|
87 |
$metadata = $manager->getBundleMetadata($bundle); |
|
|
88 |
} catch (\InvalidArgumentException $e) { |
|
|
89 |
$name = strtr($input->getArgument('name'), '/', '\\'); |
|
|
90 |
|
|
|
91 |
if (false !== $pos = strpos($name, ':')) { |
|
|
92 |
$name = $this->getContainer()->get('doctrine')->getEntityNamespace(substr($name, 0, $pos)).'\\'.substr($name, $pos + 1); |
|
|
93 |
} |
|
|
94 |
|
|
|
95 |
if (class_exists($name)) { |
|
|
96 |
$output->writeln(sprintf('Generating entity "<info>%s</info>"', $name)); |
|
|
97 |
$metadata = $manager->getClassMetadata($name, $input->getOption('path')); |
|
|
98 |
} else { |
|
|
99 |
$output->writeln(sprintf('Generating entities for namespace "<info>%s</info>"', $name)); |
|
|
100 |
$metadata = $manager->getNamespaceMetadata($name, $input->getOption('path')); |
|
|
101 |
} |
|
|
102 |
} |
|
|
103 |
|
|
|
104 |
$generator = $this->getEntityGenerator(); |
|
|
105 |
|
|
|
106 |
$backupExisting = !$input->getOption('no-backup'); |
|
|
107 |
$generator->setBackupExisting($backupExisting); |
|
|
108 |
|
|
|
109 |
$repoGenerator = new EntityRepositoryGenerator(); |
|
|
110 |
foreach ($metadata->getMetadata() as $m) { |
|
|
111 |
if ($backupExisting) { |
|
|
112 |
$basename = substr($m->name, strrpos($m->name, '\\') + 1); |
|
|
113 |
$output->writeln(sprintf(' > backing up <comment>%s.php</comment> to <comment>%s.php~</comment>', $basename, $basename)); |
|
|
114 |
} |
|
|
115 |
$output->writeln(sprintf(' > generating <comment>%s</comment>', $m->name)); |
|
|
116 |
$generator->generate(array($m), $metadata->getPath()); |
|
|
117 |
|
|
|
118 |
if ($m->customRepositoryClassName && false !== strpos($m->customRepositoryClassName, $metadata->getNamespace())) { |
|
|
119 |
$repoGenerator->writeEntityRepositoryClass($m->customRepositoryClassName, $metadata->getPath()); |
|
|
120 |
} |
|
|
121 |
} |
|
|
122 |
} |
|
|
123 |
} |