1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory; |
6
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
8
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
9
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
10
|
|
|
|
11
|
|
|
class FallbackServiceRepositoryCompilePass implements CompilerPassInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param ContainerBuilder $container |
15
|
|
|
* |
16
|
|
|
* @throws \Exception |
17
|
|
|
*/ |
18
|
|
|
public function process(ContainerBuilder $container) |
19
|
|
|
{ |
20
|
|
|
foreach (array_keys($container->getDefinitions()) as $id) { |
21
|
|
|
if (!preg_match('/^doctrine\.orm\.[^.]+_entity_manager$/', $id)) { |
22
|
|
|
continue; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$this->processMetadata( |
26
|
|
|
$container, |
27
|
|
|
$id, |
28
|
|
|
$container->get($id)->getMetadataFactory() |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ContainerBuilder $container |
35
|
|
|
* @param string $entityManagerServiceId |
36
|
|
|
* @param ClassMetadataFactory $factory |
37
|
|
|
*/ |
38
|
|
|
protected function processMetadata( |
39
|
|
|
ContainerBuilder $container, |
40
|
|
|
$entityManagerServiceId, |
41
|
|
|
ClassMetadataFactory $factory |
42
|
|
|
) { |
43
|
|
|
foreach ($factory->getAllMetadata() as $metadata) { |
44
|
|
|
$repositoryClassName = null; |
45
|
|
|
if ($metadata->customRepositoryClassName) { |
|
|
|
|
46
|
|
|
$repositoryClassName = $metadata->customRepositoryClassName; |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($repositoryClassName === null) { |
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (!$container->has($repositoryClassName)) { |
54
|
|
|
$repositoryDefinition = new Definition( |
55
|
|
|
$repositoryClassName, |
56
|
|
|
[ |
57
|
|
|
$metadata->getName() |
58
|
|
|
] |
59
|
|
|
); |
60
|
|
|
$repositoryDefinition->setFactory([ |
61
|
|
|
new Reference($entityManagerServiceId), |
62
|
|
|
'getRepository' |
63
|
|
|
]); |
64
|
|
|
$repositoryDefinition->addTag(ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG); |
65
|
|
|
$container->setDefinition( |
66
|
|
|
$repositoryClassName, |
67
|
|
|
$repositoryDefinition |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: