Skip to content

Commit 776f005

Browse files
committed
bug #149 Improve Doctrine integration Fix #146, #152 (sadikoff)
This PR was squashed before being merged into the 1.0-dev branch (closes #149). Discussion ---------- Improve Doctrine integration Fix #146, #152 This will close #146 in progress... Commits ------- 7b8ac42 Improve Doctrine integration Fix #146, #152
2 parents 1013568 + 7b8ac42 commit 776f005

File tree

19 files changed

+502
-324
lines changed

19 files changed

+502
-324
lines changed

src/Doctrine/DoctrineEntityDetails.php

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/Doctrine/DoctrineEntityHelper.php

Lines changed: 0 additions & 100 deletions
This file was deleted.

src/Doctrine/DoctrineHelper.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony MakerBundle package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\MakerBundle\Doctrine;
13+
14+
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
15+
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain;
16+
use Doctrine\ORM\EntityManagerInterface;
17+
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
18+
use Doctrine\ORM\Tools\DisconnectedClassMetadataFactory;
19+
use Symfony\Bridge\Doctrine\ManagerRegistry;
20+
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
21+
22+
/**
23+
* @author Fabien Potencier <[email protected]>
24+
* @author Ryan Weaver <[email protected]>
25+
* @author Sadicov Vladimir <[email protected]>
26+
*
27+
* @internal
28+
*/
29+
final class DoctrineHelper
30+
{
31+
/**
32+
* @var ManagerRegistry
33+
*/
34+
private $registry;
35+
36+
public function __construct(ManagerRegistry $registry = null)
37+
{
38+
$this->registry = $registry;
39+
}
40+
41+
public function getRegistry(): ManagerRegistry
42+
{
43+
// this should never happen: we will have checked for the
44+
// DoctrineBundle dependency before calling this
45+
if (null === $this->registry) {
46+
throw new \Exception('Somehow the doctrine service is missing. Is DoctrineBundle installed?');
47+
}
48+
49+
return $this->registry;
50+
}
51+
52+
private function isDoctrineInstalled(): bool
53+
{
54+
return null !== $this->registry;
55+
}
56+
57+
public function getMappingDriverForClass(string $className): ?MappingDriver
58+
{
59+
/** @var EntityManagerInterface $em */
60+
$em = $this->getRegistry()->getManagerForClass($className);
61+
62+
if (null === $em) {
63+
throw new \InvalidArgumentException(sprintf('Cannot find the entity manager for class "%s"', $className));
64+
}
65+
66+
$metadataDriver = $em->getConfiguration()->getMetadataDriverImpl();
67+
68+
if (!$metadataDriver instanceof MappingDriverChain) {
69+
return $metadataDriver;
70+
}
71+
72+
foreach ($metadataDriver->getDrivers() as $namespace => $driver) {
73+
if (0 === strpos($className, $namespace)) {
74+
return $driver;
75+
}
76+
}
77+
78+
return $metadataDriver->getDefaultDriver();
79+
}
80+
81+
public function getEntitiesForAutocomplete(): array
82+
{
83+
$entities = [];
84+
85+
if ($this->isDoctrineInstalled()) {
86+
$allMetadata = $this->getMetadata();
87+
88+
/* @var ClassMetadata $metadata */
89+
foreach (array_keys($allMetadata) as $classname) {
90+
$entityClassDetails = new ClassNameDetails($classname, 'App\\Entity');
91+
$entities[] = $entityClassDetails->getRelativeName();
92+
}
93+
}
94+
95+
return $entities;
96+
}
97+
98+
/**
99+
* @param string|null $classOrNamespace
100+
* @param bool $disconnected
101+
*
102+
* @return array|ClassMetadata
103+
*/
104+
public function getMetadata(string $classOrNamespace = null, bool $disconnected = false)
105+
{
106+
$metadata = [];
107+
108+
/** @var EntityManagerInterface $em */
109+
foreach ($this->getRegistry()->getManagers() as $em) {
110+
if ($disconnected) {
111+
$cmf = new DisconnectedClassMetadataFactory();
112+
$cmf->setEntityManager($em);
113+
} else {
114+
$cmf = $em->getMetadataFactory();
115+
}
116+
117+
foreach ($cmf->getAllMetadata() as $m) {
118+
if (null === $classOrNamespace) {
119+
$metadata[$m->getName()] = $m;
120+
} else {
121+
if ($m->getName() == $classOrNamespace) {
122+
return $m;
123+
}
124+
125+
if (0 === strpos($m->getName(), $classOrNamespace)) {
126+
$metadata[$m->getName()] = $m;
127+
}
128+
}
129+
}
130+
}
131+
132+
return $metadata;
133+
}
134+
135+
/**
136+
* @param string $entityClassName
137+
*/
138+
public function createDoctrineDetails(string $entityClassName): ?EntityDetails
139+
{
140+
$metadata = $this->getMetadata($entityClassName);
141+
142+
if ($metadata instanceof ClassMetadata) {
143+
return new EntityDetails($metadata);
144+
}
145+
146+
return null;
147+
}
148+
}

0 commit comments

Comments
 (0)