|
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\NewsBundle\DependencyInjection; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\Doctrine\Mapper\Builder\OptionsBuilder; |
|
17
|
|
|
use Sonata\Doctrine\Mapper\DoctrineCollector; |
|
18
|
|
|
use Sonata\EasyExtendsBundle\Mapper\DoctrineCollector as DeprecatedDoctrineCollector; |
|
19
|
|
|
use Symfony\Component\Config\Definition\Processor; |
|
20
|
|
|
use Symfony\Component\Config\FileLocator; |
|
21
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
22
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
23
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
24
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
25
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @author Thomas Rabaix <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class SonataNewsExtension extends Extension |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @throws \InvalidArgumentException |
|
34
|
|
|
*/ |
|
35
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
|
36
|
|
|
{ |
|
37
|
|
|
$processor = new Processor(); |
|
38
|
|
|
$configuration = new Configuration(); |
|
39
|
|
|
$config = $processor->processConfiguration($configuration, $configs); |
|
40
|
|
|
$bundles = $container->getParameter('kernel.bundles'); |
|
41
|
|
|
|
|
42
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
43
|
|
|
$loader->load('actions.xml'); |
|
44
|
|
|
$loader->load('twig.xml'); |
|
45
|
|
|
$loader->load('form.xml'); |
|
46
|
|
|
$loader->load('core.xml'); |
|
47
|
|
|
$loader->load('serializer.xml'); |
|
48
|
|
|
$loader->load('command.xml'); |
|
49
|
|
|
|
|
50
|
|
|
if (isset($bundles['SonataBlockBundle'])) { |
|
51
|
|
|
$loader->load('block.xml'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (isset($bundles['FOSRestBundle'], $bundles['NelmioApiDocBundle'])) { |
|
55
|
|
|
$loader->load(sprintf('api_form_%s.xml', $config['db_driver'])); |
|
56
|
|
|
if ('doctrine_orm' === $config['db_driver']) { |
|
57
|
|
|
$loader->load('api_controllers.xml'); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$loader->load(sprintf('%s.xml', $config['db_driver'])); |
|
62
|
|
|
|
|
63
|
|
|
if (isset($bundles['SonataAdminBundle'])) { |
|
64
|
|
|
$loader->load(sprintf('%s_admin.xml', $config['db_driver'])); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (!isset($config['salt'])) { |
|
68
|
|
|
throw new \InvalidArgumentException('The configuration node "salt" is not set for the SonataNewsBundle (sonata_news)'); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
if (!isset($config['comment'])) { |
|
72
|
|
|
throw new \InvalidArgumentException('The configuration node "comment" is not set for the SonataNewsBundle (sonata_news)'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$container->getDefinition('sonata.news.hash.generator') |
|
76
|
|
|
->replaceArgument(0, $config['salt']); |
|
77
|
|
|
|
|
78
|
|
|
$container->getDefinition('sonata.news.permalink.date') |
|
79
|
|
|
->replaceArgument(0, $config['permalink']['date']); |
|
80
|
|
|
|
|
81
|
|
|
$container->setAlias('sonata.news.permalink.generator', $config['permalink_generator']); |
|
82
|
|
|
|
|
83
|
|
|
$container->setDefinition('sonata.news.blog', new Definition('Sonata\NewsBundle\Model\Blog', [ |
|
84
|
|
|
$config['title'], |
|
85
|
|
|
$config['link'], |
|
86
|
|
|
$config['description'], |
|
87
|
|
|
new Reference('sonata.news.permalink.generator'), |
|
88
|
|
|
])); |
|
89
|
|
|
|
|
90
|
|
|
$container->getDefinition('sonata.news.hash.generator') |
|
91
|
|
|
->replaceArgument(0, $config['salt']); |
|
92
|
|
|
|
|
93
|
|
|
$container->getDefinition('sonata.news.mailer') |
|
94
|
|
|
->replaceArgument(5, [ |
|
95
|
|
|
'notification' => $config['comment']['notification'], |
|
96
|
|
|
]); |
|
97
|
|
|
|
|
98
|
|
|
if ('doctrine_orm' === $config['db_driver']) { |
|
99
|
|
|
if (isset($bundles['SonataDoctrineBundle'])) { |
|
100
|
|
|
$this->registerSonataDoctrineMapping($config); |
|
101
|
|
|
} else { |
|
102
|
|
|
// NEXT MAJOR: Remove next line and throw error when not registering SonataDoctrineBundle |
|
103
|
|
|
$this->registerDoctrineMapping($config); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$this->configureClass($config, $container); |
|
108
|
|
|
$this->configureAdmin($config, $container); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param array $config |
|
113
|
|
|
*/ |
|
114
|
|
|
public function configureClass($config, ContainerBuilder $container): void |
|
115
|
|
|
{ |
|
116
|
|
|
// admin configuration |
|
117
|
|
|
$container->setParameter('sonata.news.admin.post.entity', $config['class']['post']); |
|
118
|
|
|
$container->setParameter('sonata.news.admin.comment.entity', $config['class']['comment']); |
|
119
|
|
|
|
|
120
|
|
|
// manager configuration |
|
121
|
|
|
$container->setParameter('sonata.news.manager.post.entity', $config['class']['post']); |
|
122
|
|
|
$container->setParameter('sonata.news.manager.comment.entity', $config['class']['comment']); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param array $config |
|
127
|
|
|
*/ |
|
128
|
|
|
public function configureAdmin($config, ContainerBuilder $container): void |
|
129
|
|
|
{ |
|
130
|
|
|
$container->setParameter('sonata.news.admin.post.class', $config['admin']['post']['class']); |
|
131
|
|
|
$container->setParameter('sonata.news.admin.post.controller', $config['admin']['post']['controller']); |
|
132
|
|
|
$container->setParameter('sonata.news.admin.post.translation_domain', $config['admin']['post']['translation']); |
|
133
|
|
|
|
|
134
|
|
|
$container->setParameter('sonata.news.admin.comment.class', $config['admin']['comment']['class']); |
|
135
|
|
|
$container->setParameter('sonata.news.admin.comment.controller', $config['admin']['comment']['controller']); |
|
136
|
|
|
$container->setParameter('sonata.news.admin.comment.translation_domain', $config['admin']['comment']['translation']); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* NEXT_MAJOR: Remove this method. |
|
141
|
|
|
*/ |
|
142
|
|
|
public function registerDoctrineMapping(array $config): void |
|
143
|
|
|
{ |
|
144
|
|
|
@trigger_error( |
|
|
|
|
|
|
145
|
|
|
'Using SonataEasyExtendsBundle is deprecated since sonata-project/news-bundle 3.x. Please register SonataDoctrineBundle as a bundle instead.', |
|
146
|
|
|
E_USER_DEPRECATED |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
$collector = DeprecatedDoctrineCollector::getInstance(); |
|
150
|
|
|
|
|
151
|
|
|
foreach ($config['class'] as $type => $class) { |
|
152
|
|
|
if (!class_exists($class)) { |
|
153
|
|
|
/* |
|
154
|
|
|
* NEXT_MAJOR: |
|
155
|
|
|
* Throw an exception if the class is not defined |
|
156
|
|
|
*/ |
|
157
|
|
|
@trigger_error(sprintf( |
|
|
|
|
|
|
158
|
|
|
'The "%s" class is not defined or does not exist. This is tolerated now but will be forbidden in 4.0', |
|
159
|
|
|
$class |
|
160
|
|
|
), E_USER_DEPRECATED); |
|
161
|
|
|
|
|
162
|
|
|
return; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
$collector->addAssociation($config['class']['post'], 'mapOneToMany', [ |
|
167
|
|
|
'fieldName' => 'comments', |
|
168
|
|
|
'targetEntity' => $config['class']['comment'], |
|
169
|
|
|
'cascade' => [ |
|
170
|
|
|
0 => 'remove', |
|
171
|
|
|
1 => 'persist', |
|
172
|
|
|
], |
|
173
|
|
|
'mappedBy' => 'post', |
|
174
|
|
|
'orphanRemoval' => true, |
|
175
|
|
|
'orderBy' => [ |
|
176
|
|
|
'createdAt' => 'DESC', |
|
177
|
|
|
], |
|
178
|
|
|
]); |
|
179
|
|
|
|
|
180
|
|
|
$collector->addAssociation($config['class']['post'], 'mapManyToOne', [ |
|
181
|
|
|
'fieldName' => 'image', |
|
182
|
|
|
'targetEntity' => $config['class']['media'], |
|
183
|
|
|
'cascade' => [ |
|
184
|
|
|
0 => 'remove', |
|
185
|
|
|
1 => 'persist', |
|
186
|
|
|
2 => 'refresh', |
|
187
|
|
|
3 => 'merge', |
|
188
|
|
|
4 => 'detach', |
|
189
|
|
|
], |
|
190
|
|
|
'mappedBy' => null, |
|
191
|
|
|
'inversedBy' => null, |
|
192
|
|
|
'joinColumns' => [ |
|
193
|
|
|
[ |
|
194
|
|
|
'name' => 'image_id', |
|
195
|
|
|
'referencedColumnName' => 'id', |
|
196
|
|
|
], |
|
197
|
|
|
], |
|
198
|
|
|
'orphanRemoval' => false, |
|
199
|
|
|
]); |
|
200
|
|
|
|
|
201
|
|
|
$collector->addAssociation($config['class']['post'], 'mapManyToOne', [ |
|
202
|
|
|
'fieldName' => 'author', |
|
203
|
|
|
'targetEntity' => $config['class']['user'], |
|
204
|
|
|
'cascade' => [ |
|
205
|
|
|
1 => 'persist', |
|
206
|
|
|
], |
|
207
|
|
|
'mappedBy' => null, |
|
208
|
|
|
'inversedBy' => null, |
|
209
|
|
|
'joinColumns' => [ |
|
210
|
|
|
[ |
|
211
|
|
|
'name' => 'author_id', |
|
212
|
|
|
'referencedColumnName' => 'id', |
|
213
|
|
|
], |
|
214
|
|
|
], |
|
215
|
|
|
'orphanRemoval' => false, |
|
216
|
|
|
]); |
|
217
|
|
|
|
|
218
|
|
|
$collector->addAssociation($config['class']['post'], 'mapManyToOne', [ |
|
219
|
|
|
'fieldName' => 'collection', |
|
220
|
|
|
'targetEntity' => $config['class']['collection'], |
|
221
|
|
|
'cascade' => [ |
|
222
|
|
|
1 => 'persist', |
|
223
|
|
|
], |
|
224
|
|
|
'mappedBy' => null, |
|
225
|
|
|
'inversedBy' => null, |
|
226
|
|
|
'joinColumns' => [ |
|
227
|
|
|
[ |
|
228
|
|
|
'name' => 'collection_id', |
|
229
|
|
|
'referencedColumnName' => 'id', |
|
230
|
|
|
], |
|
231
|
|
|
], |
|
232
|
|
|
'orphanRemoval' => false, |
|
233
|
|
|
]); |
|
234
|
|
|
|
|
235
|
|
|
$collector->addAssociation($config['class']['post'], 'mapManyToMany', [ |
|
236
|
|
|
'fieldName' => 'tags', |
|
237
|
|
|
'targetEntity' => $config['class']['tag'], |
|
238
|
|
|
'cascade' => [ |
|
239
|
|
|
1 => 'persist', |
|
240
|
|
|
], |
|
241
|
|
|
'joinTable' => [ |
|
242
|
|
|
'name' => $config['table']['post_tag'], |
|
243
|
|
|
'joinColumns' => [ |
|
244
|
|
|
[ |
|
245
|
|
|
'name' => 'post_id', |
|
246
|
|
|
'referencedColumnName' => 'id', |
|
247
|
|
|
], |
|
248
|
|
|
], |
|
249
|
|
|
'inverseJoinColumns' => [ |
|
250
|
|
|
[ |
|
251
|
|
|
'name' => 'tag_id', |
|
252
|
|
|
'referencedColumnName' => 'id', |
|
253
|
|
|
], |
|
254
|
|
|
], |
|
255
|
|
|
], |
|
256
|
|
|
]); |
|
257
|
|
|
|
|
258
|
|
|
$collector->addAssociation($config['class']['comment'], 'mapManyToOne', [ |
|
259
|
|
|
'fieldName' => 'post', |
|
260
|
|
|
'targetEntity' => $config['class']['post'], |
|
261
|
|
|
'cascade' => [ |
|
262
|
|
|
], |
|
263
|
|
|
'mappedBy' => null, |
|
264
|
|
|
'inversedBy' => 'comments', |
|
265
|
|
|
'joinColumns' => [ |
|
266
|
|
|
[ |
|
267
|
|
|
'name' => 'post_id', |
|
268
|
|
|
'referencedColumnName' => 'id', |
|
269
|
|
|
'nullable' => false, |
|
270
|
|
|
], |
|
271
|
|
|
], |
|
272
|
|
|
'orphanRemoval' => false, |
|
273
|
|
|
]); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
private function registerSonataDoctrineMapping(array $config): void |
|
277
|
|
|
{ |
|
278
|
|
|
foreach ($config['class'] as $type => $class) { |
|
279
|
|
|
if (!class_exists($class)) { |
|
280
|
|
|
return; |
|
281
|
|
|
} |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$collector = DoctrineCollector::getInstance(); |
|
285
|
|
|
|
|
286
|
|
|
$collector->addAssociation( |
|
287
|
|
|
$config['class']['post'], |
|
288
|
|
|
'mapOneToMany', |
|
289
|
|
|
OptionsBuilder::createOneToMany('comments', $config['class']['comment']) |
|
290
|
|
|
->cascade(['remove', 'persist']) |
|
291
|
|
|
->mappedBy('post') |
|
292
|
|
|
->orphanRemoval() |
|
293
|
|
|
->addOrder('createdAt', 'DESC') |
|
294
|
|
|
); |
|
295
|
|
|
|
|
296
|
|
|
$collector->addAssociation( |
|
297
|
|
|
$config['class']['post'], |
|
298
|
|
|
'mapManyToOne', |
|
299
|
|
|
OptionsBuilder::createManyToOne('image', $config['class']['media']) |
|
300
|
|
|
->cascade(['remove', 'persist', 'refresh', 'merge', 'detach']) |
|
301
|
|
|
->addJoin([ |
|
302
|
|
|
'name' => 'image_id', |
|
303
|
|
|
'referencedColumnName' => 'id', |
|
304
|
|
|
]) |
|
305
|
|
|
); |
|
306
|
|
|
|
|
307
|
|
|
$collector->addAssociation( |
|
308
|
|
|
$config['class']['post'], |
|
309
|
|
|
'mapManyToOne', |
|
310
|
|
|
OptionsBuilder::createManyToOne('author', $config['class']['user']) |
|
311
|
|
|
->cascade(['persist']) |
|
312
|
|
|
->addJoin([ |
|
313
|
|
|
'name' => 'author_id', |
|
314
|
|
|
'referencedColumnName' => 'id', |
|
315
|
|
|
]) |
|
316
|
|
|
); |
|
317
|
|
|
|
|
318
|
|
|
$collector->addAssociation( |
|
319
|
|
|
$config['class']['post'], |
|
320
|
|
|
'mapManyToOne', |
|
321
|
|
|
OptionsBuilder::createManyToOne('collection', $config['class']['collection']) |
|
322
|
|
|
->cascade(['persist']) |
|
323
|
|
|
->addJoin([ |
|
324
|
|
|
'name' => 'collection_id', |
|
325
|
|
|
'referencedColumnName' => 'id', |
|
326
|
|
|
]) |
|
327
|
|
|
); |
|
328
|
|
|
|
|
329
|
|
|
$collector->addAssociation( |
|
330
|
|
|
$config['class']['post'], |
|
331
|
|
|
'mapManyToMany', |
|
332
|
|
|
OptionsBuilder::createManyToMany('tags', $config['class']['tag']) |
|
333
|
|
|
->cascade(['persist']) |
|
334
|
|
|
->addJoinTable($config['table']['post_tag'], [[ |
|
335
|
|
|
'name' => 'post_id', |
|
336
|
|
|
'referencedColumnName' => 'id', |
|
337
|
|
|
]], [[ |
|
338
|
|
|
'name' => 'tag_id', |
|
339
|
|
|
'referencedColumnName' => 'id', |
|
340
|
|
|
]]) |
|
341
|
|
|
); |
|
342
|
|
|
|
|
343
|
|
|
$collector->addAssociation( |
|
344
|
|
|
$config['class']['comment'], |
|
345
|
|
|
'mapManyToOne', |
|
346
|
|
|
OptionsBuilder::createManyToOne('post', $config['class']['post']) |
|
347
|
|
|
->inversedBy('comments') |
|
348
|
|
|
->addJoin([ |
|
349
|
|
|
'name' => 'post_id', |
|
350
|
|
|
'referencedColumnName' => 'id', |
|
351
|
|
|
'nullable' => false, |
|
352
|
|
|
]) |
|
353
|
|
|
); |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: