|
8 | 8 | use Doctrine\Bundle\DoctrineBundle\Mapping\EntityListenerServiceResolver; |
9 | 9 | use Symfony\Component\DependencyInjection\ChildDefinition; |
10 | 10 | use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
11 | | -use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; |
12 | 11 | use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; |
13 | 12 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
14 | 13 | use Symfony\Component\DependencyInjection\Definition; |
|
19 | 18 | use function method_exists; |
20 | 19 | use function sprintf; |
21 | 20 | use function substr; |
| 21 | +use function usort; |
22 | 22 |
|
23 | 23 | /** |
24 | 24 | * Class for Symfony bundles to register entity listeners |
|
27 | 27 | */ |
28 | 28 | class EntityListenerPass implements CompilerPassInterface |
29 | 29 | { |
30 | | - use PriorityTaggedServiceTrait; |
31 | | - |
32 | 30 | public function process(ContainerBuilder $container): void |
33 | 31 | { |
34 | | - $resolvers = $this->findAndSortTaggedServices('doctrine.orm.entity_listener', $container); |
35 | | - |
36 | 32 | $lazyServiceReferencesByResolver = []; |
37 | 33 |
|
38 | | - foreach ($resolvers as $reference) { |
39 | | - $id = $reference->__toString(); |
40 | | - foreach ($container->getDefinition($id)->getTag('doctrine.orm.entity_listener') as $attributes) { |
41 | | - $name = $attributes['entity_manager'] ?? $container->getParameter('doctrine.default_entity_manager'); |
42 | | - $entityManager = sprintf('doctrine.orm.%s_entity_manager', $name); |
| 34 | + $serviceTags = []; |
| 35 | + foreach ($container->findTaggedServiceIds('doctrine.orm.entity_listener', true) as $id => $tags) { |
| 36 | + foreach ($tags as $attributes) { |
| 37 | + $serviceTags[] = [ |
| 38 | + 'serviceId' => $id, |
| 39 | + 'attributes' => $attributes, |
| 40 | + ]; |
| 41 | + } |
| 42 | + } |
43 | 43 |
|
44 | | - if (! $container->hasDefinition($entityManager)) { |
45 | | - continue; |
46 | | - } |
| 44 | + usort($serviceTags, static fn (array $a, array $b) => ($b['attributes']['priority'] ?? 0) <=> ($a['attributes']['priority'] ?? 0)); |
47 | 45 |
|
48 | | - $resolverId = sprintf('doctrine.orm.%s_entity_listener_resolver', $name); |
| 46 | + foreach ($serviceTags as $tag) { |
| 47 | + $id = $tag['serviceId']; |
| 48 | + $attributes = $tag['attributes']; |
| 49 | + $name = $attributes['entity_manager'] ?? $container->getParameter('doctrine.default_entity_manager'); |
| 50 | + $entityManager = sprintf('doctrine.orm.%s_entity_manager', $name); |
49 | 51 |
|
50 | | - if (! $container->has($resolverId)) { |
51 | | - continue; |
52 | | - } |
| 52 | + if (! $container->hasDefinition($entityManager)) { |
| 53 | + continue; |
| 54 | + } |
53 | 55 |
|
54 | | - $resolver = $container->findDefinition($resolverId); |
55 | | - $resolver->setPublic(true); |
| 56 | + $resolverId = sprintf('doctrine.orm.%s_entity_listener_resolver', $name); |
56 | 57 |
|
57 | | - if (isset($attributes['entity'])) { |
58 | | - $this->attachToListener($container, $name, $this->getConcreteDefinitionClass($container->findDefinition($id), $container, $id), $attributes); |
59 | | - } |
| 58 | + if (! $container->has($resolverId)) { |
| 59 | + continue; |
| 60 | + } |
60 | 61 |
|
61 | | - $resolverClass = $this->getResolverClass($resolver, $container, $resolverId); |
62 | | - $resolverSupportsLazyListeners = is_a($resolverClass, EntityListenerServiceResolver::class, true); |
| 62 | + $resolver = $container->findDefinition($resolverId); |
| 63 | + $resolver->setPublic(true); |
63 | 64 |
|
64 | | - $lazyByAttribute = isset($attributes['lazy']) && $attributes['lazy']; |
65 | | - if ($lazyByAttribute && ! $resolverSupportsLazyListeners) { |
66 | | - throw new InvalidArgumentException(sprintf( |
67 | | - 'Lazy-loaded entity listeners can only be resolved by a resolver implementing %s.', |
68 | | - EntityListenerServiceResolver::class, |
69 | | - )); |
70 | | - } |
| 65 | + if (isset($attributes['entity'])) { |
| 66 | + $this->attachToListener($container, $name, $this->getConcreteDefinitionClass($container->findDefinition($id), $container, $id), $attributes); |
| 67 | + } |
71 | 68 |
|
72 | | - if (! isset($attributes['lazy']) && $resolverSupportsLazyListeners || $lazyByAttribute) { |
73 | | - $listener = $container->findDefinition($id); |
| 69 | + $resolverClass = $this->getResolverClass($resolver, $container, $resolverId); |
| 70 | + $resolverSupportsLazyListeners = is_a($resolverClass, EntityListenerServiceResolver::class, true); |
74 | 71 |
|
75 | | - $resolver->addMethodCall('registerService', [$this->getConcreteDefinitionClass($listener, $container, $id), $id]); |
| 72 | + $lazyByAttribute = isset($attributes['lazy']) && $attributes['lazy']; |
| 73 | + if ($lazyByAttribute && ! $resolverSupportsLazyListeners) { |
| 74 | + throw new InvalidArgumentException(sprintf( |
| 75 | + 'Lazy-loaded entity listeners can only be resolved by a resolver implementing %s.', |
| 76 | + EntityListenerServiceResolver::class, |
| 77 | + )); |
| 78 | + } |
| 79 | + |
| 80 | + if (! isset($attributes['lazy']) && $resolverSupportsLazyListeners || $lazyByAttribute) { |
| 81 | + $listener = $container->findDefinition($id); |
76 | 82 |
|
77 | | - // if the resolver uses the default class we will use a service locator for all listeners |
78 | | - if ($resolverClass === ContainerEntityListenerResolver::class) { |
79 | | - if (! isset($lazyServiceReferencesByResolver[$resolverId])) { |
80 | | - $lazyServiceReferencesByResolver[$resolverId] = []; |
81 | | - } |
| 83 | + $resolver->addMethodCall('registerService', [$this->getConcreteDefinitionClass($listener, $container, $id), $id]); |
82 | 84 |
|
83 | | - $lazyServiceReferencesByResolver[$resolverId][$id] = new Reference($id); |
84 | | - } else { |
85 | | - $listener->setPublic(true); |
| 85 | + // if the resolver uses the default class we will use a service locator for all listeners |
| 86 | + if ($resolverClass === ContainerEntityListenerResolver::class) { |
| 87 | + if (! isset($lazyServiceReferencesByResolver[$resolverId])) { |
| 88 | + $lazyServiceReferencesByResolver[$resolverId] = []; |
86 | 89 | } |
| 90 | + |
| 91 | + $lazyServiceReferencesByResolver[$resolverId][$id] = new Reference($id); |
87 | 92 | } else { |
88 | | - $resolver->addMethodCall('register', [new Reference($id)]); |
| 93 | + $listener->setPublic(true); |
89 | 94 | } |
| 95 | + } else { |
| 96 | + $resolver->addMethodCall('register', [new Reference($id)]); |
90 | 97 | } |
91 | 98 | } |
92 | 99 |
|
|
0 commit comments