1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler; |
4
|
|
|
|
5
|
|
|
use Doctrine\Bundle\DoctrineBundle\Mapping\EntityListenerServiceResolver; |
6
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; |
8
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
9
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
10
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class for Symfony bundles to register entity listeners |
14
|
|
|
*/ |
15
|
|
|
class EntityListenerPass implements CompilerPassInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritDoc} |
19
|
|
|
*/ |
20
|
|
|
public function process(ContainerBuilder $container) |
21
|
|
|
{ |
22
|
|
|
$resolvers = $container->findTaggedServiceIds('doctrine.orm.entity_listener'); |
23
|
|
|
|
24
|
|
|
$lazyServiceReferencesByResolver = []; |
25
|
|
|
|
26
|
|
|
foreach ($resolvers as $id => $tagAttributes) { |
27
|
|
|
foreach ($tagAttributes as $attributes) { |
28
|
|
|
$name = isset($attributes['entity_manager']) ? $attributes['entity_manager'] : $container->getParameter('doctrine.default_entity_manager'); |
29
|
|
|
$entityManager = sprintf('doctrine.orm.%s_entity_manager', $name); |
30
|
|
|
|
31
|
|
|
if (! $container->hasDefinition($entityManager)) { |
32
|
|
|
continue; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$resolverId = sprintf('doctrine.orm.%s_entity_listener_resolver', $name); |
36
|
|
|
|
37
|
|
|
if (! $container->has($resolverId)) { |
38
|
|
|
continue; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$resolver = $container->findDefinition($resolverId); |
42
|
|
|
$resolver->setPublic(true); |
43
|
|
|
|
44
|
|
|
if (isset($attributes['entity']) && isset($attributes['event'])) { |
45
|
|
|
$this->attachToListener($container, $name, $id, $attributes); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$class = $resolver->getClass(); |
49
|
|
|
|
50
|
|
|
if (substr($class, 0, 1) === '%') { |
51
|
|
|
// resolve container parameter first |
52
|
|
|
$class = $container->getParameterBag()->resolveValue($resolver->getClass()); |
53
|
|
|
} |
54
|
|
|
$resolverSupportsLazyListeners = is_a($class, EntityListenerServiceResolver::class, true); |
55
|
|
|
|
56
|
|
|
$lazyByAttribute = isset($attributes['lazy']) && $attributes['lazy']; |
57
|
|
|
if ($lazyByAttribute && ! $resolverSupportsLazyListeners) { |
58
|
|
|
throw new InvalidArgumentException(sprintf( |
59
|
|
|
'Lazy-loaded entity listeners can only be resolved by a resolver implementing %s.', |
60
|
|
|
EntityListenerServiceResolver::class |
61
|
|
|
)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (! isset($attributes['lazy']) && $resolverSupportsLazyListeners || $lazyByAttribute) { |
65
|
|
|
$listener = $container->findDefinition($id); |
66
|
|
|
|
67
|
|
|
if ($listener->isAbstract()) { |
68
|
|
|
throw new InvalidArgumentException(sprintf('The service "%s" must not be abstract as this entity listener is lazy-loaded.', $id)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$resolver->addMethodCall('registerService', [$listener->getClass(), $id]); |
72
|
|
|
|
73
|
|
|
if (! isset($lazyServiceReferencesByResolver[$resolverId])) { |
74
|
|
|
$lazyServiceReferencesByResolver[$resolverId] = []; |
75
|
|
|
} |
76
|
|
|
$lazyServiceReferencesByResolver[$resolverId][$id] = new Reference($id); |
77
|
|
|
} else { |
78
|
|
|
$resolver->addMethodCall('register', [new Reference($id)]); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
foreach ($lazyServiceReferencesByResolver as $resolverId => $listenerReferences) { |
84
|
|
|
$container->findDefinition($resolverId)->replaceArgument(0, ServiceLocatorTagPass::register($container, $listenerReferences)); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function attachToListener(ContainerBuilder $container, $name, $id, array $attributes) |
89
|
|
|
{ |
90
|
|
|
$listenerId = sprintf('doctrine.orm.%s_listeners.attach_entity_listeners', $name); |
91
|
|
|
|
92
|
|
|
if (! $container->has($listenerId)) { |
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$serviceDef = $container->getDefinition($id); |
97
|
|
|
|
98
|
|
|
$args = [ |
99
|
|
|
$attributes['entity'], |
100
|
|
|
$serviceDef->getClass(), |
101
|
|
|
$attributes['event'], |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
if (isset($attributes['method'])) { |
105
|
|
|
$args[] = $attributes['method']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$container->findDefinition($listenerId)->addMethodCall('addEntityListener', $args); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|