1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\DependencyInjection\Factory\Broadcaster\SlackBroadcasterFactory; |
6
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
7
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
8
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
|
11
|
|
|
class NotificationExtension extends Extension |
12
|
|
|
{ |
13
|
|
|
public function load(array $configs, ContainerBuilder $container) |
14
|
|
|
{ |
15
|
|
|
$configuration = new Configuration(); |
16
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
17
|
|
|
|
18
|
|
|
foreach ($configs as $subConfig) { |
19
|
|
|
$config = array_merge($config, $subConfig); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
$enabledChannels = []; |
23
|
|
|
foreach ($config['channels'] as $channel => $channelConfig) { |
24
|
|
|
$enabledChannels[] = $channel; |
25
|
|
|
$container->setParameter('notification.channel.' . $channel . '.enabled', true); |
26
|
|
|
|
27
|
|
|
// Set a configuration parameter for each channel also. |
28
|
|
|
$configuration = empty($channelConfig) ? [] : $channelConfig; |
29
|
|
|
$parameterName = 'notification.channel.' . $channel . '.configuration'; |
30
|
|
|
$container->setParameter($parameterName, $configuration); |
31
|
|
|
|
32
|
|
|
// Create a service for this channel. |
33
|
|
|
$this->createChannelService($channel, $container); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
// Create the channel service |
38
|
|
|
$this->createChannelManagerService($enabledChannels, $container); |
39
|
|
|
|
40
|
|
|
$container->setParameter('notification.available_channels', $enabledChannels); |
41
|
|
|
|
42
|
|
|
foreach ($config['broadcasters'] as $name => $config) { |
43
|
|
|
$adapters[$name] = $this->createBroadcaster($name, $config, $container); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function createChannelService($channel, ContainerBuilder $container) |
48
|
|
|
{ |
49
|
|
|
$definition = new Definition(); |
50
|
|
|
$definition->setClass('IrishDan\NotificationBundle\Channel\DefaultChannel'); |
51
|
|
|
$definition->setArguments( |
52
|
|
|
[ |
53
|
|
|
'%notification.channel.' . $channel . '.enabled%', |
54
|
|
|
'%notification.channel.' . $channel . '.configuration%', |
55
|
|
|
] |
56
|
|
|
); |
57
|
|
|
$formatter = new Reference('notification.' . $channel . '_data_formatter'); |
58
|
|
|
$dispatcher = new Reference('notification.' . $channel . '_message_dispatcher'); |
59
|
|
|
$definition->setMethodCalls( |
60
|
|
|
[ |
61
|
|
|
['setDataFormatter', [$formatter]], |
62
|
|
|
['setDispatcher', [$dispatcher]], |
63
|
|
|
] |
64
|
|
|
); |
65
|
|
|
$container->setDefinition('notification.channel.' . $channel, $definition); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function createChannelManagerService(array $enabledChannels, ContainerBuilder $container) |
69
|
|
|
{ |
70
|
|
|
$definition = new Definition(); |
71
|
|
|
$definition->setClass('IrishDan\NotificationBundle\ChannelManager'); |
72
|
|
|
$definition->setArguments( |
73
|
|
|
[ |
74
|
|
|
new Reference('event_dispatcher'), |
75
|
|
|
$enabledChannels, // @TODO: can we reference a parameter |
76
|
|
|
] |
77
|
|
|
); |
78
|
|
|
$container->setDefinition('notification.channel_manager', $definition); |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
foreach ($enabledChannels as $channel) { |
82
|
|
|
// Add the channel to the channel manager service |
83
|
|
|
$channelManager = $container->getDefinition('notification.channel_manager'); |
84
|
|
|
$channelId = 'notification.channel.' . $channel; |
85
|
|
|
$channelManager->addMethodCall('setChannel', [$channel, new Reference($channelId)]); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function createBroadcaster($name, $broadcaster, $container) |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
// @TODO: |
92
|
|
|
} |
93
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.