1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sonata Project package. |
5
|
|
|
* |
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sonata\NotificationBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
15
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
16
|
|
|
use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
17
|
|
|
use Symfony\Component\Config\Definition\ConfigurationInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This is the class that validates and merges configuration from your app/config files. |
21
|
|
|
* |
22
|
|
|
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class} |
23
|
|
|
*/ |
24
|
|
|
class Configuration implements ConfigurationInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function getConfigTreeBuilder() |
30
|
|
|
{ |
31
|
|
|
$treeBuilder = new TreeBuilder(); |
32
|
|
|
$rootNode = $treeBuilder->root('sonata_notification')->children(); |
33
|
|
|
|
34
|
|
|
$backendInfo = <<<'EOF' |
35
|
|
|
Other backends you can use: |
36
|
|
|
|
37
|
|
|
sonata.notification.backend.postpone |
38
|
|
|
sonata.notification.backend.doctrine |
39
|
|
|
sonata.notification.backend.rabbitmq |
40
|
|
|
EOF; |
41
|
|
|
|
42
|
|
|
$iterationListenersInfo = <<<EOF |
43
|
|
|
Listeners attached to the IterateEvent |
44
|
|
|
Iterate event is thrown on each command iteration |
45
|
|
|
|
46
|
|
|
Iteration listener class must implement Sonata\NotificationBundle\Event\IterationListener |
47
|
|
|
EOF; |
48
|
|
|
|
49
|
|
|
$rootNode |
50
|
|
|
->scalarNode('backend') |
51
|
|
|
->info($backendInfo) |
52
|
|
|
->defaultValue('sonata.notification.backend.runtime') |
53
|
|
|
->end() |
54
|
|
|
->append($this->getQueueNode()) |
55
|
|
|
->arrayNode('backends') |
56
|
|
|
->children() |
57
|
|
|
->arrayNode('doctrine') |
58
|
|
|
->children() |
59
|
|
|
->scalarNode('message_manager') |
60
|
|
|
->defaultValue('sonata.notification.manager.message.default') |
61
|
|
|
->end() |
62
|
|
|
->scalarNode('max_age') |
63
|
|
|
->info('The max age in seconds') |
64
|
|
|
->defaultValue(86400) |
65
|
|
|
->end() |
66
|
|
|
->scalarNode('pause') |
67
|
|
|
->info('The delay in microseconds') |
68
|
|
|
->defaultValue(500000) |
69
|
|
|
->end() |
70
|
|
|
->scalarNode('batch_size') |
71
|
|
|
->info('The number of items on each iteration') |
72
|
|
|
->defaultValue(10) |
73
|
|
|
->end() |
74
|
|
|
->arrayNode('states') |
75
|
|
|
->info('Raising errors level') |
76
|
|
|
->addDefaultsIfNotSet() |
77
|
|
|
->children() |
78
|
|
|
->integerNode('in_progress') |
79
|
|
|
->defaultValue(10) |
80
|
|
|
->end() |
81
|
|
|
->integerNode('error') |
82
|
|
|
->defaultValue(20) |
83
|
|
|
->end() |
84
|
|
|
->integerNode('open') |
85
|
|
|
->defaultValue(100) |
86
|
|
|
->end() |
87
|
|
|
->integerNode('done') |
88
|
|
|
->defaultValue(10000) |
89
|
|
|
->end() |
90
|
|
|
->end() |
91
|
|
|
->end() |
92
|
|
|
->end() |
93
|
|
|
->end() |
94
|
|
|
->arrayNode('rabbitmq') |
95
|
|
|
->children() |
96
|
|
|
->scalarNode('exchange') |
97
|
|
|
->cannotBeEmpty() |
98
|
|
|
->isRequired() |
99
|
|
|
->end() |
100
|
|
|
->arrayNode('connection') |
101
|
|
|
->addDefaultsIfNotSet() |
102
|
|
|
->children() |
103
|
|
|
->scalarNode('host') |
104
|
|
|
->defaultValue('localhost') |
105
|
|
|
->end() |
106
|
|
|
->scalarNode('port') |
107
|
|
|
->defaultValue(5672) |
108
|
|
|
->end() |
109
|
|
|
->scalarNode('user') |
110
|
|
|
->defaultValue('guest') |
111
|
|
|
->end() |
112
|
|
|
->scalarNode('pass') |
113
|
|
|
->defaultValue('guest') |
114
|
|
|
->end() |
115
|
|
|
->scalarNode('vhost') |
116
|
|
|
->defaultValue('guest') |
117
|
|
|
->end() |
118
|
|
|
->scalarNode('console_url') |
119
|
|
|
->defaultValue('http://localhost:55672/api') |
120
|
|
|
->end() |
121
|
|
|
->end() |
122
|
|
|
->end() |
123
|
|
|
->end() |
124
|
|
|
->end() |
125
|
|
|
->end() |
126
|
|
|
->end() |
127
|
|
|
->arrayNode('consumers') |
128
|
|
|
->addDefaultsIfNotSet() |
129
|
|
|
->children() |
130
|
|
|
->booleanNode('register_default') |
131
|
|
|
->info('If set to true, SwiftMailerConsumer and LoggerConsumer will be registered as services') |
132
|
|
|
->defaultTrue() |
133
|
|
|
->end() |
134
|
|
|
->end() |
135
|
|
|
->end() |
136
|
|
|
->arrayNode('iteration_listeners') |
137
|
|
|
->info($iterationListenersInfo) |
138
|
|
|
->defaultValue(array()) |
139
|
|
|
->prototype('scalar')->end() |
140
|
|
|
->end() |
141
|
|
|
->arrayNode('class') |
142
|
|
|
->addDefaultsIfNotSet() |
143
|
|
|
->children() |
144
|
|
|
->scalarNode('message') |
145
|
|
|
->defaultValue('Application\\Sonata\\NotificationBundle\\Entity\\Message') |
146
|
|
|
->end() |
147
|
|
|
->end() |
148
|
|
|
->end() |
149
|
|
|
->arrayNode('admin') |
150
|
|
|
->addDefaultsIfNotSet() |
151
|
|
|
->children() |
152
|
|
|
->booleanNode('enabled') |
153
|
|
|
->defaultTrue() |
154
|
|
|
->end() |
155
|
|
|
->arrayNode('message') |
156
|
|
|
->addDefaultsIfNotSet() |
157
|
|
|
->children() |
158
|
|
|
->scalarNode('class') |
159
|
|
|
->cannotBeEmpty() |
160
|
|
|
->defaultValue('Sonata\\NotificationBundle\\Admin\\MessageAdmin') |
161
|
|
|
->end() |
162
|
|
|
->scalarNode('controller') |
163
|
|
|
->cannotBeEmpty() |
164
|
|
|
->defaultValue('SonataNotificationBundle:MessageAdmin') |
165
|
|
|
->end() |
166
|
|
|
->scalarNode('translation') |
167
|
|
|
->cannotBeEmpty() |
168
|
|
|
->defaultValue('SonataNotificationBundle') |
169
|
|
|
->end() |
170
|
|
|
->end() |
171
|
|
|
->end() |
172
|
|
|
->end() |
173
|
|
|
->end() |
174
|
|
|
; |
175
|
|
|
|
176
|
|
|
return $treeBuilder; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return ArrayNodeDefinition|NodeDefinition |
181
|
|
|
*/ |
182
|
|
|
protected function getQueueNode() |
183
|
|
|
{ |
184
|
|
|
$treeBuilder = new TreeBuilder(); |
185
|
|
|
$node = $treeBuilder->root('queues'); |
186
|
|
|
|
187
|
|
|
$queuesInfo = <<<'EOF' |
188
|
|
|
Example for using RabbitMQ |
189
|
|
|
- { queue: myQueue, recover: true, default: false, routing_key: the_routing_key, dead_letter_exchange: 'my.dead.letter.exchange' } |
190
|
|
|
- { queue: catchall, default: true } |
191
|
|
|
|
192
|
|
|
Example for using Doctrine |
193
|
|
|
- { queue: sonata_page, types: [sonata.page.create_snapshot, sonata.page.create_snapshots] } |
194
|
|
|
- { queue: catchall, default: true } |
195
|
|
|
EOF; |
196
|
|
|
|
197
|
|
|
$routingKeyInfo = <<<'EOF' |
198
|
|
|
Only used by RabbitMQ |
199
|
|
|
|
200
|
|
|
Direct exchange with routing_key |
201
|
|
|
EOF; |
202
|
|
|
|
203
|
|
|
$recoverInfo = <<<'EOF' |
204
|
|
|
Only used by RabbitMQ |
205
|
|
|
|
206
|
|
|
If set to true, the consumer will respond with a `basic.recover` when an exception occurs, |
207
|
|
|
otherwise it will not respond at all and the message will be unacknowledged |
208
|
|
|
EOF; |
209
|
|
|
|
210
|
|
|
$deadLetterExchangeInfo = <<<'EOF' |
211
|
|
|
Only used by RabbitMQ |
212
|
|
|
|
213
|
|
|
If is set, failed messages will be rejected and sent to this exchange |
214
|
|
|
EOF; |
215
|
|
|
|
216
|
|
|
$typesInfo = <<<'EOF' |
217
|
|
|
Only used by Doctrine |
218
|
|
|
|
219
|
|
|
Defines types handled by the message backend |
220
|
|
|
EOF; |
221
|
|
|
|
222
|
|
|
$connectionNode = $node |
|
|
|
|
223
|
|
|
->info($queuesInfo) |
224
|
|
|
->requiresAtLeastOneElement() |
225
|
|
|
->prototype('array') |
226
|
|
|
; |
227
|
|
|
|
228
|
|
|
$connectionNode |
229
|
|
|
->children() |
230
|
|
|
->scalarNode('queue') |
231
|
|
|
->info('The name of the queue') |
232
|
|
|
->cannotBeEmpty() |
233
|
|
|
->isRequired() |
234
|
|
|
->end() |
235
|
|
|
->booleanNode('default') |
236
|
|
|
->info('Set the name of the default queue') |
237
|
|
|
->defaultValue(false) |
238
|
|
|
->end() |
239
|
|
|
|
240
|
|
|
// RabbitMQ configuration |
241
|
|
|
->scalarNode('routing_key') |
242
|
|
|
->info($routingKeyInfo) |
243
|
|
|
->defaultValue('') |
244
|
|
|
->end() |
245
|
|
|
->booleanNode('recover') |
246
|
|
|
->info($recoverInfo) |
247
|
|
|
->defaultValue(false) |
248
|
|
|
->end() |
249
|
|
|
->scalarNode('dead_letter_exchange') |
250
|
|
|
->info($deadLetterExchangeInfo) |
251
|
|
|
->defaultValue(null) |
252
|
|
|
->end() |
253
|
|
|
|
254
|
|
|
// Database configuration (Doctrine) |
255
|
|
|
->arrayNode('types') |
256
|
|
|
->info($typesInfo) |
257
|
|
|
->defaultValue(array()) |
258
|
|
|
->prototype('scalar')->end() |
259
|
|
|
->end() |
260
|
|
|
->end(); |
261
|
|
|
|
262
|
|
|
return $node; |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: