1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\NotificationBundle\DependencyInjection\Compiler; |
15
|
|
|
|
16
|
|
|
use Sonata\NotificationBundle\Event\IterateEvent; |
17
|
|
|
use Sonata\NotificationBundle\Event\IterationListener; |
18
|
|
|
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument; |
19
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
21
|
|
|
use Symfony\Component\DependencyInjection\Exception\RuntimeException; |
22
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
23
|
|
|
|
24
|
|
|
class NotificationCompilerPass implements CompilerPassInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function process(ContainerBuilder $container): void |
30
|
|
|
{ |
31
|
|
|
if (!$container->hasDefinition('sonata.notification.dispatcher')) { |
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$definition = $container->getDefinition('sonata.notification.dispatcher'); |
36
|
|
|
|
37
|
|
|
$informations = []; |
38
|
|
|
|
39
|
|
|
foreach ($container->findTaggedServiceIds('sonata.notification.consumer') as $id => $events) { |
40
|
|
|
$container->getDefinition($id)->setPublic(true); |
41
|
|
|
|
42
|
|
|
foreach ($events as $event) { |
43
|
|
|
$priority = $event['priority'] ?? 0; |
44
|
|
|
|
45
|
|
|
if (!isset($event['type'])) { |
46
|
|
|
throw new \InvalidArgumentException(sprintf( |
47
|
|
|
'Service "%s" must define the "type" attribute on "sonata.notification" tags.', |
48
|
|
|
$id |
49
|
|
|
)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!isset($informations[$event['type']])) { |
53
|
|
|
$informations[$event['type']] = []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$informations[$event['type']][] = $id; |
57
|
|
|
|
58
|
|
|
$definition->addMethodCall( |
59
|
|
|
'addListener', |
60
|
|
|
[ |
61
|
|
|
$event['type'], |
62
|
|
|
[new ServiceClosureArgument(new Reference($id)), 'process'], |
63
|
|
|
$priority, |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$container->getDefinition('sonata.notification.consumer.metadata')->replaceArgument(0, $informations); |
70
|
|
|
|
71
|
|
|
if ($container->getParameter('sonata.notification.event.iteration_listeners')) { |
72
|
|
|
$ids = $container->getParameter('sonata.notification.event.iteration_listeners'); |
73
|
|
|
|
74
|
|
|
foreach ($ids as $serviceId) { |
75
|
|
|
$definition = $container->getDefinition($serviceId); |
76
|
|
|
|
77
|
|
|
$class = new \ReflectionClass($definition->getClass()); |
78
|
|
|
if (!$class->implementsInterface(IterationListener::class)) { |
79
|
|
|
throw new RuntimeException( |
80
|
|
|
'Iteration listeners must implement Sonata\NotificationBundle\Event\IterationListener' |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$definition->addTag( |
85
|
|
|
'kernel.event_listener', |
86
|
|
|
['event' => IterateEvent::EVENT_NAME, 'method' => 'iterate'] |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|