/
GenerateEntitiesDoctrineCommand.php
91 lines (73 loc) · 3.59 KB
/
GenerateEntitiesDoctrineCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
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 Symfony\Component\Console\Output\Output;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Doctrine\ORM\Tools\EntityGenerator;
/*
* 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.
*/
/**
* Generate entity classes from mapping information
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class GenerateEntitiesDoctrineCommand extends DoctrineCommand
{
protected function configure()
{
$this
->setName('doctrine:generate:entities')
->setDescription('Generate entity classes and method stubs from your mapping information.')
->addOption('bundle', null, InputOption::PARAMETER_OPTIONAL, 'The bundle to initialize the entity or entities in.')
->addOption('entity', null, InputOption::PARAMETER_OPTIONAL, 'The entity class to initialize (requires bundle parameter).')
->setHelp(<<<EOT
The <info>doctrine:generate:entities</info> command generates entity classes and method stubs from your mapping information:
<info>./symfony doctrine:generate:entities</info>
The above would generate entity classes for all bundles.
You can also optionally limit generation to entities within an individual bundle:
<info>./symfony doctrine:generate:entities --bundle="Bundle/MyCustomBundle"</info>
Alternatively, you can limit generation to a single entity within a bundle:
<info>./symfony doctrine:generate:entities --bundle="Bundle/MyCustomBundle" --entity="User"</info>
EOT
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$filterBundle = $input->getOption('bundle') ? str_replace('/', '\\', $input->getOption('bundle')) : false;
$filterEntity = $filterBundle ? $filterBundle . '\\Entity\\' . str_replace('/', '\\', $input->getOption('entity')) : false;
if (!isset($filterBundle) && isset($filterEntity)) {
throw new \InvalidArgumentException(sprintf('Unable to specify an entity without also specifying a bundle.'));
}
$entityGenerator = $this->getEntityGenerator();
foreach ($this->container->getKernelService()->getBundles() as $bundle) {
// retrieve the full bundle classname
$class = $bundle->getReflection()->getName();
if ($filterBundle && $filterBundle != $class) {
continue;
}
// transform classname to a path and substract it to get the destination
$path = dirname(str_replace('\\', '/', $class));
$destination = str_replace('/'.$path, "", $bundle->getPath());
if ($metadatas = $this->getBundleMetadatas($bundle)) {
$output->writeln(sprintf('Generating entities for "<info>%s</info>"', $class));
foreach ($metadatas as $metadata) {
if ($filterEntity && strpos($metadata->name, $filterEntity) !== 0) {
continue;
}
$output->writeln(sprintf(' > generating <comment>%s</comment>', $metadata->name));
$entityGenerator->generate(array($metadata), $destination);
}
}
}
}
}