|
16 | 16 | use Symfony\Component\Console\Input\InputInterface;
|
17 | 17 | use Symfony\Component\Console\Output\OutputInterface;
|
18 | 18 | use Doctrine\ORM\Tools\EntityRepositoryGenerator;
|
| 19 | +use Symfony\Bundle\DoctrineBundle\Mapping\MetadataFactory; |
19 | 20 |
|
20 | 21 | /**
|
21 | 22 | * Generate entity classes from mapping information
|
@@ -69,87 +70,39 @@ protected function configure()
|
69 | 70 |
|
70 | 71 | protected function execute(InputInterface $input, OutputInterface $output)
|
71 | 72 | {
|
| 73 | + $manager = new MetadataFactory($this->container->get('doctrine')); |
| 74 | + |
72 | 75 | try {
|
73 | 76 | $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
|
74 | 77 |
|
75 | 78 | $output->writeln(sprintf('Generating entities for bundle "<info>%s</info>"', $bundle->getName()));
|
76 |
| - list($metadatas, $namespace, $path) = $this->getBundleInfo($bundle); |
| 79 | + $metadata = $manager->getBundleMetadata($bundle); |
77 | 80 | } catch (\InvalidArgumentException $e) {
|
78 | 81 | $name = strtr($input->getArgument('name'), '/', '\\');
|
79 | 82 |
|
80 |
| - if (false !== strpos($name, ':')) { |
81 |
| - $name = $this->getAliasedClassName($name); |
| 83 | + if (false !== $pos = strpos($name, ':')) { |
| 84 | + $name = $this->container->get('doctrine')->getEntityNamespace(substr($name, 0, $pos)).'\\'.substr($name, $pos + 1); |
82 | 85 | }
|
83 | 86 |
|
84 | 87 | if (class_exists($name)) {
|
85 | 88 | $output->writeln(sprintf('Generating entity "<info>%s</info>"', $name));
|
86 |
| - list($metadatas, $namespace, $path) = $this->getClassInfo($name, $input->getOption('path')); |
| 89 | + $metadata = $manager->getClassMetadata($name, $input->getOption('path')); |
87 | 90 | } else {
|
88 | 91 | $output->writeln(sprintf('Generating entities for namespace "<info>%s</info>"', $name));
|
89 |
| - list($metadatas, $namespace, $path) = $this->getNamespaceInfo($name, $input->getOption('path')); |
| 92 | + $metadata = $manager->getNamespaceMetadata($name, $input->getOption('path')); |
90 | 93 | }
|
91 | 94 | }
|
92 | 95 |
|
93 | 96 | $generator = $this->getEntityGenerator();
|
94 | 97 | $generator->setBackupExisting(!$input->getOption('no-backup'));
|
95 | 98 | $repoGenerator = new EntityRepositoryGenerator();
|
96 |
| - foreach ($metadatas as $metadata) { |
97 |
| - $output->writeln(sprintf(' > generating <comment>%s</comment>', $metadata->name)); |
98 |
| - $generator->generate(array($metadata), $path); |
99 |
| - |
100 |
| - if ($metadata->customRepositoryClassName) { |
101 |
| - if (false === strpos($metadata->customRepositoryClassName, $namespace)) { |
102 |
| - continue; |
103 |
| - } |
| 99 | + foreach ($metadata->getMetadata() as $m) { |
| 100 | + $output->writeln(sprintf(' > generating <comment>%s</comment>', $m->name)); |
| 101 | + $generator->generate(array($m), $metadata->getPath()); |
104 | 102 |
|
105 |
| - $repoGenerator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $path); |
| 103 | + if ($m->customRepositoryClassName && false !== strpos($m->customRepositoryClassName, $metadata->getNamespace())) { |
| 104 | + $repoGenerator->writeEntityRepositoryClass($m->customRepositoryClassName, $metadata->getPath()); |
106 | 105 | }
|
107 | 106 | }
|
108 | 107 | }
|
109 |
| - |
110 |
| - private function getBundleInfo($bundle) |
111 |
| - { |
112 |
| - $namespace = $bundle->getNamespace(); |
113 |
| - if (!$metadatas = $this->findMetadatasByNamespace($namespace)) { |
114 |
| - throw new \RuntimeException(sprintf('Bundle "%s" does not contain any mapped entities.', $bundle->getName())); |
115 |
| - } |
116 |
| - |
117 |
| - $path = $this->findBasePathForClass($bundle->getName(), $bundle->getNamespace(), $bundle->getPath()); |
118 |
| - |
119 |
| - return array($metadatas, $bundle->getNamespace(), $path); |
120 |
| - } |
121 |
| - |
122 |
| - private function getClassInfo($class, $path) |
123 |
| - { |
124 |
| - if (!$metadatas = $this->findMetadatasByClass($class)) { |
125 |
| - throw new \RuntimeException(sprintf('Entity "%s" is not a mapped entity.', $class)); |
126 |
| - } |
127 |
| - |
128 |
| - if (class_exists($class)) { |
129 |
| - $r = $metadatas[$class]->getReflectionClass(); |
130 |
| - $path = $this->findBasePathForClass($class, $r->getNamespacename(), dirname($r->getFilename())); |
131 |
| - } elseif (!$path) { |
132 |
| - throw new \RuntimeException(sprintf('Unable to determine where to save the "%s" class (use the --path option).', $class)); |
133 |
| - } |
134 |
| - |
135 |
| - return array($metadatas, $r->getNamespacename(), $path); |
136 |
| - } |
137 |
| - |
138 |
| - private function getNamespaceInfo($namespace, $path) |
139 |
| - { |
140 |
| - if (!$metadatas = $this->findMetadatasByNamespace($namespace)) { |
141 |
| - throw new \RuntimeException(sprintf('Namespace "%s" does not contain any mapped entities.', $namespace)); |
142 |
| - } |
143 |
| - |
144 |
| - $first = reset($metadatas); |
145 |
| - $class = key($metadatas); |
146 |
| - if (class_exists($class)) { |
147 |
| - $r = $first->getReflectionClass(); |
148 |
| - $path = $this->findBasePathForClass($namespace, $r->getNamespacename(), dirname($r->getFilename())); |
149 |
| - } elseif (!$path) { |
150 |
| - throw new \RuntimeException(sprintf('Unable to determine where to save the "%s" class (use the --path option).', $class)); |
151 |
| - } |
152 |
| - |
153 |
| - return array($metadatas, $namespace, $path); |
154 |
| - } |
155 | 108 | }
|
0 commit comments