1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\DependencyInjection\Factory\BroadcasterFactory; |
6
|
|
|
use IrishDan\NotificationBundle\DependencyInjection\Factory\ChannelFactory; |
7
|
|
|
use Symfony\Component\Config\FileLocator; |
8
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
9
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
10
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
11
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
13
|
|
|
|
14
|
|
|
class NotificationExtension extends Extension |
15
|
|
|
{ |
16
|
|
|
public function load(array $configs, ContainerBuilder $container) |
17
|
|
|
{ |
18
|
|
|
$configuration = new Configuration(); |
19
|
|
|
|
20
|
|
|
// Load our YAML resources |
21
|
|
|
$loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
22
|
|
|
$loader->load('services.yml'); |
23
|
|
|
|
24
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
25
|
|
|
|
26
|
|
|
foreach ($configs as $subConfig) { |
27
|
|
|
$config = array_merge($config, $subConfig); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
$enabledChannels = []; |
31
|
|
|
foreach ($config['channels'] as $channel => $channelConfig) { |
32
|
|
|
$enabledChannels[] = $channel; |
33
|
|
|
$container->setParameter('notification.channel.' . $channel . '.enabled', true); |
34
|
|
|
|
35
|
|
|
// Set a configuration parameter for each channel also. |
36
|
|
|
$parameters = empty($channelConfig) ? [] : $channelConfig; |
37
|
|
|
$parameterName = 'notification.channel.' . $channel . '.configuration'; |
38
|
|
|
$container->setParameter($parameterName, $parameters); |
39
|
|
|
|
40
|
|
|
// Create a service for this channel. |
41
|
|
|
$this->createChannelService($channel, $container); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$container->setParameter('notification.available_channels', $enabledChannels); |
45
|
|
|
|
46
|
|
|
// Create the channel service |
47
|
|
|
$this->createChannelManagerService($enabledChannels, $container); |
48
|
|
|
|
49
|
|
|
// Create the notification manager service |
50
|
|
|
$this->createNotificationManagerService($container); |
51
|
|
|
|
52
|
|
|
// Create services need for broadcasting |
53
|
|
|
if (!empty($config['broadcasters'])) { |
54
|
|
|
foreach ($config['broadcasters'] as $name => $config) { |
55
|
|
|
$this->createBroadcaster($name, $config, $container); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
private function createChannelService($channel, ContainerBuilder $container) |
61
|
|
|
{ |
62
|
|
|
$factory = new ChannelFactory(); |
63
|
|
|
$factory->create($container, $channel); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
private function createBroadcaster($name, $config, ContainerBuilder $container) |
67
|
|
|
{ |
68
|
|
|
$broadcastFactory = new BroadcasterFactory(); |
69
|
|
|
$broadcastFactory->create($container, $name, $config); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function createNotificationManagerService(ContainerBuilder $container) |
73
|
|
|
{ |
74
|
|
|
$definition = new Definition(); |
75
|
|
|
$definition->setClass('IrishDan\NotificationBundle\NotificationManager'); |
76
|
|
|
$definition->setArguments( |
77
|
|
|
[ |
78
|
|
|
new Reference('notification.channel_manager'), |
79
|
|
|
new Reference('notification.database_notification_manager'), |
80
|
|
|
] |
81
|
|
|
); |
82
|
|
|
$container->setDefinition('notification.manager', $definition); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function createChannelManagerService(array $enabledChannels, ContainerBuilder $container) |
86
|
|
|
{ |
87
|
|
|
$definition = new Definition(); |
88
|
|
|
$definition->setClass('IrishDan\NotificationBundle\ChannelManager'); |
89
|
|
|
$definition->setArguments( |
90
|
|
|
[ |
91
|
|
|
new Reference('event_dispatcher'), |
92
|
|
|
$container->getParameter('notification.available_channels'), |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
$container->setDefinition('notification.channel_manager', $definition); |
96
|
|
|
|
97
|
|
|
foreach ($enabledChannels as $channel) { |
98
|
|
|
// Add the channel to the channel manager service |
99
|
|
|
$channelManager = $container->getDefinition('notification.channel_manager'); |
100
|
|
|
$channelId = 'notification.channel.' . $channel; |
101
|
|
|
$channelManager->addMethodCall('setChannel', [$channel, new Reference($channelId)]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |