8000 [DoctrineBundle] extracted code from Doctrine commands · lidaa/symfony@a046259 · GitHub
[go: up one dir, main page]

Skip to content

Commit a046259

Browse files
committed
[DoctrineBundle] extracted code from Doctrine commands
1 parent c19d6c0 commit a046259

File tree

5 files changed

+258
-139
lines changed

5 files changed

+258
-139
lines changed

src/Symfony/Bundle/DoctrineBundle/Command/DoctrineCommand.php

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@
1212
namespace Symfony\Bundle\DoctrineBundle\Command;
1313

1414
use Symfony\Bundle\FrameworkBundle\Command\Command;
15-
use Symfony\Component\HttpKernel\Bundle\Bundle;
16-
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
17-
use Doctrine\ORM\Mapping\ClassMetadata;
18-
use Doctrine\ORM\Mapping\ClassMetadataInfo;
1915
use Doctrine\ORM\Tools\EntityGenerator;
20-
use Doctrine\ORM\Version as DoctrineVersion;
21-
use Doctrine\ORM\ORMException;
2216

2317
/**
2418
* Base class for Doctrine console commands to extend from.
@@ -54,77 +48,4 @@ protected function getDoctrineConnection($name)
5448
{
5549
return $this->container->get('doctrine')->getConnection($name);
5650
}
57-
58-
protected function findMetadatasByNamespace($namespace)
59-
{
60-
$metadatas = array();
61-
foreach ($this->findAllMetadatas() as $name => $metadata) {
62-
if (strpos($name, $namespace) === 0) {
63-
$metadatas[$name] = $metadata;
64-
}
65-
}
66-
67-
return $metadatas;
68-
}
69-
70-
protected function findMetadatasByClass($entity)
71-
{
72-
foreach ($this->findAllMetadatas() as $name => $metadata) {
73-
if ($name === $entity) {
74-
return array($name => $metadata);
75-
}
76-
}
77-
78-
return array();
79-
}
80-
81-
protected function findAllMetadatas()
82-
{
83-
$metadatas = array();
84-
foreach ($this->container->get('doctrine')->getEntityManagerNames() as $id) {
85-
$cmf = new DisconnectedClassMetadataFactory();
86-
$cmf->setEntityManager($this->container->get($id));
87-
foreach ($cmf->getAllMetadata() as $metadata) {
88-
$metadatas[$metadata->name] = $metadata;
89-
}
90-
}
91-
92-
return $metadatas;
93-
}
94-
95-
/**
96-
* Transform classname to a path $foundBundle substract it to get the destination
97-
*
98-
* @param Bundle $bundle
99-
* @return string
100-
*/
101-
protected function findBasePathForClass($name, $namespace, $path)
102-
{
103-
$namespace = str_replace('\\', '/', $namespace);
104-
$search = str_replace('\\', '/', $path);
105-
$destination = str_replace('/'.$namespace, '', $search, $c);
106-
107-
if ($c != 1) {
108-
throw new \RuntimeException(sprintf('Can\'t find base path for "%s" (path: "%s", destination: "%s").', $name, $path, $destination));
109-
}
110-
111-
return $destination;
112-
}
113-
114-
protected function getAliasedClassName($name)
115-
{
116-
$pos = strpos($name, ':');
117-
$alias = substr($name, 0, $pos);
118-
119-
foreach ($this->container->get('doctrine')->getEntityManagerNames() as $id) {
120-
$em = $this->container->get($id);
121-
122-
try {
123-
return $em->getConfiguration()->getEntityNamespace($alias).'\\'.substr($name, $pos + 1);
124-
} catch (ORMException $e) {
125-
}
126-
}
127-
128-
throw new \RuntimeException(sprintf('Entity "%s" does not exist.', $name));
129-
}
13051
}

src/Symfony/Bundle/DoctrineBundle/Command/GenerateEntitiesDoctrineCommand.php

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Output\OutputInterface;
1818
use Doctrine\ORM\Tools\EntityRepositoryGenerator;
19+
use Symfony\Bundle\DoctrineBundle\Mapping\MetadataFactory;
1920

2021
/**
2122
* Generate entity classes from mapping information
@@ -69,87 +70,39 @@ protected function configure()
6970

7071
protected function execute(InputInterface $input, OutputInterface $output)
7172
{
73+
$manager = new MetadataFactory($this->container->get('doctrine'));
74+
7275
try {
7376
$bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('name'));
7477

7578
$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);
7780
} catch (\InvalidArgumentException $e) {
7881
$name = strtr($input->getArgument('name'), '/', '\\');
7982

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);
8285
}
8386

8487
if (class_exists($name)) {
8588
$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'));
8790
} else {
8891
$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'));
9093
}
9194
}
9295

9396
$generator = $this->getEntityGenerator();
9497
$generator->setBackupExisting(!$input->getOption('no-backup'));
9598
$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());
104102

105-
$repoGenerator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $path);
103+
if ($m->customRepositoryClassName && false !== strpos($m->customRepositoryClassName, $metadata->getNamespace())) {
104+
$repoGenerator->writeEntityRepositoryClass($m->customRepositoryClassName, $metadata->getPath());
106105
}
107106
}
108107
}
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-
}
155108
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Mapping;
13+
14+
/**
15+
*
16+
*
17+
* @author Fabien Potencier <fabien@symfony.com>
18+
*/
19+
class ClassMetadataCollection
20+
{
21+
private $path;
22+
private $namespace;
23+
private $metadata;
24+
25+
public function __construct(array $metadata)
26+
{
27+
$this->metadata = $metadata;
28+
}
29+
30+
public function getMetadata()
31+
{
32+
return $this->metadata;
33+
}
34+
35+
public function setPath($path)
36+
{
37+
$this->path = $path;
38+
}
39+
40+
public function getPath()
41+
{
42+
return $this->path;
43+
}
44+
45+
public function setNamespace($namespace)
46+
{
47+
$this->namespace = $namespace;
48+
}
49+
50+
public function getNamespace()
51+
{
52+
return $this->namespace;
53+
}
54+
}

0 commit comments

Comments
 (0)
0